0
0
GraphQLquery~30 mins

Why federation scales GraphQL - See It in Action

Choose your learning style9 modes available
Building a Scalable GraphQL API with Federation
📖 Scenario: You are working on a growing e-commerce platform. As the platform expands, different teams manage different parts of the data: products, users, and orders. To keep the API fast and scalable, you want to use GraphQL federation to combine these separate services into one unified API.
🎯 Goal: Build a simple federated GraphQL setup with three services: products, users, and orders. Each service defines its own schema and shares key fields to enable federation. Then, create a gateway schema that combines them into one API.
📋 What You'll Learn
Create a GraphQL schema for the products service with a Product type and a products query.
Create a GraphQL schema for the users service with a User type and a users query.
Create a GraphQL schema for the orders service with an Order type and an orders query.
Add federation directives to share keys between services.
Create a gateway schema that composes the three services into one federated API.
💡 Why This Matters
🌍 Real World
Large companies use GraphQL federation to split their API into smaller, manageable services owned by different teams. This helps scale development and maintain performance.
💼 Career
Understanding GraphQL federation is valuable for backend developers and API architects working on scalable, modular APIs in modern web applications.
Progress0 / 4 steps
1
Create the Products Service Schema
Create a GraphQL schema for the products service. Define a Product type with fields id (ID!), name (String!), and price (Float!). Add a query products that returns a list of Product. Use the federation directive @key(fields: "id") on the Product type.
GraphQL
Need a hint?

Use @key(fields: "id") on the Product type to enable federation. Define the products query to return a list of products.

2
Create the Users Service Schema
Create a GraphQL schema for the users service. Define a User type with fields id (ID!), username (String!), and email (String!). Add a query users that returns a list of User. Use the federation directive @key(fields: "id") on the User type.
GraphQL
Need a hint?

Use @key(fields: "id") on the User type to enable federation. Define the users query to return a list of users.

3
Create the Orders Service Schema
Create a GraphQL schema for the orders service. Define an Order type with fields id (ID!), productId (ID!), userId (ID!), and quantity (Int!). Add a query orders that returns a list of Order. Use the federation directive @key(fields: "id") on the Order type.
GraphQL
Need a hint?

Use @key(fields: "id") on the Order type to enable federation. Define the orders query to return a list of orders.

4
Create the Gateway Schema to Compose Services
Create a gateway schema that composes the products, users, and orders services into one federated API. Use the Apollo Federation @apollo/gateway or similar tool to combine the schemas. The gateway should be able to resolve queries across services seamlessly.
GraphQL
Need a hint?

Use Apollo Gateway's serviceList to list the three services by name and URL. This composes the federated schema.