Apollo Federation: GraphQL Supergraph Architecture Guide
Apollo Federation lets you split a large GraphQL schema across independent services (subgraphs) owned by separate teams, then compose them into one unified supergraph. Clients see a single API; your teams stay decoupled.
Subgraph Definition with @key
Each subgraph declares its entity types with @key. The Users subgraph owns the User entity; the Orders subgraph extends it with order data:
# users-subgraph schema.graphql
type User @key(fields: "id") {
id: ID!
name: String!
email: String!
}
type Query {
user(id: ID!): User
me: User
}
# orders-subgraph schema.graphql
# Reference User without redefining all its fields
type User @key(fields: "id") {
id: ID!
orders: [Order!]!
}
type Order {
id: ID!
total: Float!
status: OrderStatus!
createdAt: String!
}
enum OrderStatus { PENDING SHIPPED DELIVERED }
type Query {
order(id: ID!): Order
} Subgraph Server Setup (Node.js)
Use @apollo/subgraph to build each federation-aware subgraph and implement the __resolveReference function for entity resolution:
import { ApolloServer } from '@apollo/server';
import { buildSubgraphSchema } from '@apollo/subgraph';
import { gql } from 'graphql-tag';
const typeDefs = gql`
type User @key(fields: "id") {
id: ID!
name: String!
email: String!
}
type Query { user(id: ID!): User }
`;
const resolvers = {
User: {
// Called when another subgraph references this entity by key
__resolveReference(ref: { id: string }) {
return db.users.findById(ref.id);
},
},
Query: {
user: (_: unknown, { id }: { id: string }) => db.users.findById(id),
},
};
const server = new ApolloServer({
schema: buildSubgraphSchema({ typeDefs, resolvers }),
});
// Start on a unique port per subgraph, e.g. 4001 Composing with Rover CLI
Use rover supergraph compose to combine subgraph schemas locally, or publish to Apollo Studio for managed composition:
# supergraph.yaml โ compose config
federation_version: =2.4.0
subgraphs:
users:
routing_url: http://users-service:4001/graphql
schema:
subgraph_url: http://users-service:4001/graphql
orders:
routing_url: http://orders-service:4002/graphql
schema:
subgraph_url: http://orders-service:4002/graphql
# Compose locally and output the supergraph SDL
rover supergraph compose --config supergraph.yaml > supergraph.graphql
# Start the Apollo Router with the composed schema
./router --supergraph supergraph.graphql --config router.yaml
# Publish a subgraph schema to Apollo Studio (CI step)
rover subgraph publish my-graph@prod \
--name users \
--schema ./users-schema.graphql \
--routing-url https://users.prod.internal/graphql Federation vs Schema Stitching vs Monolith
| Criteria | Federation 2 | Schema Stitching | Monolith Schema |
|---|---|---|---|
| Team ownership | Per-subgraph | Partial | Shared codebase |
| Entity references | @key directive | Manual mergeTypes | N/A (co-located) |
| Schema checks | rover graph check | Manual | TypeScript types |
| Performance | Apollo Router (Rust) | Node.js gateway | Direct resolvers |
| Best for | Large orgs, microservices | Legacy migration | Small teams, startups |
Related Products
on GumroadAPI Testing Cheatsheet Pack
Quick-reference cheatsheets for REST, GraphQL, and WebSocket testing. Includes Postman collections, shell scripts for smoke tests and load tests, and JWT decoder.
Buy on Gumroad โDevPlaybook Interview Vault
210+ real technical interview questions with model answers, asked at Google, Meta, Amazon, Microsoft, Stripe, and Shopify. Covers Frontend (55), Backend (55), System Design (50), and DSA (50). Available in English and Traditional Chinese.
Buy on Gumroad โTypeScript Quick Reference Toolkit
5 practical reference files for TypeScript developers: cheatsheet, TS vs JS comparison, 20 utility types with real examples, tsconfig best practices template, and 15 common mistakes guide. Print it. Keep it on your desk.
Buy on Gumroad โYou Might Also Need
GraphQL Query Builder
Build GraphQL queries visually from your schema SDL. Select root fields, pick sub-fields, add arguments, and generate query/mutation/subscription strings instantly. Supports nested types.
Open free tool โ
OpenAPI Validator
Paste your OpenAPI 3.x spec (YAML or JSON) to validate structure, find missing required fields, detect duplicate paths, check for missing operationIds, and get errors and warnings with path references.
Open free tool โ
GraphQL Fragment Generator
Paste a GraphQL schema type definition and auto-generate reusable fragments. Supports nested types, interfaces, and unions with optional __typename and nested expansion.
Open free tool โ
More Free Tools
Related Articles
GraphQL Federation vs REST Microservices: Which Architecture in 2026?
Compare GraphQL Federation vs REST microservices โ Apollo Federation, schema stitching, performance, team scaling, migration paths, and when to choose each in 2026.
GraphQL vs REST vs gRPC: A 2026 API Design Comparison
A practical guide comparing GraphQL vs REST vs gRPC for modern API development. Covers performance, type safety, tooling, use cases, and a decision framework for 2026.
tRPC vs GraphQL: End-to-End Type Safety in 2026
A comprehensive comparison of tRPC and GraphQL in 2026 โ developer experience, type safety, performance, schema definition, client libraries, real-time, and when to choose each for your fullstack TypeScript application.
FastAPI vs Django REST Framework 2025: Which Should You Choose?
FastAPI vs Django REST Framework compared for 2025. Performance benchmarks, feature comparison, real-world use cases, and a clear decision guide for your next Python API project.
Microservices vs Monolith 2026: Real-World Decision Framework
Stop following hype โ choose the right architecture for your specific context. Practical comparison of microservices vs monolith in 2026, with a decision framework based on team size, scale, and stage.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- โก AI Code Review, Doc Generator & SQL Builder
- โก All premium templates & early access
- โก Member discounts on Gumroad products
$9/mo after trial ยท Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products โ grab them all at once and save big.
MIT licensed ยท Instant download ยท No subscription
See what's included โ Browse all deals โFrequently Asked Questions
What is Apollo Federation?▼
Apollo Federation is an architecture for composing multiple GraphQL services (subgraphs) into a single unified API (supergraph). Instead of one monolithic GraphQL schema, each team owns their own subgraph. The Apollo Router or Apollo Gateway combines them into one schema that clients query as if it were a single service.
What is the difference between Federation 1 and Federation 2?▼
Federation 2 (Apollo Federation v2) introduced shareable types, removing the restriction that only one subgraph can define a given type. It replaced the @external directive with a simpler @shareable model, improved entity resolution with @interfaceObject, and introduced the rover CLI for schema composition and Rover Studio for visualization. Federation 1 is still supported but new projects should use Federation 2.
What is a subgraph @key directive?▼
The @key directive marks which field(s) uniquely identify an entity type in a subgraph. When other subgraphs reference the same entity, they use @key to declare how to resolve a reference to that entity. For example, @key(fields: 'id') on a User type means any subgraph can provide more fields for User as long as they can resolve it by id.
Can subgraphs be written in different languages?▼
Yes. Apollo Federation is language-agnostic at the protocol level. Subgraphs communicate via standard GraphQL and a federation specification extension. Libraries exist for Node.js, Java (DGS), Python (strawberry-graphql), Go, Rust, and more. Each subgraph just needs to implement the _entities query and the federation SDL directives.
What is the Rover CLI used for?▼
Rover is Apollo's CLI tool for managing supergraph schemas. You use rover subgraph publish to push a subgraph's schema to Apollo Studio, rover supergraph compose to compose a supergraph schema locally for development, and rover graph check to run schema checks against your registered schema before deploying.
What is schema stitching and why choose federation over it?▼
Schema stitching is an older approach to combining GraphQL schemas where a gateway manually merges type definitions and delegates resolvers. Federation improves on this by making subgraphs self-describing โ each subgraph declares its own entity keys and relationships without the gateway needing custom configuration per type. Federation is more maintainable at scale and is the recommended approach for new projects.