How it works
The explanation, with examples you can run.
Entities
The glue is the entity: an object type with a key that any subgraph can use to refer to it. In Apollo Federation's syntax:
# Users subgraph: owns Usertype User @key(fields: "id") { id: ID! name: String!}type Query { user(id: ID!): User }# Orders subgraph: owns Order, and adds a field to Usertype User @key(fields: "id") { id: ID! orders(first: Int = 10): [Order!]!}type Order @key(fields: "id") { id: ID! total: Int! product: Product!}type Product @key(fields: "id") { id: ID! } # a reference: Products owns the restThe client sees one User type with name and orders, and one Order whose product has every field the Products subgraph defines.
What the router does
query { user(id: "u1") { name orders { total product { title } } } }- Ask Users for
user(id: "u1") { id name }. - Send Orders the entity key
{ __typename: "User", id: "u1" }and ask fororders { total product { id } }. - Send Products all the product keys from step 2 in one request and ask for
title. - Merge the three results into the shape of the original query.
This plan is the execution model from earlier in this path, applied across services: parents before children, siblings in parallel, and entity lookups batched per step so ten orders don't mean ten calls to Products. Inside each subgraph, a DataLoader still turns those keys into one database query.
Costs and trade-offs
- Latency adds up along the plan's longest chain of dependent fetches. Deeply interleaved ownership (A needs B needs A) makes slow plans.
- The router is critical infrastructure: authentication, cost limits, timeouts and tracing usually live there, as in any API gateway.
- Composition is a contract check. Run it in CI for every subgraph change, together with checks of the operations clients actually send, so a removed field is caught before deploy.
- You may not need it. A single GraphQL server in front of several REST or gRPC services (a backend for frontend) is simpler until several teams need to evolve the schema independently.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.