What if you could get all your app's data with just one simple question, no matter how many teams built it?
Why Gateway composition in GraphQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have many different teams building separate parts of a big app, each with its own data and services. You try to ask each team separately for information and then combine it yourself.
This manual way is slow and confusing. You have to remember many places to ask, write lots of code to join data, and if one team changes their data, your code breaks. It's like juggling many balls and dropping some.
Gateway composition lets you create one smart gateway that talks to all teams' services behind the scenes. You ask the gateway once, and it gathers and combines data for you automatically, making your life much easier.
fetchUser(); fetchOrders(); combineDataManually();
query { user { name orders { id } } }It enables a smooth, single point of access to complex, distributed data, so you get exactly what you need with one simple request.
A shopping app where user info, product details, and order history come from different teams but appear together instantly when you open your profile.
Manual data gathering from many sources is slow and error-prone.
Gateway composition creates one unified access point.
This simplifies data fetching and keeps apps fast and reliable.
Practice
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]
- Thinking it creates multiple endpoints
- Confusing with REST APIs
- Believing it disables client requests
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]
- Passing services as simple string array
- Using 'endpoints' instead of 'services'
- Omitting 'new' keyword
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?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]
- Assuming gateway fetches from only one service
- Confusing __exposeQueryPlanExperimental with data fetching
- Assuming error without service failure
const gateway = new ApolloGateway({
services: [
{ name: 'users', url: 'http://localhost:4001/graphql' },
{ name: 'products' }
]
});
What is the likely cause of the error?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]
- Confusing 'services' with 'endpoints'
- Thinking ApolloGateway supports single service only
- Using wrong property names
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]
- Trying to disable subscriptions inside ApolloGateway
- Removing schema fields instead of config
- Setting subscriptions false per service
