Gateway composition helps combine data from different sources into one simple API. It makes it easy to get all needed info in one place.
Gateway composition in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
GraphQL
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 });
The serviceList contains all GraphQL services to combine.
Subscriptions are usually disabled in gateway setup.
Examples
GraphQL
const gateway = new ApolloGateway({ serviceList: [ { name: 'accounts', url: 'http://localhost:4001/graphql' }, { name: 'reviews', url: 'http://localhost:4002/graphql' } ] });
GraphQL
const server = new ApolloServer({ gateway, subscriptions: false });
Sample Program
This program creates a gateway combining two services: users and products. It starts the server and logs the URL.
GraphQL
import { ApolloServer } from 'apollo-server'; import { ApolloGateway } from '@apollo/gateway'; 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}`); });
Important Notes
Each service must have a GraphQL schema that supports federation.
Gateway merges schemas and resolves queries across services automatically.
Make sure services are running before starting the gateway.
Summary
Gateway composition combines multiple GraphQL services into one API.
It simplifies client requests by providing a single endpoint.
Use ApolloGateway with a list of services and disable subscriptions.
Practice
1. What is the main purpose of gateway composition in GraphQL?
easy
Solution
Step 1: Understand gateway composition
Gateway composition merges several GraphQL services into one unified API.Step 2: Identify the main benefit
This allows clients to send requests to a single endpoint instead of multiple services.Final Answer:
To combine multiple GraphQL services into a single API endpoint -> Option AQuick Check:
Gateway composition = single API endpoint [OK]
Hint: Gateway composition means one API endpoint for many services [OK]
Common Mistakes:
- Thinking it creates multiple endpoints
- Confusing with REST APIs
- Believing it disables client requests
2. Which of the following is the correct way to initialize ApolloGateway with two services named 'users' and 'products'?
easy
Solution
Step 1: Recall ApolloGateway initialization syntax
ApolloGateway expects an object with a 'services' array containing objects with 'name' and 'url'.Step 2: Match the correct syntax
new ApolloGateway({ services: [{ name: 'users', url: 'http://users' }, { name: 'products', url: 'http://products' }] }) correctly uses new ApolloGateway with services array of objects including name and url.Final Answer:
new ApolloGateway({ services: [{ name: 'users', url: 'http://users' }, { name: 'products', url: 'http://products' }] }) -> Option CQuick Check:
ApolloGateway needs services array with name and url [OK]
Hint: Use services array with name and url objects in ApolloGateway [OK]
Common Mistakes:
- Passing services as simple string array
- Using 'endpoints' instead of 'services'
- Omitting 'new' keyword
3. Given this ApolloGateway setup:
const gateway = new ApolloGateway({
services: [
{ name: 'users', url: 'http://localhost:4001/graphql' },
{ name: 'products', url: 'http://localhost:4002/graphql' }
],
__exposeQueryPlanExperimental: false
});
What will be the result if a client queries for a product's name and its owner's username?medium
Solution
Step 1: Understand gateway data fetching
The gateway composes schemas and fetches data from all relevant services for the query.Step 2: Analyze the query involving product and user data
Since the query asks for product and owner username, gateway calls both 'products' and 'users' services.Final Answer:
The gateway fetches data from both 'products' and 'users' services and returns combined result -> Option DQuick Check:
Gateway combines data from multiple services [OK]
Hint: Gateway merges data from all services needed by query [OK]
Common Mistakes:
- Assuming gateway fetches from only one service
- Confusing __exposeQueryPlanExperimental with data fetching
- Assuming error without service failure
4. You wrote this ApolloGateway code but get an error:
const gateway = new ApolloGateway({
services: [
{ name: 'users', url: 'http://localhost:4001/graphql' },
{ name: 'products' }
]
});
What is the likely cause of the error?medium
Solution
Step 1: Check service definitions
Each service object must have both 'name' and 'url' properties.Step 2: Identify missing property
The 'products' service lacks the 'url' property, causing the error.Final Answer:
Missing 'url' property for the 'products' service -> Option AQuick Check:
Each service needs name and url [OK]
Hint: Always include 'url' for each service in ApolloGateway [OK]
Common Mistakes:
- Confusing 'services' with 'endpoints'
- Thinking ApolloGateway supports single service only
- Using wrong property names
5. You want to disable subscriptions in your ApolloGateway setup while composing three services. Which is the correct way to do this?
hard
Solution
Step 1: Understand where to disable subscriptions
Subscriptions are disabled in ApolloServer, not ApolloGateway.Step 2: Apply correct configuration
Pass { subscriptions: false } as an option to ApolloServer constructor wrapping the gateway.Final Answer:
Pass { subscriptions: false } in ApolloServer constructor, not in ApolloGateway -> Option BQuick Check:
Disable subscriptions in ApolloServer, not ApolloGateway [OK]
Hint: Disable subscriptions in ApolloServer, not ApolloGateway [OK]
Common Mistakes:
- Trying to disable subscriptions inside ApolloGateway
- Removing schema fields instead of config
- Setting subscriptions false per service
