dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.86k stars 1.33k forks source link

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 #10161

Open catdalfonso opened 1 month ago

catdalfonso commented 1 month ago

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<Scalars['Int']['output']>;
};

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<TResult, TVariables>
  extends String
  implements DocumentTypeDecoration<TResult, TVariables>
{
  __apiType?: DocumentTypeDecoration<TResult, TVariables>['__apiType'];

  constructor(private value: string, public __meta__?: Record<string, any>) { // <-  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<string, any>) {

    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

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

PopGoesTheWza commented 5 days ago

Same thing happens with other reserved JavaScript/TypeScript words. It also occurs with properties of a type like function