⬡ GraphQL Advanced

How do you implement entity resolution in Apollo Federation?

Answer

Entity resolution is how Federation resolves entities (types with @key) across subgraph boundaries. The process: (1) Service A returns a stub reference: { __typename: "User", id: "42" }. (2) The Router sends these stubs to Service B's _entities query: query(_representations: [{ __typename: "User", id: "42" }]) { _entities { ... on User { orders { id } } } }. (3) Service B's __resolveReference function fetches the full entity: User: { __resolveReference: (ref) => db.user.findById(ref.id) }. Complex keys: @key(fields: "region { name }") — nested key fields. Multiple keys: a type can have multiple @key directives. Stubbed entities: Service B doesn't need to implement all User fields — only those it extends. The Router's query planner optimizes entity fetching with batching.