0
0
GraphQLquery~30 mins

Apollo Federation concepts in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Building a Simple Apollo Federation Setup
📖 Scenario: You are working on a project where multiple teams manage different parts of a product catalog. Each team has its own GraphQL service. You want to combine these services into one unified graph so clients can query all product information easily.
🎯 Goal: Build a basic Apollo Federation setup with two services: products and reviews. Each service defines its own GraphQL schema. Then create a gateway schema that composes these services into one federated graph.
📋 What You'll Learn
Create a products service schema with a Product type having id and name fields
Create a reviews service schema with a Review type and extend the Product type to add reviews field
Use Apollo Federation directives like @key and @extends correctly
Create a gateway schema that composes the two services into one federated graph
💡 Why This Matters
🌍 Real World
Large companies often split their GraphQL APIs into multiple services owned by different teams. Apollo Federation helps combine these into one seamless API for clients.
💼 Career
Understanding Apollo Federation is valuable for backend developers working with GraphQL in microservice architectures.
Progress0 / 4 steps
1
Create the Products Service Schema
Create a GraphQL schema string called productsTypeDefs that defines a Product type with fields id (ID!) and name (String!). Add the Apollo Federation directive @key(fields: "id") to the Product type.
GraphQL
Need a hint?

Use backticks to create a multi-line string. The @key directive marks the primary key field for federation.

2
Create the Reviews Service Schema
Create a GraphQL schema string called reviewsTypeDefs that defines a Review type with fields id (ID!) and content (String!). Extend the Product type with @extends and @key(fields: "id") directives, adding a reviews field that returns a list of Review.
GraphQL
Need a hint?

Use extend type to add fields to Product in the reviews service. Mark id as @external because it is owned by the products service.

3
Create the Apollo Gateway Setup
Create a variable called gateway that initializes an ApolloGateway instance with a serviceList containing two services: one named products with URL http://localhost:4001/graphql and one named reviews with URL http://localhost:4002/graphql.
GraphQL
Need a hint?

Use new ApolloGateway({ serviceList: [...] }) to configure the gateway with the two services.

4
Complete the Apollo Server Setup with Gateway
Create a variable called server that initializes an ApolloServer instance using the gateway variable. Set subscriptions to false in the server options.
GraphQL
Need a hint?

Pass the gateway variable to the ApolloServer constructor and disable subscriptions.