What is schema stitching and federation in GraphQL?
Understand schema stitching and federation in GraphQL: how they compose multiple services into one graph, the role of @key and gateways, and when to use each.
Expected Interview Answer
Schema stitching and federation are two approaches to composing multiple GraphQL services into a single unified graph, so clients query one endpoint while the work is distributed across independent backend services.
Schema stitching is a gateway-centric technique where a central server merges remote schemas and manually declares how types link together, keeping the composition logic in the gateway. Federation, popularized by Apollo Federation, inverts control: each subgraph annotates its own types with directives like @key and @external, and a gateway or router composes them automatically from the subgraphs' metadata. Federation scales better organizationally because ownership of type extensions lives with each team rather than in one bottleneck gateway.
- Presents many services as one unified graph to clients
- Lets teams own and deploy their subgraphs independently
- Federation decentralizes composition via @key and @external
- Stitching offers flexible manual control at the gateway
- Avoids a single monolithic schema and repo
- Enables cross-service references resolved transparently
AI Mentor Explanation
Federation is like a national team assembled from separate state squads, where each state develops its own players and declares who is available by a jersey number, and the selectors compose the final eleven from those declarations. Stitching is more like one head coach personally hand-picking and wiring every player together, holding all the roster logic in a single office.
Step-by-Step Explanation
Step 1
Identify subgraphs
Split your domain into independent GraphQL services, each owning part of the schema.
Step 2
Declare entity keys
In federation, annotate shared types with @key(fields:) so they can be referenced across services.
Step 3
Extend types across services
One subgraph can extend another's type, marking borrowed fields @external and adding its own.
Step 4
Compose the supergraph
A router or gateway composes subgraph SDL into one supergraph clients can query.
Step 5
Resolve references
The gateway resolves cross-service references via reference resolvers using the entity keys.
What Interviewer Expects
- Clear distinction between stitching and federation
- Knows federation decentralizes composition to subgraphs
- Understands @key and @external directives
- Mentions the gateway or router role
- Aware federation scales better for large orgs
Common Mistakes
- Treating stitching and federation as identical
- Thinking clients must query each service separately
- Ignoring how cross-service entity references are resolved
- Believing federation removes the need for a gateway or router
- Assuming stitching is always deprecated or obsolete
Best Answer (HR Friendly)
“Both are ways to combine several GraphQL services into one API that clients see as a single graph. Schema stitching wires them together at a central gateway, while federation lets each team describe its own piece and have them composed automatically, which scales better for large organizations.”
Code Example
type User @key(fields: "id") {
id: ID!
name: String!
}type Review {
id: ID!
body: String!
author: User!
}
extend type User @key(fields: "id") {
id: ID! @external
reviews: [Review!]!
}const resolvers = {
User: {
__resolveReference(user) {
return { id: user.id, reviews: getReviewsByUser(user.id) };
}
}
};Follow-up Questions
- What does the @key directive do in Apollo Federation?
- How does a reference resolver (__resolveReference) work?
- When would you still choose schema stitching over federation?
- What is the difference between a gateway and a router in federation?
MCQ Practice
1. In Apollo Federation, what does @key(fields: "id") declare?
@key specifies the fields that uniquely identify an entity so it can be referenced and resolved across subgraphs.
2. How does federation differ from schema stitching?
Federation pushes composition metadata into each subgraph, while stitching keeps merging logic in the central gateway.
3. What does marking a field @external in a subgraph indicate?
@external marks a field defined in another subgraph that this subgraph references rather than owns.
Flash Cards
What problem do stitching and federation solve? — Composing multiple GraphQL services into one unified graph for clients.
Key difference: stitching vs federation? — Stitching composes at the gateway; federation decentralizes composition to subgraphs.
What does @key do? — Declares the fields that uniquely identify an entity across subgraphs.
What resolves cross-service entity references? — The __resolveReference reference resolver in the owning subgraph.