Codegen doesn't fail when identical operations are present in multiple files
Which packages are impacted by your issue?
@graphql-codegen/core
Describe the bug
Codegen doesn't throw any error if identical operations are present in different files
Your Example Website or App
https://codesandbox.io/p/devbox/zealous-leakey-cwjm77
Steps to Reproduce the Bug or Issue
- Clone https://codesandbox.io/p/devbox/zealous-leakey-cwjm77
- run
yarn codegen
Expected behavior
Expected to see Not all operations have an unique name: user: error
Screenshots or Videos
No response
Platform
- OS: any
- NodeJS: ^22
graphqlversion: ^16@graphql-codegen/*version(s): ^4
Codegen Config File
No response
Additional context
We do see the error when, in any of the file, we add a new field or remove a field, marking these operations as not identical. I think this can cause problems for a huge team if everyone is adding an identical operation in their own files, and suddenly adding another field breaks the codegen. IMO, the codegen should have failed the moment same name operations were introduced, and it should have forced devs to create a common gql file.
Hi @ankitprahladsoni , thanks for reporting this.
and it should have forced devs to create a common gql file.
Generally I advise not to create a common operation file to be shared. It's not guaranteed all the callers will always have the same requirements. There are some valid cases like shared components, but that's slightly different because these usually have operations built-in, and teams will consume the whole package without having to manually write operations.
We do see the error when, in any of the file, we add a new field or remove a field, marking these operations as not identical. I think this can cause problems for a huge team if everyone is adding an identical operation in their own files, and suddenly adding another field breaks the codegen.
I recommend adding some naming convention to distinguish where the query is being called e.g. having the component name or feature name in the operation name. That can help avoid conflicts
From Codegen's POV, two identical operations are treated as one, because they are when it comes to generated types and document nodes.
I think codegen should at least warn on such incidents, if not throw an error. I came to know about this error when a popular Apollo VSCode extension I use crashed on start with the duplicate operation name error. Or is possible, allow us to configure our codegen to throw an error in such cases. Unless there's some kind of push from the tools, it is difficult to always give a unique name to an operation based on the feature to avoid potential future conflicts, especially for a large team where a lot of subteams work in isolation.
Maybe an option called operationDedupeLogic: 'operation-name' | 'whole' could do. 🙂
const config: CodegenConfig = {
operationDedupeLogic: 'operation-name' // for your use case
}
Would you consider sending through a PR?