Skip to content

fix(graphql): link the federation 2 spec in the auto schema file - #4189

Merged
kamilmysliwiec merged 1 commit into
masterfrom
fix/federation2-auto-schema-file-link
Oct 26, 2026
Merged

kamilmysliwiec merged 1 commit into
masterfrom
fix/federation2-auto-schema-file-link

Conversation

@kamilmysliwiec

Copy link
Copy Markdown
Member

PR Checklist

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)

PR Type

  • Bugfix

What is the current behavior?

With a code-first federation subgraph and autoSchemaFile: { path, federation: 2 }, the generated file contains the federation directive usages (@key, @shareable, @external, ...) but no extend schema @link(url: "https://specs.apollo.dev/federation/v2.x", import: [...]). The schema actually served (_service { sdl }) does have the @link: it is added to the in-memory type definitions after the file has been written. As a result, rover subgraph check / rover supergraph compose treat the file as a federation 1 subgraph and fail on federation 2 directives, e.g. Unknown directive "@shareable". ... note that this schema is a federation 1 schema. To be a federation 2 schema, it needs to @link to the federation specification v2.

Issue Number: #2765

What is the new behavior?

GraphQLFederationFactory decorates the file printer with the same TypeDefsFederation2Decorator (and the same autoSchemaFile.federation config: importUrl, directives) that is applied to the served type definitions, so the written file links the federation 2 spec and can be composed as-is. Federation 1 and non-federated schemas are untouched.

A new e2e spec (federation2-auto-schema-file.spec.ts) asserts that the written file parses, links the federation 2 spec, imports every federation directive it uses, and composes with another federation 2 subgraph via LocalCompose from @apollo/gateway. The @link assertion fails on master.

The original error in #2765 ("Type User must define one or more fields") no longer reproduces with current @apollo/subgraph; the remaining gap was the missing @link. Generating a subgraph's SDL without bootstrapping the app (GraphQLSchemaFactory + printSubgraphSchema) is covered by a docs PR.

Closes #2765

Does this PR introduce a breaking change?

  • Yes
  • No

The file emitted for federation: 2 now starts with extend schema @link(...). It already carried federation directive usages without their definitions, so it now matches the SDL the subgraph serves.

With `autoSchemaFile: { federation: 2 }`, the written SDL file kept the
federation directives but not the `extend schema @link(...)` that the
served subgraph schema gets, so tools such as `rover` read it as a
federation 1 subgraph and rejected federation 2 directives.

Decorate the printed file with the same federation 2 `@link` as the
served type definitions. Federation 1 and non-federated schemas are
unchanged.

Closes #2765
@kamilmysliwiec
kamilmysliwiec merged commit 9d34219 into master Oct 26, 2026
1 check passed
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.

Can not print schema for a subgraph: GraphQLError: Type User must define one or more fields

1 participant