What problem does GraphQL and schema federation solve in a microservices system?

Topic: microservices, Communication. Level: Intermediate.

Answer

GraphQL gives clients a single endpoint to request exactly the fields they need, eliminating over-/under-fetching across many services. **Schema federation** (e.g. Apollo Federation) lets each service own a slice of one unified graph: the `users` service defines the `User` type, the `orders` service extends `User` with an `orders` field. A **gateway** composes these subgraphs and resolves cross-service references, so clients see one schema while ownership stays decentralized. ```graphql # users subgraph type User @key(fields: "id") { id: ID! name: String! } # orders subgraph — extends a type it does not own extend type User @key(fields: "id") { id: ID! @external orders: [Order!]! # resolved by the orders service } type Order { id: ID! total: Float! } ``` The gateway fetches the user from one service and its orders from another, stitching them into a single response.