graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

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

Open hecticme opened this issue 1 year ago • 0 comments

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

  • OS: Linux 6.8 Fedora Linux 40 (Workstation Edition)
  • NodeJS: 20.14.0
  • graphql version: 16.8.2
  • @graphql-codegen/cli version: 5.0.2

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

hecticme avatar Jun 18 '24 11:06 hecticme