Skip to content

feat: support new option inlineReferencedFragmentsInFragmentDocuments… - #1673

Open
hoi4 wants to merge 4 commits into
dotansimha:mainfrom
hoi4:support-inlineReferencedFragmentsInFragmentDocuments-option-in-near-operation-file-preset
Open

hoi4 wants to merge 4 commits into
dotansimha:mainfrom
hoi4:support-inlineReferencedFragmentsInFragmentDocuments-option-in-near-operation-file-preset

Conversation

@hoi4

@hoi4 hoi4 commented Oct 21, 2026 •

Copy link
Copy Markdown

Description

Adds support for the inlineReferencedFragmentsInFragmentDocuments option, which was added in @graphql-codegen/visitor-plugin-common 7.4.0, to the near-operation-file preset.

When the option is on with documentMode: graphQLTag, each fragment document interpolates the documents of the fragments it spreads directly (${PageFragmentDoc}). Until now, the preset only imported the types of those external fragments, so the generated file failed to compile with Cannot find name 'PageFragmentDoc'.

With this change, a generated file also imports the *FragmentDoc of every external fragment it spreads directly (level === 0). Fragments that are only reached indirectly are not imported, because the documents that spread them already interpolate them. This also holds with dedupeFragments: true, so no unused imports are generated. When the option is off, or in any other documentMode, output is unchanged.

Changes:

  • fragment-resolver.ts: reads inlineReferencedFragmentsInFragmentDocuments (default false). In graphQLTag mode, the *FragmentDoc import is now also emitted for directly spread external fragments.
  • Bumps @graphql-codegen/visitor-plugin-common to ^7.4.0, which is required for the option.
  • Adds a minor changeset for @graphql-codegen/near-operation-file-preset.

Related #1672

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Added a unit test in near-operation-file.spec.ts with a chain of three fragments (Book → Page → Line) across separate files. It checks the imports generated for Book's file:

  • Option off: only PageFragment (the type) is imported, the same as before
  • Option on: PageFragmentDoc and PageFragment are imported, LineFragmentDoc is not
  • Option on + dedupeFragments: true: PageFragmentDoc is imported, LineFragmentDoc is not
  • Option on + documentMode: documentNode: imports are the same as before

Run with npx vitest run packages/presets/near-operation-file.

Test Environment:

  • OS: macOS 27.0.1
  • @graphql-codegen/near-operation-file-preset: 5.2.3 (+ @graphql-codegen/visitor-plugin-common 7.4.0)
  • NodeJS: v24.18.0

Checklist:

  • I have followed the
    CONTRIBUTING doc and the
    style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Further comments

  • The option itself is documented in visitor-plugin-common. The preset just honors it, so it has no option of its own to document.
  • #6520 - self-importing fragment and #6546 - duplicate fragment imports also fail locally on an unchanged main, so this PR doesn't cause them. Every other test passes, including the new one.

@changeset-bot

changeset-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8ffad7d

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/near-operation-file-preset Minor

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

@hoi4

hoi4 commented Nov 2, 2026

Copy link
Copy Markdown
Author

Could you maybe have a quick look? @dotansimha ❤️

(Not sure who to tag - I hope that's fine)

…ocuments-option-in-near-operation-file-preset
@hoi4

hoi4 commented Nov 14, 2026

Copy link
Copy Markdown
Author

Or maybe @eddeee888 since you were already involved in the respective change in the visitor-plugin-common change 🙏🏻

@yharaskrik

yharaskrik commented Nov 18, 2026 •

Copy link
Copy Markdown

We hit this exact bug upgrading @graphql-codegen/near-operation-file-preset to 6.0.1 in the Trellis.org monorepo, and this PR is the same fix we ended up patching locally. Adding some production data in case it helps get this merged.

Validation in a real monorepo

  • Repo: private Trellis monorepo (Nx + Angular + Apollo), 2,294 GraphQL documents across 204 codegen targets.
  • Config: inlineReferencedFragmentsInFragmentDocuments: true on every Apollo Angular output, documentMode: graphQLTag.
  • Without the fix: a generated fragment-only document interpolates ${OtherFragmentDoc} but never imports it, so tsc fails with Cannot find name 'OtherFragmentDoc'.
  • Scale of the breakage: of 586 fragment-only documents, 165 spread a fragment defined in another file, producing 350 missing *FragmentDoc imports.
  • With this change (applied as a pnpm patch to the compiled cjs + esm output): the full codegen upgrade PR — 550 files changed, 3.8k insertions — passes all checks

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.

2 participants