0
0
GraphQLquery~10 mins

Gateway composition in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Gateway composition
Client sends query
Gateway receives query
Gateway splits query into subqueries
Send subqueries to respective services
Services process subqueries
Services return partial results
Gateway merges partial results
Gateway returns combined result to client
The gateway receives a client query, splits it into parts for different services, collects their responses, merges them, and sends back a single combined result.
Execution Sample
GraphQL
query {
  user(id: "1") {
    name
    posts {
      title
    }
  }
}
A client requests a user’s name and their posts’ titles; the gateway splits this to user and post services and combines the results.
Execution Table
StepActionQuery/ResponseServiceResult
1Receive full query{ user(id: "1") { name posts { title } } }GatewayFull query received
2Split query{ user(id: "1") { name } }GatewayUser subquery created
3Split query{ posts(userId: "1") { title } }GatewayPost subquery created
4Send subquery{ user(id: "1") { name } }User ServiceRequest sent
5Send subquery{ posts(userId: "1") { title } }Post ServiceRequest sent
6Receive response{ name: "Alice" }User ServiceUser data received
7Receive response{ posts: [{ title: "Hello World" }, { title: "GraphQL Rocks" }] }Post ServicePost data received
8Merge responses{ user: { name: "Alice", posts: [{ title: "Hello World" }, { title: "GraphQL Rocks" }] } }GatewayCombined result created
9Return combined result{ user: { name: "Alice", posts: [{ title: "Hello World" }, { title: "GraphQL Rocks" }] } }GatewayResult sent to client
10EndQuery completed
💡 All subqueries processed and merged; final combined result returned to client.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 7Final
fullQuerynull{ user(id: "1") { name posts { title } } }{ user(id: "1") { name posts { title } } }{ user(id: "1") { name posts { title } } }{ user(id: "1") { name posts { title } } }null
userSubquerynull{ user(id: "1") { name } }{ user(id: "1") { name } }{ name: "Alice" }{ name: "Alice" }{ name: "Alice" }
postSubquerynullnull{ posts(userId: "1") { title } }null{ posts: [{ title: "Hello World" }, { title: "GraphQL Rocks" }] }{ posts: [{ title: "Hello World" }, { title: "GraphQL Rocks" }] }
combinedResultnullnullnullnullnull{ user: { name: "Alice", posts: [{ title: "Hello World" }, { title: "GraphQL Rocks" }] } }
Key Moments - 3 Insights
Why does the gateway split the query into subqueries?
Because each service owns part of the data, the gateway splits the query so each service can handle its part independently, as shown in steps 2 and 3 of the execution_table.
How does the gateway combine results from different services?
After receiving partial results from services (steps 6 and 7), the gateway merges them into one combined response (step 8), so the client gets a single unified answer.
What happens if one service is slow or fails?
The gateway waits for all services to respond before merging. If a service is slow or fails, the gateway may delay or return an error. This is why step 9 only happens after all responses are received.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what query does the gateway send to the Post Service?
A{ user(id: "1") { name posts { title } } }
B{ posts(userId: "1") { title } }
C{ user(id: "1") { name } }
D{ posts { title } }
💡 Hint
Check step 3 and 5 in the execution_table where the post subquery is created and sent.
At which step does the gateway merge the partial results?
AStep 6
BStep 7
CStep 8
DStep 9
💡 Hint
Look for the step labeled 'Merge responses' in the execution_table.
If the user service returned no name, how would the combined result change?
AThe combined result would have user name as null or missing
BThe combined result would not include posts
CThe gateway would skip merging
DThe gateway would send an error to the client immediately
💡 Hint
Refer to variable_tracker's combinedResult and how partial results affect it.
Concept Snapshot
Gateway Composition in GraphQL:
- Gateway receives full client query
- Splits query into subqueries per service
- Sends subqueries to respective services
- Receives partial results
- Merges results into one response
- Returns combined data to client
Full Transcript
Gateway composition in GraphQL works by the gateway receiving a full client query. It then splits this query into smaller subqueries, each targeting a specific service responsible for part of the data. The gateway sends these subqueries to the respective services. Each service processes its subquery and returns partial results. The gateway waits for all responses, merges them into a single combined result, and sends this unified response back to the client. This process allows multiple services to work together seamlessly to fulfill a single client request.