Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Oct 3, 2024 · 1 comment

Comments

@catdalfonso
Copy link

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

  • 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

@PopGoesTheWza
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@PopGoesTheWza @catdalfonso and others