Skip to content

Conversation

@ardatan
Copy link
Owner

@ardatan ardatan commented Nov 12, 2025

Support "federation/subgraph style" schemas in astFromSchema and printSchemaWithDirectives

If a GraphQLSchema doesn't have any defined operation types, we should print the schema definition as an extension rather than omitting it entirely.
They are not a valid schema on their own, but they are valid subgraph schemas in a federation setup, and it is possible to build such schemas with assumeValid options.

// A schema without defined root types
buildSchema(
  /* GraphQL */ `
    extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key"])

    type User @key(fields: "id") {
      id: ID!
      username: String
    }
  `,
  { assumeValid: true, assumeValidSDL: true }
)

POTENTIAL BREAKING CHANGE:
This can be a breaking change because now the schema above will be printed as the input, previously extend schema was converted to schema {}.

@changeset-bot
Copy link

changeset-bot bot commented Nov 12, 2025

🦋 Changeset detected

Latest commit: 7b3e869

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

This PR includes changesets to release 26 packages
Name Type
@graphql-tools/utils Major
@graphql-tools/executor Patch
@graphql-tools/graphql-tag-pluck Patch
@graphql-tools/import Patch
@graphql-tools/links Patch
@graphql-tools/load Patch
@graphql-tools/merge Patch
@graphql-tools/mock Patch
@graphql-tools/node-require Patch
@graphql-tools/relay-operation-optimizer Patch
@graphql-tools/resolvers-composition Patch
@graphql-tools/schema Patch
@graphql-tools/apollo-engine-loader Patch
@graphql-tools/code-file-loader Patch
@graphql-tools/git-loader Patch
@graphql-tools/github-loader Patch
@graphql-tools/graphql-file-loader Patch
@graphql-tools/json-file-loader Patch
@graphql-tools/module-loader Patch
@graphql-tools/url-loader Patch
@graphql-tools/executor-apollo-link Patch
@graphql-tools/executor-envelop Patch
@graphql-tools/executor-legacy-ws Patch
@graphql-tools/executor-urql-exchange Patch
@graphql-tools/executor-yoga Patch
graphql-tools 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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 12, 2025

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added support for federation-style subgraph schemas in schema printing operations.
  • Bug Fixes

    • Fixed schema extension handling to correctly print schemas without defined operation types as extension schemas rather than omitting them.
  • Tests

    • Added test coverage for federation-style subgraph schema printing validation.

Walkthrough

This change adds support for federation-style subgraph schemas in printSchemaWithDirectives. It modifies the logic to emit schemas as extend schema when no operation types are defined, allowing proper representation of GraphQL subgraph schemas that may lack operation root types.

Changes

Cohort / File(s) Change Summary
Federation subgraph schema support
packages/utils/src/print-schema-with-directives.ts
Modified schema kind selection logic to check operationTypes.length instead of operationTypes != null, ensuring schemas without operation types are emitted as SCHEMA_EXTENSION instead of SCHEMA_DEFINITION
Federation subgraph schema test
packages/utils/tests/print-schema-with-directives.spec.ts
Added test case "prints a federation-style subgraph schema correctly" to verify that subgraph SDL with extend schema and @key directive is correctly reproduced
Changeset documentation
.changeset/fine-jobs-love.md
Added changeset describing federation/subgraph schema support and documenting breaking change in schema output behavior

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Key attention area: Verify the logic change from operationTypes != null to operationTypes.length > 0 correctly handles empty array case
  • Confirm test assertion properly validates federation-style subgraph schema reproduction using stripIgnoredCharacters normalization

Possibly related PRs

Suggested reviewers

  • Aenimus

Poem

🐰 A schema without operations roams free,
Now extends itself proudly for all to see—
No more hidden as definitions so plain,
Federation flows through every domain! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: enhancing utils/astFromSchema to support GraphQLSchema instances without operation types, which is directly reflected in the changeset modifications to print-schema-with-directives.ts and the corresponding test.
Description check ✅ Passed The description is directly related to the changeset, explaining the federation/subgraph schema support and providing concrete examples and context for the changes made to handle schemas without operation types.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch revert-7683-revert-7679-support-federation-style-subgraphs

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8e2f481 and 7b3e869.

📒 Files selected for processing (3)
  • .changeset/fine-jobs-love.md (1 hunks)
  • packages/utils/src/print-schema-with-directives.ts (1 hunks)
  • packages/utils/tests/print-schema-with-directives.spec.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/utils/tests/print-schema-with-directives.spec.ts (1)
packages/utils/src/print-schema-with-directives.ts (1)
  • printSchemaWithDirectives (119-125)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: alpha / snapshot
  • GitHub Check: Unit Test on Node 18 (windows-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 22 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Node 24 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v16
  • GitHub Check: deployment
  • GitHub Check: Unit Test on Bun
  • GitHub Check: Full Check on GraphQL v16
🔇 Additional comments (4)
packages/utils/src/print-schema-with-directives.ts (1)

177-186: LGTM! Clean implementation of federation-style schema support.

The logic correctly determines the schema node kind based on the presence of operation types. When operationTypes is empty, the schema is printed as an extension (SCHEMA_EXTENSION), which properly supports federation/subgraph schemas that lack defined operation types. The early return guard on lines 177-179 ensures we only create a schema node when there's meaningful content (operations or directives).

packages/utils/tests/print-schema-with-directives.spec.ts (2)

16-16: Good addition of import for SDL normalization.

The stripIgnoredCharacters import supports the new test case and ensures reliable SDL comparison by removing formatting differences.


406-418: Solid test coverage for federation-style subgraph schemas.

The test correctly verifies that schemas without operation types (federation-style subgraphs) can be round-tripped through printSchemaWithDirectives. Using stripIgnoredCharacters for normalization ensures the comparison is resilient to whitespace differences.

.changeset/fine-jobs-love.md (1)

1-26: Well-documented breaking change with clear migration guidance.

The changeset appropriately marks this as a major version change and clearly explains both the new behavior and the potential breaking change. The included code example helps users understand the use case for federation-style subgraph schemas.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Contributor

💻 Website Preview

The latest changes are available as preview in: https://pr-7685.graphql-tools.pages.dev

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.

3 participants