0
0
GraphqlConceptBeginner · 4 min read

When to Use Federation in GraphQL: Key Use Cases and Examples

Use GraphQL federation when you want to combine multiple GraphQL services into one unified API. It helps teams work independently on different parts of the schema while providing a single endpoint for clients.
⚙️

How It Works

GraphQL federation works like a team of specialists each managing their own part of a big project. Instead of one big GraphQL server, you have multiple smaller services called subgraphs. Each subgraph owns its data and schema part.

These subgraphs are combined by a gateway that acts like a smart coordinator. When a client sends a query, the gateway figures out which subgraphs to ask and merges their answers into one response. This way, teams can build and update their parts independently without breaking the whole API.

💻

Example

This example shows two simple subgraphs: one for users and one for products. The gateway combines them so clients can query both in one request.

graphql and javascript
### User Subgraph Schema
# user.graphql

type User @key(fields: "id") {
  id: ID!
  name: String
}

type Query {
  me: User
}

### Product Subgraph Schema
# product.graphql

type Product @key(fields: "upc") {
  upc: String!
  name: String
  price: Int
}

type Query {
  topProducts(first: Int = 5): [Product]
}

### Gateway Setup (simplified)
const { ApolloGateway } = require('@apollo/gateway');
const { ApolloServer } = require('apollo-server');

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'users', url: 'http://localhost:4001/graphql' },
    { name: 'products', url: 'http://localhost:4002/graphql' }
  ]
});

const server = new ApolloServer({
  gateway,
  subscriptions: false
});

server.listen().then(({ url }) => {
  console.log(`🚀 Gateway ready at ${url}`);
});
Output
🚀 Gateway ready at http://localhost:4000/
🎯

When to Use

Use federation when your app grows too big for one GraphQL server or when multiple teams manage different data domains. It fits well for microservices architectures where each service owns its data and schema.

Real-world cases include large e-commerce sites with separate teams for users, products, and orders, or companies merging multiple APIs into one unified GraphQL endpoint. Federation helps keep codebases clean and teams independent while giving clients a smooth experience.

Key Points

  • Federation splits a big schema into smaller subgraphs managed by different teams.
  • The gateway merges subgraphs into one API for clients.
  • It supports independent development and deployment of services.
  • Ideal for microservices and large-scale apps.
  • Clients get a single, unified GraphQL endpoint.

Key Takeaways

Use federation to combine multiple GraphQL services into one unified API.
It allows teams to own and develop their schema parts independently.
Federation is best for large apps or microservices needing a single GraphQL endpoint.
The gateway merges data from subgraphs transparently for clients.
Federation improves scalability and team collaboration.