diagram.mmd — sequence
GraphQL Query Execution sequence diagram

GraphQL query execution is the server-side process that takes an incoming query string, validates it against the schema, and resolves each requested field — potentially by calling multiple backing data sources — before assembling the final JSON response.

What the diagram shows

This sequence diagram follows a GraphQL request through five stages across four participants: Client, GraphQL Server, Resolver Layer, and Data Source (representing a database or external API).

1. Parse: the server tokenizes the raw query string into an Abstract Syntax Tree (AST). 2. Validate: the AST is checked against the schema. Field names, argument types, and nesting depth are all verified. If validation fails, the server returns an error immediately — no resolver is ever called. 3. Execute: the server walks the validated AST and dispatches each field to its registered resolver function. 4. Resolve: individual resolvers query databases, call microservices, or read from cache. Results bubble back up through the execution engine. 5. Respond: the server merges all resolver outputs into a single JSON object and returns it under the data key, with any partial errors in the errors array.

The diagram highlights the crucial validation short-circuit: a malformed or unauthorized query never reaches your data layer.

Why this matters

GraphQL's resolver-per-field execution model is powerful but non-obvious. Developers who treat it like a REST call often hit N+1 query problems because they don't realize each list item triggers its own resolver invocation. Visualizing the execution order makes the problem — and the DataLoader batching solution — immediately apparent.

For comparison, see the REST API Request Lifecycle to understand how a simpler handler-based approach differs. If GraphQL sits behind an API Gateway Request Flow, authentication can be offloaded before the query ever reaches the GraphQL layer. For data persistence details, see the database transaction lifecycle.

Free online editor
Edit this diagram in Graphlet
Fork, modify, and export to SVG or PNG. No sign-up required.
Open in Graphlet →

Frequently asked questions

GraphQL query execution is the server-side process that takes an incoming query string, validates it against the schema, and resolves each requested field — potentially by calling multiple backing data sources — before assembling the final JSON response. Unlike REST, where a single endpoint returns a fixed shape, GraphQL dispatches each field in the query to an individual resolver function.
The server parses the raw query string into an Abstract Syntax Tree (AST), then validates the AST against the schema — checking field names, argument types, and depth. If validation fails, an error is returned immediately and no resolver is called. On success, the execution engine walks the validated AST, dispatches each field to its resolver, waits for all resolvers to return, and merges the results into a single JSON object under the `data` key.
GraphQL is most valuable when clients have highly variable data requirements — for example, a mobile app that needs a compact subset of fields and a web app that needs rich nested data. It removes the need for multiple REST endpoints or request parameters to control field selection. It is also well-suited when different teams consume the same API and each team needs to fetch exactly the fields it owns without coordinating on a shared response shape.
The N+1 problem occurs when a list field resolver fetches N items and each item's child resolver triggers an individual database query — resulting in 1 query for the list plus N queries for the children. The standard solution is DataLoader, a batching utility that accumulates all child IDs within a single event loop tick and issues a single batched query instead of N individual queries, dramatically reducing database load for nested list queries.
mermaid
sequenceDiagram participant C as Client participant GQL as GraphQL Server participant R as Resolver Layer participant DS as Data Source C->>GQL: POST /graphql { query, variables } GQL->>GQL: Parse query string to AST alt Parse error GQL-->>C: 400 Syntax Error else Parse success GQL->>GQL: Validate AST against schema alt Validation error GQL-->>C: 400 Validation Error else Validation success GQL->>GQL: Build execution plan GQL->>R: Dispatch root field resolvers R->>DS: Query database / call service DS-->>R: Return raw data R->>R: Resolve nested fields R->>DS: Fetch related records DS-->>R: Return related data R-->>GQL: Resolver results map GQL->>GQL: Merge results into response shape GQL-->>C: 200 OK with data object and errors array end end
Copied to clipboard