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.87k stars 1.34k forks source link

Uppercase and Lowercase graphql operation names result in duplicated types / interfaces in the generated graphql.ts #8774

Open gentlementlegen opened 1 year ago

gentlementlegen commented 1 year ago

Which packages are impacted by your issue?

@graphql-codegen/client-preset

Describe the bug

When generating the graphql.ts from codegen using client-preset, there is a scenario where no error in thrown but duplicated interfaces / types are created in the generated file, causing the TypeScript compilation to fail afterwards.

This happens when the names given to the operations are unique and differ only with the case, e.g.:

query myQuery {
    myQuery {
        id
    }
}

query MyQuery {
    myQuery {
        id
    }
}

will not trigger any error on the generation but will create twice the MyQuery type, resulting in compile errors.

Your Example Website or App

https://stackblitz.com/edit/github-7tzyn8?file=src/gql/graphql.ts

Steps to Reproduce the Bug or Issue

  1. Use the client preset for codegen with graphql
  2. Create 2 queries named the same but one with an uppercase and the other with a lowercase
  3. Observe that the code generation happens normally
  4. Notice the generated graphql.ts containing duplicated types

Expected behavior

Either a Not all operations have an unique name should be thrown, or having a myQuery and MyQuery type generated (first solution seems to make more sense).

Screenshots or Videos

No response

Platform

Codegen Config File

import { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  schema: 'schema.graphql',
  overwrite: true,
  documents: ['src/**/*.tsx'],
  ignoreNoDocuments: true,
  generates: {
    './src/gql/': {
      preset: 'client',
      plugins: [],
    },
  },
};

export default config;

Additional context

No response

saihaj commented 1 year ago

ok so after some digging in I realized this happens because the output types follow namingConvention which can be defined by the user and default it is change-case-all#pascalCase https://the-guild.dev/graphql/codegen/docs/config-reference/naming-convention#namingconvention

One option is during generation we normalize all names and then it will lead to error for unique names which I guess we can somehow better suggest hints to rename cause today it will just generate types with duplicates which is way more annoying to debug.

Thoughts @dotansimha @n1ru4l @B2o5T?

iki commented 1 month ago

Also fails for enums with case sensitive keys, which can be resolved with

 config:
   namingConvention:
     enumValues: keep