Course contentsShow
← Software Engineering
Lesson 820 of 1,93325. RPC and GraphQL API DesignFree lesson

Schema Stitching and Federation

Composing multiple GraphQL schemas into a unified API across distributed services.

Schema Stitching and Federation

What you'll learn: How to combine multiple GraphQL schemas from different services into one unified API gateway that clients can query.

The Challenge of Distributed GraphQL

As your application grows, you might have separate GraphQL services for users, products, orders, and reviews—each owned by different teams. Without composition, clients would need to query four different endpoints. Schema stitching and federation solve this by creating a single, cohesive GraphQL API that routes requests to the appropriate backend services.

Schema Stitching

Schema stitching merges multiple GraphQL schemas at the gateway level. Think of it like stapling separate documents together—you're physically combining schemas into one. The gateway knows which service owns which types and forwards parts of queries accordingly.

For example, if your User type lives in the users service and Order type in the orders service, stitching lets clients write one query that spans both, and the gateway splits and routes it behind the scenes.

The challenge: manually defining how types from different services relate and ensuring resolvers know where to fetch data.

Schema Federation

Federation (popularized by Apollo) takes a more sophisticated approach. Each service defines its own schema and declares which types it extends or references. Services annotate their schemas to indicate relationships:

  • The users service owns User
  • The orders service can extend User with an orders field

The gateway automatically understands these relationships through special directives and metadata. Services remain independent but can reference each other's types without tight coupling.

Federation is like a postal system where each office knows how to route packages based on addresses, rather than manually directing every shipment.

When to Use What

Use stitching for simpler scenarios or legacy systems. Use federation when you have multiple teams, need better autonomy, or want services to evolve independently while maintaining type safety.

Key Takeaway: Schema stitching and federation enable you to compose multiple GraphQL services into a unified API, with federation offering better scalability and team independence through declarative schema extensions.