Skip to content

fix(utils/astFromSchema): support GraphQLSchema instances without operation types defined#7679

Merged
ardatan merged 2 commits intomasterfrom
support-federation-style-subgraphs
Nov 11, 2025
Merged

fix(utils/astFromSchema): support GraphQLSchema instances without operation types defined#7679
ardatan merged 2 commits intomasterfrom
support-federation-style-subgraphs

Conversation

@ardatan
Copy link
Owner

@ardatan ardatan commented Nov 11, 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 }
)

@changeset-bot
Copy link

changeset-bot bot commented Nov 11, 2025

🦋 Changeset detected

Latest commit: 172f3d3

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 Patch
@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 11, 2025

📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for federation/subgraph style schemas in schema printing functionality.
  • Bug Fixes

    • Improved handling of schemas without operation types to correctly print schema definitions as extensions, enabling valid subgraph schemas with federation v2.0.

Summary by CodeRabbit

  • New Features

    • Added support for federation and subgraph style schemas with v2.0 linking, enabling valid schema generation for schemas without root operation types.
  • Improvements

    • Enhanced schema printing to correctly emit schemas lacking operation types as extensions for improved subgraph compatibility.

Walkthrough

Changed schema AST printing so schemas with empty operation type arrays are emitted as schema extensions (SCHEMA_EXTENSION) instead of definitions, added a test for federation-style subgraph printing, and included a changeset documenting the patch.

Changes

Cohort / File(s) Summary
Changeset documentation
.changeset/all-bats-make.md
Adds a changeset entry documenting the patch that introduces federation/subgraph support and describes usage with assumeValid/assumeValidSDL.
Core implementation
packages/utils/src/print-schema-with-directives.ts
Adjusted astFromSchema logic: use operationTypes?.length truthiness to choose between SCHEMA_DEFINITION and SCHEMA_EXTENSION, causing empty operation type arrays to be printed as extensions.
Test coverage
packages/utils/tests/print-schema-with-directives.spec.ts
Adds a test "prints a federation-style subgraph schema correctly"; imports stripIgnoredCharacters and asserts printed SDL matches expected federation-style SDL.

Sequence Diagram(s)

sequenceDiagram
  participant Test as Test
  participant Builder as buildSchema
  participant Printer as printSchemaWithDirectives
  participant AST as astFromSchema

  Note over Test,Builder: create subgraph schema with assumeValid/assumeValidSDL
  Test->>Builder: buildSchema(SDL, {assumeValid, assumeValidSDL})
  Builder-->>Test: GraphQLSchema (no operationTypes)

  Test->>Printer: printSchemaWithDirectives(schema)
  Printer->>AST: astFromSchema(schema)
  alt schema.operationTypes.length > 0
    AST->>AST: emit SCHEMA_DEFINITION
  else schema.operationTypes.length == 0
    AST->>AST: emit SCHEMA_EXTENSION
  end
  AST-->>Printer: DocumentNode
  Printer-->>Test: SDL string with directives (extension printed)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Small conditional change in printing logic.
  • One new focused test and a changeset entry.
  • Pay attention to operationTypes null vs empty-array handling and test correctness.

Poem

🐰 A schema without roots in a subgraph glade,
Became an extension where directives parade.
I hopped in the code, changed one little line,
Now federation voices in harmony shine—
Hooray for small patches that brighten the glade! 🎉

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 describes the main change: adding support for GraphQLSchema instances without operation types in astFromSchema.
Description check ✅ Passed The description explains the feature addition, rationale, and provides a code example demonstrating the federation/subgraph use case.
✨ 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 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 9ac3293 and 172f3d3.

📒 Files selected for processing (1)
  • packages/utils/src/print-schema-with-directives.ts (1 hunks)
⏰ 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: Unit Test on Node 24 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v15
  • GitHub Check: Unit Test on Node 18 (windows-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 22 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Full Check on GraphQL v16
  • GitHub Check: Unit Test on Bun
  • GitHub Check: alpha / snapshot
  • GitHub Check: deployment
🔇 Additional comments (1)
packages/utils/src/print-schema-with-directives.ts (1)

182-182: LGTM! This correctly fixes the kind determination for empty operation types.

The change from operationTypes ? ... to operationTypes.length ? ... is crucial because operationTypes is always an array (created on line 173). The previous code would incorrectly emit SCHEMA_DEFINITION even for empty arrays, whereas the new code correctly emits SCHEMA_EXTENSION when no operation types are defined. This enables proper support for federation-style subgraph schemas and makes the logic consistent with the early-return check on line 177.


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

github-actions bot commented Nov 11, 2025

💻 Website Preview

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

Copy link

@Aenimus Aenimus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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