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.72k stars 1.31k forks source link

Redundant suffix when handling anonymous operation names with user-provided `namingConvention` function #10012

Open hecticme opened 2 weeks ago

hecticme commented 2 weeks ago

Which packages are impacted by your issue?

@graphql-codegen/visitor-plugin-common

Describe the bug

When using a custom namingConvention function to add a suffix for generated types, the operations' name does not match with the respective value inside TypedDocumentNode. See "Expected behavior" section for more details.

It looks like a redundant suffix is being added in handleAnonymousOperation:

https://github.com/dotansimha/graphql-code-generator/blob/9af9ce215e210241b1ae9e8b7e3f60e3f9f10aa7/packages/plugins/other/visitor-plugin-common/src/base-documents-visitor.ts#L218-L234

It's then added again in few lines underneath:

https://github.com/dotansimha/graphql-code-generator/blob/9af9ce215e210241b1ae9e8b7e3f60e3f9f10aa7/packages/plugins/other/visitor-plugin-common/src/base-documents-visitor.ts#L285-L293

Your Example Website or App

https://stackblitz.com/edit/gql-codgen-13f1zd

Steps to Reproduce the Bug or Issue

  1. Open the reproduction link
  2. Run npm run generate (optional)
  3. Take a look at the errors inside ~/__generated__/gql/graphql.ts

Or if you prefer, you can create a new project with the provided codegen.ts and run graphql-codegen.

Expected behavior

If I don't specify a namingConvention to add a suffix, this is how the generated code would look like:

export type UserQueryVariables = Exact<{ ... }>;
export type UserQuery = { ... };
export const UserDocument = { ... } as unknown as DocumentNode<UserQuery, UserQueryVariables>;

I expected that after adding the suffix, it would look like this:

export type UserQueryVariablesCustomSuffix = Exact<{ ... }>;
export type UserQueryCustomSuffix = { ... };
export const UserDocumentCustomSuffix = { ... } as unknown as DocumentNode<UserQueryCustomSuffix, UserQueryVariablesCustomSuffix>;

However, this is the result:

// šŸ‘‡ļø This does not match with DocumentNode in UserDocumentCustomSuffix
export type UserCustomSuffixQueryVariablesCustomSuffix = Exact<{ ... }>; // <-- šŸ‘€
// šŸ‘‡ļø This does not match with DocumentNode in UserDocumentCustomSuffix
export type UserCustomSuffixQueryCustomSuffix = { ... };

export const UserDocumentCustomSuffix = { ... } as unknown as DocumentNode<UserQueryCustomSuffix, UserQueryVariablesCustomSuffix>;

Screenshots or Videos

image

Platform

Codegen Config File

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

const config: CodegenConfig = {
  schema: 'schema.graphql',
  documents: 'document.graphql',
  generates: {
    './__generated__/gql/': {
      preset: 'client',
      config: {
        namingConvention: './appendSuffix',
      },
    },
  },
}

export default config