GraphQL Federation & Composition
Federation lets many teams each own a subgraph and compose them into one supergraph the client sees as a single API. It is an org-boundary tool first and a technical one second — reach for it when team ownership is the problem, not when a single service simply got large.
Engineering judgment. GraphQL spec/library specifics change across versions — every version/library/spec-feature claim is [verify-at-use]. No PII.
Workflow
- Test whether you actually need federation. One team, one deployable, one datastore → a monolithic graph is simpler and faster; don't pay the gateway tax for it. Multiple teams needing independent deploy cadence and clear ownership of parts of the graph → federation. An existing set of separate GraphQL services you can't modify → schema stitching as the pragmatic (older, more manual) fallback.
- Draw ownership boundaries along the domain, not the data. Each subgraph owns a coherent slice —
accounts, products, reviews — and the types that belong to it. If two teams keep fighting over one type, the boundary is wrong. Ownership is the unit; the wire protocol serves it.
- Define entities with
@key. An entity is a type that more than one subgraph contributes fields to, identified by a @key. The owning subgraph defines the canonical type; other subgraphs extend it, keyed on the same field(s).
- Implement reference resolvers. A subgraph that extends an entity must resolve it from just the
@key fields the router passes it (__resolveReference / equivalent). Keep it a cheap, batchable lookup — the router will fan these out.
- Use
@external/@requires/@provides sparingly and deliberately. @external marks a field defined elsewhere that this subgraph references; @requires pulls a field from another subgraph to compute a local one (adds a router hop — cost); @provides lets a subgraph return a field it can supply inline to save a hop. Each is a performance/coupling lever, not decoration.
- Compose and validate the supergraph in CI. Composition catches conflicts (a type owned by two subgraphs, a
@key that doesn't resolve, an @external with no source) before deploy. A subgraph that composes locally but breaks the supergraph is a broken deploy — gate it. [verify-at-use] for the current composition tool/version.
- Budget the operational cost honestly. A federated gateway/router is a new tier: extra network hops per entity resolution, a single point of failure, distributed tracing across subgraphs, and schema-composition as a release gate. That cost is worth it for org scaling and worthless for a single team.
Decision table — federate vs monolith vs stitch
| Situation |
Choose |
Why |
| One team, one deployable |
Monolithic graph |
No gateway tax; simplest ops |
| Many teams, independent deploy cadence, clear domain slices |
Federation |
Ownership boundaries + independent deploys |
| Pre-existing GraphQL services you can't restructure |
Schema stitching |
Compose without re-architecting the subgraphs |
| One large service that's just messy internally |
Refactor the monolith |
Federation won't fix an internal-modularity problem |
Federation directive cheat-sheet
| Directive |
Means |
Cost / caution |
@key(fields: "id") |
This type is an entity, resolvable by these fields |
Fields must be stably resolvable |
@external |
Field is owned by another subgraph, referenced here |
Only meaningful with @requires/@provides |
@requires(fields: "…") |
Compute a local field using fields from elsewhere |
Adds a router round-trip — real latency |
@provides(fields: "…") |
This subgraph can return these inline |
Saves a hop only if the data is truly local |
@shareable / value types |
A field/type resolvable by more than one subgraph |
Values must agree or composition fails |
[verify-at-use] — directive names, syntax, and availability differ by federation version and library; confirm against the current spec before committing.
Subgraph & entity example
Owning subgraph (products) defines the canonical entity:
# products subgraph
type Product @key(fields: "id") {
id: ID!
name: String!
price: Money!
}
Extending subgraph (reviews) contributes fields to the same entity, keyed identically:
# reviews subgraph
type Product @key(fields: "id") {
id: ID!
reviews: [Review!]! # this subgraph's contribution
averageRating: Float
}
type Review {
id: ID!
body: String!
rating: Int!
}
The reviews subgraph's reference resolver receives only { id } from the router and hydrates its slice:
const resolvers = {
Product: {
__resolveReference(ref) {
// ref = { id } — resolve reviews for this product id only
return reviewsByProductId.load(ref.id); // batched — see resolver-performance skill
},
},
};
Note the reference resolver is a prime N+1 site: the router calls it once per entity in the result set. Batch it with a per-request loader — that's resolver-performance-and-n-plus-one's domain.
Anti-patterns
- Federating a single-team service — buying the gateway tier and composition gate to solve a problem you don't have.
- Boundaries drawn along the database instead of the domain, so two teams co-own one type and every change is a negotiation.
@requires chains that add hidden router hops nobody budgeted — federation latency death by a thousand cross-subgraph fetches.
- Reference resolvers that aren't batchable, turning each entity in a list into its own downstream call.
- Skipping supergraph composition in CI, so a locally-valid subgraph breaks the whole graph on deploy.
- Treating stitching and federation as interchangeable — stitching is the fallback for services you can't restructure, not the default.
See also
1---2name: graphql-federation-and-composition3description: "Decide whether to federate, keep a monolithic graph, or stitch — and if federating, design clean subgraph ownership boundaries with entity @key, reference resolvers, and @external/@requires/@provides, then weigh the real operational cost of running a gateway/router. Federation spec and directive specifics verify-at-use."4metadata:5 source: https://skillmd.com/skills/mcorbett51090/graphql-federation-and-composition6---78# GraphQL Federation & Composition910Federation lets many teams each own a subgraph and compose them into one supergraph the client sees as a single API. It is an org-boundary tool first and a technical one second — reach for it when *team ownership* is the problem, not when a single service simply got large.1112> **Engineering judgment.** GraphQL spec/library specifics change across versions — every version/library/spec-feature claim is `[verify-at-use]`. No PII.1314## Workflow15161. **Test whether you actually need federation.** One team, one deployable, one datastore → a **monolithic graph** is simpler and faster; don't pay the gateway tax for it. Multiple teams needing independent deploy cadence and clear ownership of parts of the graph → **federation**. An existing set of separate GraphQL services you can't modify → **schema stitching** as the pragmatic (older, more manual) fallback.172. **Draw ownership boundaries along the domain, not the data.** Each subgraph owns a coherent slice — `accounts`, `products`, `reviews` — and the types that belong to it. If two teams keep fighting over one type, the boundary is wrong. Ownership is the unit; the wire protocol serves it.183. **Define entities with `@key`.** An entity is a type that more than one subgraph contributes fields to, identified by a `@key`. The owning subgraph defines the canonical type; other subgraphs extend it, keyed on the same field(s).194. **Implement reference resolvers.** A subgraph that extends an entity must resolve it from just the `@key` fields the router passes it (`__resolveReference` / equivalent). Keep it a cheap, batchable lookup — the router will fan these out.205. **Use `@external`/`@requires`/`@provides` sparingly and deliberately.** `@external` marks a field defined elsewhere that this subgraph references; `@requires` pulls a field from another subgraph to compute a local one (adds a router hop — cost); `@provides` lets a subgraph return a field it can supply inline to *save* a hop. Each is a performance/coupling lever, not decoration.216. **Compose and validate the supergraph in CI.** Composition catches conflicts (a type owned by two subgraphs, a `@key` that doesn't resolve, an `@external` with no source) *before* deploy. A subgraph that composes locally but breaks the supergraph is a broken deploy — gate it. `[verify-at-use]` for the current composition tool/version.227. **Budget the operational cost honestly.** A federated gateway/router is a new tier: extra network hops per entity resolution, a single point of failure, distributed tracing across subgraphs, and schema-composition as a release gate. That cost is worth it for org scaling and worthless for a single team.2324## Decision table — federate vs monolith vs stitch2526| Situation | Choose | Why |27|---|---|---|28| One team, one deployable | **Monolithic graph** | No gateway tax; simplest ops |29| Many teams, independent deploy cadence, clear domain slices | **Federation** | Ownership boundaries + independent deploys |30| Pre-existing GraphQL services you can't restructure | **Schema stitching** | Compose without re-architecting the subgraphs |31| One large service that's just messy internally | **Refactor the monolith** | Federation won't fix an internal-modularity problem |3233## Federation directive cheat-sheet3435| Directive | Means | Cost / caution |36|---|---|---|37| `@key(fields: "id")` | This type is an entity, resolvable by these fields | Fields must be stably resolvable |38| `@external` | Field is owned by another subgraph, referenced here | Only meaningful with `@requires`/`@provides` |39| `@requires(fields: "…")` | Compute a local field using fields from elsewhere | Adds a router round-trip — real latency |40| `@provides(fields: "…")` | This subgraph can return these inline | Saves a hop *only if* the data is truly local |41| `@shareable` / value types | A field/type resolvable by more than one subgraph | Values must agree or composition fails |4243`[verify-at-use]` — directive names, syntax, and availability differ by federation version and library; confirm against the current spec before committing.4445## Subgraph & entity example4647Owning subgraph (`products`) defines the canonical entity:4849```graphql50# products subgraph51type Product @key(fields: "id") {52 id: ID!53 name: String!54 price: Money!55}56```5758Extending subgraph (`reviews`) contributes fields to the same entity, keyed identically:5960```graphql61# reviews subgraph62type Product @key(fields: "id") {63 id: ID!64 reviews: [Review!]! # this subgraph's contribution65 averageRating: Float66}6768type Review {69 id: ID!70 body: String!71 rating: Int!72}73```7475The `reviews` subgraph's reference resolver receives only `{ id }` from the router and hydrates its slice:7677```js78const resolvers = {79 Product: {80 __resolveReference(ref) {81 // ref = { id } — resolve reviews for this product id only82 return reviewsByProductId.load(ref.id); // batched — see resolver-performance skill83 },84 },85};86```8788Note the reference resolver is a prime N+1 site: the router calls it once per entity in the result set. Batch it with a per-request loader — that's `resolver-performance-and-n-plus-one`'s domain.8990## Anti-patterns9192- Federating a single-team service — buying the gateway tier and composition gate to solve a problem you don't have.93- Boundaries drawn along the database instead of the domain, so two teams co-own one type and every change is a negotiation.94- `@requires` chains that add hidden router hops nobody budgeted — federation latency death by a thousand cross-subgraph fetches.95- Reference resolvers that aren't batchable, turning each entity in a list into its own downstream call.96- Skipping supergraph composition in CI, so a locally-valid subgraph breaks the whole graph on deploy.97- Treating stitching and federation as interchangeable — stitching is the fallback for services you can't restructure, not the default.9899## See also100101- Traverse the **federate-vs-monolith-vs-stitch** and **subgraph-boundary** trees in [`../../knowledge/graphql-decision-trees.md`](../../knowledge/graphql-decision-trees.md).102- Dated federation-spec/router/directive specifics: [`../../knowledge/graphql-reference-2026.md`](../../knowledge/graphql-reference-2026.md).103- Sibling skills: [`../graphql-schema-design-and-evolution/SKILL.md`](../graphql-schema-design-and-evolution/SKILL.md), [`../resolver-performance-and-n-plus-one/SKILL.md`](../resolver-performance-and-n-plus-one/SKILL.md), [`../graphql-security-and-governance/SKILL.md`](../graphql-security-and-governance/SKILL.md).104- Sibling agents: `graphql-schema-architect` (owns subgraph boundaries), `graphql-server-engineer` (reference-resolver implementation + gateway ops), `graphql-security-governance-engineer` (auth across the supergraph).