dotansimha / dotansimha/graphql-code-generator-community
TypeGraphQL Unions don't generate properly
- Dominant language
- TypeScript
- Stars
- 137
- Forks
- 195
- Avg merge
- 6h 20m
- Merged PRs (30d)
- 16
Description
### Which packages are impacted by your issue?
@graphql-codegen/typescript-type-graphql
### Describe the bug
The motivation for this issue is that I'm trying to slowly adopt TypeGraphQL for a large existing project that uses the `typescript` and `typescript-resolvers` plugins. I'm aiming to remove the `typescript` plugin in favor of using the `typescript-type-graphql` plugin and have all of my TypeScript code in my app simply use these classes with their decorators instead of the types that are regularly generated.
The current setup is that we have a bunch of graphql files that we generate from. Notably, see the repro. The issue is specifically in these lines:
```ts
export type SomeUnion = A | B;
// ...
@TypeGraphQL.Field(type => SomeUnion)
someAB!: FixDecorator;
```
The problem is that in `@TypeGraphQL.Field(type => X)`, the `X` is required to be a value not a type — the typescript generator builds a regular union via its visitor that makes just does a `availableTypes.join(' | ')` to generate the resulting type, but TypeGraphQL needs that to be a class that is registered like https://typegraphql.com/docs/unions.html
I tried to tinker with the visitor at `packages/plugins/typescript/type-graphql/src/visitor.ts` but I couldn't figure it out. I want to do something similar to the enum type definition, but I think it should basically just generate the syntax from the TypeGraphQL docs
```ts
const SomeUnion = TypeGraphQL.createUnionType({
name: "SomeUnion",
types: () => [availableTypes.join(', ')] as const,
});
```
I apologize for not being able to take this idea to a real pull request, I'm new to OSS and I'm not sure how to test any resulting implementation locally. I'm going to keep trying to get that set up but figured I'd post this here anyways.
### Your Example Website or App
https://codesandbox.io/p/sandbox/fast-cloud-jywck6?file=%2Ftypes.ts%3A63%2C1
### Steps to Reproduce the Bug or Issue
1. Look at `schema.graphql` for SomeUnioin
2. See the resulting `types.ts` at line 74. This is not going to work in TypeGraphQL.
### Expected behavior
I wanted a real TypeGraphQL union to be created in the codegen and for that to be used in the field decorator.
### Screenshots or Videos
_No response_
### Platform
- OS: macOS
- NodeJS: `18.5.0`
- `graphql` version: `^16.8.1`
- `@graphql-codegen/*` version(s): [e.g. 2.6.2]
```
"@graphql-codegen/cli": "=2.6.2",
"@graphql-codegen/near-operation-file-preset": "=2.2.9",
"@graphql-codegen/typescript": "=2.4.8",
"@graphql-codegen/typescript-operations": "=2.3.5",
"@graphql-codegen/typescript-react-apollo": "=3.2.11",
"@graphql-codegen/typescript-resolvers": "=2.6.1",
"@graphql-codegen/typescript-type-graphql": "^3.0.0",
```
### Codegen Config File
```ts
// codegen.ts
import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
overwrite: true,
hooks: {
afterAllFileWrite: ['prettier --write'],
},
generates: {
'src/management/graphql/types.generated.ts': {
schema: [
'src/**/*schema.mgmt.graphql',
'!(**/*typegraphql*.graphql)', // Ignore any GraphQL file with 'typegraphql' in its name since they're auto-generated from the TypeGraphQL decorators
],
plugins: ['typescript', 'typescript-resolvers'],
config: {
enumsAsConst: true,
namingConvention: {
enumValues: 'keep',
},
useImplementingTypes: true,
},
},
'src/graphql-retail/types.generated.ts': {
schema: [
'src/**/*schema.retail.graphql',
'!(**/*typegraphql*.graphql)', // Ignore any GraphQL file with 'typegraphql' in its name since they're auto-generated from the TypeGraphQL decorators
],
plugins: [
'typescript-type-graphql',
'typescript-resolvers',
{
add: {
// support `contextType` below. Use an explicit `add` instead of `module#type` to ensure an `import type`
content: "import type { AuthenticationContextType } from 'src/graphql-retail/middleware/authenticate/types';",
},
},
{
add: {
content: "import { JSONSchema7 } from 'json-schema';",
},
},
],
config: {
enumsAsConst: true,
namingConvention: {
enumValues: 'keep',
},
useImplementingTypes: true,
scalars: {
JsonSchema7: 'JSONSchema7',
},
contextType: 'AuthenticationContextType', // imported above
},
},
},
};
export default config;
### Additional context
_No response_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.