dotansimha / dotansimha/graphql-code-generator
typescript: Code generation fails when a type in the GraphQL schema is a reserved Typescript type alongside usage of a plugin that utilizes that type
- 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?
_No response_
### Describe the bug
We had an instance where a GraphQL schema had a type name on it called `Record`. The Typescript type generator generates the following for this:
```
export type Record = {
__typename?: 'Record';
wins?: Maybe;
};
```
This causes issues elsewhere in the generated code, as it overrides the built-in Typescript `Record` type used in the generated Typescript file, which causes Typescript issues.
Specifically, `Record` seems to be an issue with the following configuration options:
```
preset: 'client',
config: {
documentMode: 'string',
},
```
The generated types include:
```
export class TypedDocumentString
extends String
implements DocumentTypeDecoration
{
__apiType?: DocumentTypeDecoration['__apiType'];
constructor(private value: string, public __meta__?: Record) { // <- this is the issue
super(value);
}
```
### Your Example Website or App
https://stackblitz.com/edit/github-yjyxnx?file=schema.graphql
### Steps to Reproduce the Bug or Issue
1. Run `yarn generate`.
2. Observe `src/generated/graphql.ts`.
3. Type errors exist on the following:
```
constructor(private value: string, public __meta__?: Record) {
```
with the error:
```
Type 'Record' is not generic.(2315)
```
### Expected behavior
As as user, I would expect the GraphQL type generator to not generate types that override Typescript built-ins, and instead rename or suffix types that collide with builtin types.
### Screenshots or Videos
_No response_
### Platform
- NodeJS: v18.20.3
- `graphql` version: 16.2.0
- `@graphql-codegen/*` version(s): 5.0.2
### Codegen Config File
import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'schema.graphql',
documents: ['src/**/*.ts'],
ignoreNoDocuments: true,
generates: {
'./src/generated/graphql/': {
preset: 'client',
config: {
documentMode: 'string',
},
},
'./schema.graphql': {
plugins: ['schema-ast'],
config: {
includeDirectives: 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.