dotansimha / dotansimha/graphql-code-generator
PreResolveTypes: true with template MaybeValues is broken
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
### Which packages are impacted by your issue?
@graphql-codegen/typescript-operations
### Describe the bug
**Description**
If I use a template `MaybeValue` like [suggested here ](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations#allow-null-in-resolvers)
```
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript'],
config: {
maybeValue: 'T extends PromiseLike ? Promise : T | null'
},
},
},
};
```
And I use the default `preResolveTypes: true` then the generated types will be broken for simple queries.
### Your Example Website or App
https://stackblitz.com/edit/github-55hzb3?file=codegen.ts,document.graphql,schema.graphql,types.ts
### Steps to Reproduce the Bug or Issue
Given this schema:
```
interface AB {
id: String!
}
type AConfig {
key: String!
}
type BConfig {
value: String!
}
type A implements AB {
id: String!
config: AConfig!
}
type B implements AB {
id: String!
config: BConfig!
}
type Query {
ab(id: String!): AB
}
```
And this operation:
```
query GetAB($id: String!) {
ab(id: $id) {
id
... on A {
config {
key
}
}
... on B {
config {
value
}
}
}
}
```
And this codegen-config:
```
import type { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
overwrite: true,
schema: "schema.graphql",
documents: ["./operations"],
generates: {
"src/generated/graphql.ts": {
plugins: ["typescript", "typescript-operations", "typescript-resolvers"],
config: {
maybeValue:
"T extends PromiseLike ? Promise : T | null | undefined",
omitOperationSuffix: true
},
},
},
};
export default config;
```
Then the generated type for the query operation defined above will be generated as:
```
export type GetAb = {
__typename?: 'Query';
ab?:
| {
__typename?: 'A';
id: string;
config: { __typename?: 'AConfig'; key: string };
}
| {
__typename?: 'B';
id: string;
config: { __typename?: 'BConfig'; value: string };
} extends PromiseLike
? Promise
: T | null | undefined;
};
```
But `ab` here resolves to `any` because `T` is not defined (since it has preResolved the types and removed the `Maybe` wrapper.
If I instead set `preResolveTypes: false`, it works as expected (but I expect this to work with the default values too).
### Expected behavior
I expected `ab` not be resolved to `any` when `preResolveTypes: true` is set.
### Screenshots or Videos
_No response_
### Platform
- OS: macOS
- NodeJS: 18.20.3 (but doesnt really matter)
- `graphql` version: 16.2.0
- `@graphql-codegen/*` version(s): 4.0.1
### Codegen Config File
```
import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'schema.graphql',
documents: 'document.graphql',
generates: {
'types.ts': {
plugins: ['typescript', 'typescript-operations'],
config: {
maybeValue:
'T extends PromiseLike ? Promise : T | null | undefined',
omitOperationSuffix: true,
},
},
},
};
export default config;
```
### Additional context
_No response_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.