Senior LevelREST APIs & Web Services
How does Apollo Federation v2 compose multiple microservice GraphQL subgraphs into a unified supergraph?
❓ Interview Prompt: Explain Apollo Federation v2, Subgraphs, Supergraph schema composition, `@key` directive, `@shareable`, and query planning by Federation Router.
Apollo Federation v2 composes independent GraphQL microservices (Subgraphs) into a single unified API endpoint (Supergraph):
Core Directives & Routing:
1
'@key(fields: "id")': Marks an entity (e.g. 'User') that can be extended by other subgraphs.
2
Federation Router: Intercepts client GraphQL queries, analyzes entity keys, and automatically generates an optimized multi-service query execution plan, merging sub-responses seamlessly!
Code Implementation Example
yaml
# User Subgraph Schema (Users Microservice)
type User @key(fields: "id") {
id: ID!
name: String!
}
# Reviews Subgraph Schema (Reviews Microservice)
type User @key(fields: "id", resolvable: false) {
id: ID!
}
extend type User {
reviews: [Review!]!
}💡Apollo Federation v2 entity key sharing across subgraphs.
🎯 Interview Key Takeaway
Apollo Federation v2 composes modular GraphQL subgraphs into a unified supergraph using entity key directives.
#Apollo Federation#GraphQL Subgraphs#Architecture#Senior
Back to all REST APIs & Web Services Questions→