Skip to content

[visitor-plugin-common] fix: keep local fragment definition over its external copy (#10932) - #11048

Open
akashchamp wants to merge 1 commit into
dotansimha:masterfrom
akashchamp:fix/near-op-local-fragment-doc
Open

akashchamp wants to merge 1 commit into
dotansimha:masterfrom
akashchamp:fix/near-op-local-fragment-doc

Conversation

@akashchamp

Copy link
Copy Markdown

Description

Related #10932

Since #8757, ClientSideBaseVisitor dedupes fragments by name with new Map(...), so the last entry wins. near-operation-file passes a fragment that is generated into the same output file both as a local definition and as an external fragment, so the external copy replaced the local one and get fragments() skipped it. The operation still interpolates ${ThingFieldsFragmentDoc}, so the generated file doesn't compile.

This keeps the local definition when the same fragment name also comes in as external (in either order), which is what happened before 2.13.8 with the array plus dependency-graph. Fragments from other files are unchanged: they are still only imported.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added a test in visitor-plugin-common/tests/client-side-base-visitor.spec.ts that passes the same fragment as local and external. It fails on master (expected '' to contain 'export const UserFieldsFragmentDoc') and passes with the fix.
  • Ran the issue's repro end to end in a scratch project: published @graphql-codegen/cli 7.4.5, near-operation-file-preset 5.2.2, typescript-operations 6.1.10 and typed-document-node 7.1.1 with documentMode: 'graphQLTag'. With the published visitor-plugin-common 7.2.9 the generated Thing.generated.ts has no ThingFieldsFragmentDoc and tsc fails with TS2304: Cannot find name 'ThingFieldsFragmentDoc'. With this branch's build of visitor-plugin-common swapped in, the doc is emitted and tsc --noEmit --strict passes. I also added a fragment in a separate file spread from another file's query: it is still imported from ./Other.generated, not redeclared.
  • pnpm vitest run, tsc --noEmit, eslint and prettier --check on the changed files. The only vitest failure (besides a watcher test that times out under load and passes on its own) is the import-types snapshot, which depends on the clone directory name and fails on master too.

Test Environment:

  • OS: Linux Mint
  • @graphql-codegen/visitor-plugin-common: 7.2.9 (master)
  • NodeJS: 24.21.0

…external copy (dotansimha#10932)

ClientSideBaseVisitor dedupes fragments by name into a Map, so when the
same fragment is passed both as a local definition and as an external
fragment (near-operation-file does this for fragments generated into
the same file), the external copy won and the fragment document was no
longer generated, while operations still interpolated it.

Keep the local definition in that case, as before 2.13.8.
@changeset-bot

changeset-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 35b92cc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@graphql-codegen/visitor-plugin-common Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant