Skip to content

near-operation-file preset: "Multiple fragments with the name(s) ... were found" error with fragments in different directories #405

@JMartinCollins

Description

@JMartinCollins

Which packages are impacted by your issue?

@graphql-codegen/near-operation-file-preset

Describe the bug

image
This is the result of running npx graphql-codegen --verbose

The directory structure is as follows with each fragment appearing once in different files and different directories.
image

If I comment out two of these, it breaks my code but all of the types are correctly generated for other modules in [moduleName].generated.ts and in the same directory as that module.

It's necessary that these fragments all share the same name because the parent is responsible for rendering the child component and it passes a fragment to tell it what fields it needs fetched from the API, and the child spread the fragment into its own query.

From what I understand the skipDocumentsValidation.skipDuplicateValidation is the related config setting: https://the-guild.dev/graphql/codegen/docs/config-reference/codegen-config

And it's supposed to be active by default:

Your Example Website or App

example.com

Steps to Reproduce the Bug or Issue

  1. Make fragments with the same name in nested directories under the components directory, using gql template tag.
  2. Run the codegen with the provided settings.

Expected behavior

I expected that the codegen would write the generated types to file without error. Since I'm using the near-operation-file preset it shouldn't matter if there's a naming conflict in different directories because each different fragment type definition will be written to its own ts file in a different location.

Screenshots or Videos

No response

Platform

  • OS: Windows 10
  • NodeJS: 16.16.0
  • graphql version: 16.6.0
  • @graphql-codegen/cli: 5.0.0
  • @graphql-codegen/core: 4.0.0
  • @graphql-codegen/typescript: 4.0.1
  • @graphql-codegen/typescript-operations: 4.0.1
  • @graphql-codegen/near-operation-file-preset: 2.5.0

Codegen Config File

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

const config: CodegenConfig = {
    schema: 'http://localhost/graphql',
    ignoreNoDocuments: true, // for better experience with the watcher
    generates: {
        'types/operationTypes.ts': {
            plugins: ['typescript'],
        },
        './': {
            documents: ['components/**/!(*.generated).{ts,tsx}'],
            preset: 'near-operation-file',
            presetConfig: {
                extension: '.generated.ts',
                baseTypesPath: 'types/operationTypes.ts',
            },
            plugins: ['typescript-operations']
        }
    }
}

export default config;

Additional context

Related issues:
#383
dotansimha/graphql-code-generator#5196
dotansimha/graphql-code-generator#2890

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions