Graphql-code-generator: "Not all operations have an unique name" error with "near-operation-file" preset

Created on 5 Nov 2019  路  4Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

I am using the 'near-operation-file' preset to generate types next to the files that have the GraphQL operations. When I try to generate the types again though I get the "Not all operations have an unique name" error, presumably because the generated types are not ignored.

To Reproduce
Steps to reproduce the behavior:

  • Use the following config with a query in a .ts file.
  • Generate the types using @graphql-codegen.
  • Generate the types again.

My codegen.yml config file:

overwrite: true
schema: 'http://localhost:24000/graphql'
documents:
  - './pages/**/*.{ts,tsx}'
  - './components/**/*.{ts,tsx}'
generates:
  ./generated/graphql/types.ts:
    plugins:
      - 'typescript'
  ./:
    preset: 'near-operation-file'
    presetConfig:
      extension: '.gql.types.tsx'
      baseTypesPath: 'generated/graphql/types.ts'
    plugins:
      - 'typescript-operations'
      - 'typescript-react-apollo'
    config:
      withHooks: true
hooks:
  afterAllFileWrite:
    - prettier --write

Expected behavior

It should work and not error.

Environment:

  • OS: macOS 10.14.5
  • @graphql-codegen: 1.8.3
  • Node: 11.10.1

Additional context

Similar issues I could find:

bug presets

Most helpful comment

This was already fixed, but in your use case, you are loading the documents on the root level, and it causes validation to run while generating the ./generated/graphql/types.ts base file.

You should do something like that:

overwrite: true
schema: 'http://localhost:24000/graphql'
generates:
  ./generated/graphql/types.ts:
    plugins:
      - 'typescript'
  ./:
    preset: 'near-operation-file'
    documents:
      - './pages/**/*.{ts,tsx}'
      - './components/**/*.{ts,tsx}'
    presetConfig:
      extension: '.gql.types.tsx'
      baseTypesPath: 'generated/graphql/types.ts'
    plugins:
      - 'typescript-operations'
      - 'typescript-react-apollo'
    config:
      withHooks: true
hooks:
  afterAllFileWrite:
    - prettier --write

If it's in the root level, then while running generated/graphql/types.ts, it causes it to pass documents (and it's not necessary there, build it checks it anyway because of the current plugins architecture).

@vasilak

All 4 comments

This was already fixed, but in your use case, you are loading the documents on the root level, and it causes validation to run while generating the ./generated/graphql/types.ts base file.

You should do something like that:

overwrite: true
schema: 'http://localhost:24000/graphql'
generates:
  ./generated/graphql/types.ts:
    plugins:
      - 'typescript'
  ./:
    preset: 'near-operation-file'
    documents:
      - './pages/**/*.{ts,tsx}'
      - './components/**/*.{ts,tsx}'
    presetConfig:
      extension: '.gql.types.tsx'
      baseTypesPath: 'generated/graphql/types.ts'
    plugins:
      - 'typescript-operations'
      - 'typescript-react-apollo'
    config:
      withHooks: true
hooks:
  afterAllFileWrite:
    - prettier --write

If it's in the root level, then while running generated/graphql/types.ts, it causes it to pass documents (and it's not necessary there, build it checks it anyway because of the current plugins architecture).

@vasilak

Hmm, OK, so I have to add it for every task separately.
Sounds good, thanks for looking into it 馃憤

I found what cousing this problem on my end , documents: "./src/**/*.{tsx,ts}" , I include everything including the already exist generated types , to be look and generate . Either delete the generated types file everytime running codegen or modify the config to exlude the generated type like this documents: "./src/**/!(*.types).{tsx,ts} would solve this issue. My generated types file look something like this "types.type.tsx".

@syahmiyani Try to use more specific glob patterns like *.graphql.ts etc

Was this page helpful?
0 / 5 - 0 ratings