Gateway composition in GraphQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using gateway composition in GraphQL, we combine data from multiple services into one response.
We want to understand how the time to get data grows as the number of services or data size increases.
Analyze the time complexity of the following GraphQL gateway composition query.
query GetUserAndOrders($userId: ID!) {
user(id: $userId) {
id
name
orders {
id
total
}
}
}
This query fetches a user and their orders by combining data from a user service and an order service.
Look for repeated data fetching or processing steps.
- Primary operation: Fetching each order for the user from the order service.
- How many times: Once per order linked to the user.
As the number of orders grows, the time to fetch all orders grows too.
| Input Size (n orders) | Approx. Operations |
|---|---|
| 10 | 10 fetches for orders + 1 fetch for user |
| 100 | 100 fetches for orders + 1 fetch for user |
| 1000 | 1000 fetches for orders + 1 fetch for user |
Pattern observation: The total work grows roughly in direct proportion to the number of orders.
Time Complexity: O(n)
This means the time to get the full response grows linearly with the number of orders.
[X] Wrong: "Fetching data from multiple services happens all at once, so time stays the same no matter how many orders there are."
[OK] Correct: Even if requests are parallel, each order still requires processing, so total work grows with the number of orders.
Understanding how gateway composition scales helps you design efficient APIs and explain performance trade-offs clearly.
What if the gateway batches order requests instead of fetching each order separately? How would the time complexity change?
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
