dotansimha / dotansimha/graphql-code-generator-community

TypeGraphQL Generator with Union type fails with error

Open
#121 2 comments 2 reactions 0 assignees View on GitHub
help wanted
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

**Describe the bug**

When generating TypeGraphQL, using a union type, the type passed to `@TypeGraphQL.Field` fails with the error: `'Step' only refers to a type, but is being used as a value here. (TS 2693)`

**To Reproduce**
Steps to reproduce the behavior:

Run the generation below.

1. My GraphQL schema:

```graphql
type PullStep @entity {
nextSteps: [Step!] @column
# ...other fields...
}

type PushStep @entity {
nextSteps: [Step!] @column
# ...other fields...
}

union Step = PullStep | PushStep
```

2. My `codegen.yml` config file:

```yml
generates:
types.ts:
plugins:
- typescript-type-graphql
```

3. The output:

```ts
import * as TypeGraphQL from 'type-graphql';
export { TypeGraphQL };
export type Maybe = T | null;
export type Exact = { [K in keyof T]: T[K] };
export type MakeOptional = Omit & { [SubKey in K]?: Maybe };
export type MakeMaybe = Omit & { [SubKey in K]: Maybe };
export type FixDecorator = T;
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};

@TypeGraphQL.ObjectType()
export class PullStep {
__typename?: 'PullStep';

@TypeGraphQL.Field(type => [Step], { nullable: true })
// ^^^^ !!! "'Step' only refers to a type, but is being used as a value here. (TS 2693)" !!!
// ^^^^
nextSteps!: Maybe>;
};

@TypeGraphQL.ObjectType()
export class PushStep {
__typename?: 'PushStep';

@TypeGraphQL.Field(type => [Step], { nullable: true })
// ^^^^ !!! "'Step' only refers to a type, but is being used as a value here. (TS 2693)" !!!
// ^^^^
nextSteps!: Maybe>;
};

export type Step = PullStep | PushStep;

```

**Expected behavior**

No error. To fix this, it probably needs a union type generated:
https://typegraphql.com/docs/unions.html

**Environment:**

- OS: Linux
- `@graphql-codegen/...`: 1.21.3
- NodeJS: 14

**Additional context**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.