"Not all operations have an unique name" error with "near-operation-file" preset

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
dotansimhacommented, Nov 26, 2019

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

1reaction
ardatancommented, Jan 31, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

near-operation-file-preset - GraphQL Code Generator
This preset generates a file per each operation file. Example. In order to use this preset, you need to add 2 outputs to...
Read more >
@graphql-codegen/near-operation-file-preset - npm
GraphQL Code Generator preset for generating operation code near the operation file. ... This package does not have a README.
Read more >
Graphql - what constitutes an operation having a unique name?
I am having trouble making my code generator run. The reason given is that Not all operations have an unique name: AllIssueGroups.
Read more >
graphql-code-generator - Bountysource
We are receiving an error when generating our types from graphql (via gatsby) that ... Graphql Schemas provide everything we need to generate...
Read more >
How I start a GraphQL app with Slash GraphQL - Dgraph Blog
GraphQL isn't coming into it until I have something to iterate on. ... @graphql-codegen/add @graphql-codegen/near-operation-file-preset.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found