The execution model
What happens between a POST to /graphql and the JSON that comes back: parse, validate, coerce variables, then walk the selection set. Sibling fields run together, top-level mutation fields run one at a time, and both stacks' source-call logs show it.
Learning objectives
By the end of this chapter you can:
- Walk a GraphQL request through its four stages: parse, validate, coerce variables, execute.
- Explain why sibling fields may run concurrently while top-level
Mutationfields execute serially. - Read a source-call log to verify, rather than assume, resolver ordering and overlap.
- Compare blocking and asynchronous resolver styles (a plain return value vs.
Uni, a blockingdefvs.async def) and their effect on thread and event-loop occupancy. - Map the GraphQL-over-HTTP status-code recommendations to what SmallRye and Strawberry return in practice.
Why this matters
A GraphQL server doesn’t run a query the way a database runs SQL. It parses text, checks it against the schema, converts its inputs, then calls ordinary functions, one per field, in an order the specification mostly leaves to the engine. Knowing that order tells you which calls a selection costs, which of them wait for each other, and what a failed request leaves behind.
Five sources (booking, routing, voyage, location, delivery) stand behind the schema. Each call takes 250 ms and is written to a log served at /debug/source-calls, so order and overlap are read off the log instead of assumed. The log is a lab tool and is off unless TRACKING_DEBUG_ENDPOINTS=true.
A request is a document plus inputs
Over HTTP a request is a JSON object with query, the document text; variables, a JSON object of inputs; and operationName, which says which operation to run. A document can hold several operations and any number of fragments, and the server runs exactly one. The GetOperation step of “Executing Requests” in the GraphQL specification picks the named one, or the only one when there’s a single operation and no name. With several operations and no name, the specification says to raise a request error.
The stacks diverge here. Given query A { cargo(trackingId: "ABC123") { trackingId } } query B { ... } with no operationName, SmallRye rejects the request with Must provide operation name if query contains multiple operations., no data and no source call. Strawberry runs the first operation and answers ABC123. Both answer XYZ789 when operationName is B, and only booking.getCargo:XYZ789 appears in the log. Send operationName whenever a document holds more than one operation. It also names each request in logs and traces.
A request error is the specification’s term for a failure before execution starts, and the current edition calls a failure inside a resolver an execution error (older editions said “field error”). Three stages produce request errors.
Parse
Parsing turns the text into a document: a tree of operations, selection sets, fields, arguments and fragments. It checks syntax only. An unclosed brace, a missing name or a stray character stops the request.
{ cargo(trackingId: "ABC123") { trackingId }
Quarkus answers Unparseable input document and omits data. Python answers Syntax Error: Expected Name, found <EOF>. with "data":null. The specification says data should be absent for a request error, but Strawberry sends the key with a null value, so a client should test for absent or null. Both answer HTTP 200 and leave the log empty, because no resolver was reached.
Validate
Validation checks the document against the schema without touching data. The rules in the specification’s “Validation” section include: every field exists on the type it is selected from, every object-typed field has a selection set and every leaf has none, arguments match their declared types, fragments are defined, used and acyclic, and variables are declared, used and compatible with their positions.
{ cargo(trackingId: "ABC123") { ...A } }
fragment A on Cargo { ...B }
fragment B on Cargo { ...A }
A fragment cycle would make execution unbounded, so validation rejects it. Quarkus reports Validation error (FragmentCycle@[A]) : Fragment cycles not allowed for each fragment in the cycle. Python reports Cannot spread fragment 'A' within itself via 'B'. An unknown field such as cargo { weight } fails the same way, and the log stays empty. Validation needs only the document and the schema, so a client can fix the document and resend it with no risk of having changed anything. It costs CPU on every request, which is where persisted queries and query-cost limits attach.
Coerce variables
Variables arrive as JSON, which has fewer types than GraphQL. Coercion converts each variable to its declared type: a JSON string becomes a TrackingId through the scalar’s input coercion, a missing optional variable takes its default, and a JSON 123 offered to a string scalar is an error. The engine won’t call a resolver with an argument it can’t trust, so this runs before execution.
query($t: TrackingId!) { cargo(trackingId: $t) { trackingId } }
With {"t":123}, Quarkus answers Variable 't' has an invalid value: SRGQL000013: Expected type [String or class extending class com.patterncatalyst.shipping.tracking.TrackingId] but was [BigDecimal]. and Python answers Variable '$t' has invalid value: Expected value of type 'TrackingId', but encountered error 'expected a string, got int'; found: 123. Both fail before any source call. The same mistake as a literal, location(unLocode: 123), is rejected in validation, so a literal fails in the second stage and a variable in the third. A variable error names the variable, and the offending value is in the request body, not the document.
Execute: walking the selection set
Execution starts with the root selection set and the root value. The specification’s algorithms, from “Executing Operations” to “Executing Fields”, reduce to three steps.
- Collect fields. “Field Collection” groups the selection set by response key. Fields with the same key merge, fragments expand when their type condition applies, and
@skipand@includedrop what they exclude. This happens before any resolver runs, so a pruned field never costs a call. - Resolve each field. The resolver receives the parent value, the field’s arguments and the request context, and returns the value or something that becomes it.
- Complete the value. “Value Completion” checks the result against the field’s type. A leaf is serialized, a list is completed item by item, an object is walked with the sub-selection as its selection set and the value as its parent, and a null in a non-null position is an error that null propagation carries upward.
Take cargo { itinerary { legs { loadLocation { unLocode } } } delivery { transportStatus } }.
Query.cargo calls booking.getCargo and returns a Cargo, the parent of itinerary and delivery. Neither takes the tracking id as an argument: each reads it from the parent. itinerary returns an Itinerary whose legs list needs no call of its own, because routing returned the legs with the itinerary. loadLocation turns each leg’s UN/LOCODE into a Location. A selection such as cargo { trackingId description } makes exactly one call, booking.getCargo:ABC123, because description is a property of the booking record and has no resolver.
@Description("The planned route. Null until the routing service has assigned one.")
public Uni<Itinerary> itinerary(@Source Cargo cargo, Context context) {
return routing.getItinerary(context.getExecutionId(), cargo.trackingId().value());
}
@NonNull
@Description("Where the cargo is loaded.")
public Uni<Location> loadLocation(@Source Leg leg, Context context) {
return locations.getLocation(context.getExecutionId(), leg.loadUnLocode());
}
@strawberry.field(description="Where the cargo is loaded.")
async def load_location(self, info: Info[TrackingContext]) -> Location:
return await info.context.location.get_location(self.load_un_locode)
In SmallRye a @Source parameter is the parent value, and the method lives in the @GraphQLApi class beside the root fields. A Uni is Mutiny’s lazy single-value type: the method returns a description of the work and the engine subscribes to it. The record components Leg uses for the codes are marked @Ignore, which keeps them out of the schema. In Strawberry the parent is self, strawberry.Private keeps a field out of the schema, and info.context is whatever the router’s context_getter returned.
One request id across every call
The log has to say which request a call belongs to, and each stack gets the id from a different place.
On Quarkus the id is Context#getExecutionId(), the SmallRye Context passed to the resolver. Read it at method entry, while the resolver still runs for its request, and pass it to the source as an argument. The continuation of a delayed Uni runs later on a different thread, where request-scoped state isn’t guaranteed to be the same request’s. On Python a uuid4 is created once per request in the router’s context getter:
def get_context(calls: Annotated[SourceCalls, Depends(get_calls)]) -> TrackingContext:
request_id = str(uuid.uuid4())
return TrackingContext(
request_id,
BookingSource(calls, request_id),
RoutingSource(calls, request_id),
VoyageSource(calls, request_id),
LocationSource(calls, request_id),
DeliverySource(calls, request_id),
)
Every resolver of the request sees the same info.context, so all five sources are bound to one id.
Sibling fields may run in parallel
“Normal and Serial Execution” in the specification says the executor can run the entries of a collected fields map “in whatever order it chooses (normally in parallel)”, because field resolution is expected to be free of side effects. That is a permission, not a promise. Whether siblings overlap depends on the engine and on how the resolvers are written.
On the field-walk query, booking.getCargo ends before routing.getItinerary and delivery.getDelivery start, those two overlap, and the two getLocation calls overlap.
Python shows the same shape. Total time follows the depth of the tree, three levels of 250 ms, not the five calls. Doubling the siblings at one level adds nothing, and adding a level adds 250 ms.
Siblings overlap when the resolver gives control back while it waits. Each stack’s idiom does that:
public <T> Uni<T> call(String request, String source, String op, String key, Supplier<T> lookup) {
return Uni.createFrom().deferred(() -> {
long startMs = nowMs();
return afterLatency(Uni.createFrom().item(lookup))
.eventually(() -> calls.add(new SourceCall(request, source, op, key, startMs, nowMs())));
});
}
async def call[T](
self, request: str, source: str, op: str, key: str, lookup: Callable[[], T]
) -> T:
"""Run ``lookup`` after the latency; log the call when it ends, however it ends."""
start_ms = _now_ms()
try:
await asyncio.sleep(self._latency)
return lookup()
finally:
self._calls.append(SourceCall(request, source, op, key, start_ms, _now_ms()))
Uni.deferred starts the clock when the engine subscribes, the timer waits without holding a thread, and eventually writes the log entry whether the call produced a value, a null or an error. Python does the same with await asyncio.sleep and a finally. Both record start and end from a monotonic clock.
Resolver style decides what a waiting call costs:
| Style | Behavior |
|---|---|
SmallRye, returns a Uni |
Subscribed on a Vert.x event-loop thread and resumed there after the timer. Holds no thread while waiting. |
| SmallRye, returns a plain value | A blocking method. Each call gets its own worker thread (executor-thread-1 and -2 in one run). Calls overlap, with a thread held for each. |
Strawberry, async def |
Awaited together by graphql-core. Overlaps without holding anything. |
Strawberry, plain def that blocks |
Runs one after another, 250 ms each, on the event loop, and holds every other request on that loop meanwhile. A function that never yields has no point at which to overlap. |
So Quarkus spends a worker thread per blocking call, and on Python a blocking resolver occupies the event loop. A resolver that is asynchronous end to end scales with concurrent requests instead of with threads. Many sibling calls that want the same data are the next problem, which DataLoader batching addresses.
Top-level mutation fields run serially
For a mutation the rule changes. “Normal and Serial Execution” says the fields of a mutation’s root selection set execute serially, in the order written, and each completes, nested selections included, before the next begins. Below that level nothing changes: the fields inside one mutation field’s payload are siblings again.
mutation {
a: registerHandlingEvent(input: { ... }) { cargo { delivery { transportStatus } } }
b: registerHandlingEvent(input: { ... }) { cargo { delivery { transportStatus } } }
}
The aliases are needed because two fields with one name and different arguments would collide. The serial rule exists because mutations have side effects and b may depend on what a wrote: if a registers a load event and b the matching unload, b is only valid once a has landed. Query fields get no such guarantee, so a client that needs ordering must use a mutation.
graphql-java, the engine under SmallRye, has a serial execution strategy for mutations (AsyncSerialExecutionStrategy), and graphql-core runs a mutation’s root fields through execute_fields_serially.
Assembling the response
Execution ends with a map: the response keys, in order, each holding the completed value of its field. “Serialized Map Ordering” says the map should follow the order of the selection set, and a response key is the alias when there is one and the field name otherwise.
{ b: cargo(trackingId: "XYZ789") { trackingId } a: cargo(trackingId: "ABC123") { trackingId } }
Both stacks answer {"data":{"b":{"trackingId":"XYZ789"},"a":{"trackingId":"ABC123"}}}, with b before a, although a sorts first alphabetically and either getCargo call might finish first. Selection order is the only order a client can rely on.
errors sits beside data. When a resolver fails, “Handling Execution Errors” has the engine record an error whose path names the failed field, null the field, and continue with its siblings, so partial data comes back with errors. A request error has no data at all.
Meta fields cost nothing
Fields whose names begin with two underscores belong to the engine. __typename returns the name of the object type the selection set is applied to, and __schema and __type answer from the schema. { __typename } returns {"data":{"__typename":"Query"}} on both stacks with an empty log: it is answered after validation without a resolver, so it can sit in any selection set and never adds a call. It also checks that a deployment parses, validates and executes a request end to end.
Status codes
GraphQL-over-HTTP, the working draft listed in the Bibliography, covers this under “Status Codes”. For an application/graphql-response+json response one rule is mandatory: a result without a data entry is a request error, and the server must answer with a 4xx or 5xx status. It recommends 400 when the document can’t be parsed, and 422 when validation fails, the operation can’t be determined or variables can’t be coerced. A result that executed is 200 without errors, and 294, a partial-success code the draft defines itself and recommends only with its own media type, when errors sits beside data. A server must support that media type. A client that accepts only application/json is a legacy client, and the draft asks servers to apply the same status codes to it, except that a 2xx response keeps the application/json media type. A gateway, load balancer or log can then tell a rejected request from an executed one without reading the body.
Both stacks answer HTTP 200 for every request error above: parse, validation and coercion on both, and the missing operationName on Quarkus.
| Request | Quarkus | Python |
|---|---|---|
Parse error, Accept: application/json |
200, data absent |
200, "data":null |
Unknown field, Accept: application/json |
200 | 200 |
Unknown field, Accept: application/graphql-response+json |
200 with that content type | 200 with application/json |
Quarkus acknowledges the new media type in Content-Type but answers 200 with no data, where the draft recommends a 4xx. Strawberry answers application/json. A client therefore detects a request error from the body on either stack: errors present and data absent or null. A gateway that wants the 4xx semantics can add them. These values belong to SmallRye GraphQL 2.18.5 and Strawberry 0.335.0, and they’re asserted so that a change on upgrade is visible.
🤖 Explore with your agent
“In
examples/06-execution-model, add aCargo.handlingCountfield that calls a newhandling.countEventssource with a 250 ms latency, on both stacks. Write it as aUnion Quarkus andasync defon Python, take the request id at method entry, and query it together withitineraryso the two sibling calls overlap in/debug/source-calls. Then show what the log looks like if the Python version is a plaindefthat blocks.”Check:
curl localhost:8160/debug/source-callsshowsgetItineraryandcountEventswith overlappingstartMsandendMswindows, and the blocking variant shows them one after the other.
Build, run, observe
Quarkus is on 8160 and Python on 8161, and compose sets TRACKING_SOURCE_LATENCY_MS=250 and TRACKING_DEBUG_ENDPOINTS=true. Reset the log, send a query that selects only a booking field, and read the log:
docker compose -f examples/06-execution-model/compose.yaml up -d --build
curl -s -X DELETE -w '%{http_code}\n' localhost:8160/debug/source-calls
curl -s localhost:8160/graphql -H 'Content-Type: application/json' -d '{"query":"{ cargo(trackingId: \"ABC123\") { trackingId description } }"}'
curl -s localhost:8160/debug/source-calls
204
{"data":{"cargo":{"trackingId":"ABC123","description":"Palletized electronics"}}}
{"calls":[{"request":"16323002360","source":"booking","op":"getCargo","key":"ABC123","startMs":98,"endMs":351}]}
One call, to the one source whose data the selection needs. Now the field-walk query, after another reset:
curl -s localhost:8160/graphql -H 'Content-Type: application/json' -d '{"query":"{ cargo(trackingId: \"ABC123\") { itinerary { legs { loadLocation { unLocode } } } delivery { transportStatus } } }"}'
curl -s localhost:8160/debug/source-calls
{"data":{"cargo":{"itinerary":{"legs":[{"loadLocation":{"unLocode":"USNYC"}},{"loadLocation":{"unLocode":"DEHAM"}}]},"delivery":{"transportStatus":"ONBOARD_CARRIER"}}}}
{"calls":[{"request":"16323002361","source":"booking","op":"getCargo","key":"ABC123","startMs":509,"endMs":760},{"request":"16323002361","source":"routing","op":"getItinerary","key":"ABC123","startMs":762,"endMs":1013},{"request":"16323002361","source":"delivery","op":"getDelivery","key":"ABC123","startMs":763,"endMs":1025},{"request":"16323002361","source":"location","op":"getLocation","key":"USNYC","startMs":1021,"endMs":1272},{"request":"16323002361","source":"location","op":"getLocation","key":"DEHAM","startMs":1023,"endMs":1274}]}
The request id and timings differ on every run and on Python, where the id is a uuid4. The shape holds: getCargo ends before getItinerary and getDelivery start, those two overlap, and the two getLocation calls overlap. A failed request leaves the log empty. With the unclosed-brace document:
curl -s -X DELETE localhost:8161/debug/source-calls
curl -s -w ' [HTTP %{http_code}]\n' localhost:8161/graphql -H 'Content-Type: application/json' -d '{"query":"{ cargo(trackingId: \"ABC123\") { trackingId }"}'
curl -s localhost:8161/debug/source-calls
{"data":null,"errors":[{"message":"Syntax Error: Expected Name, found <EOF>.","locations":[{"line":1,"column":45}]}]} [HTTP 200]
{"calls":[]}
bash examples/06-execution-model/verify.sh runs all of this on both stacks and prints PASS only when both hold. It compares order and overlap of the 250 ms windows and never a duration, so a slow machine doesn’t fail it.
Experiments:
- Drop the operation name. Send two operations with no
operationName. Quarkus returnserrorswith nodataand no source call. Python runs the first operation and makes onebooking.getCargo:ABC123call. - Select
__typenamealone.{ __typename }returnsQuerywith an empty log. - Ask for a different
Accepttype. An unknown field withAccept: application/graphql-response+jsonreturns200 application/graphql-response+jsonon Quarkus and200 application/jsonon Python.
Decision
Parse, validation and coercion are where a document can be refused before any data is touched, so cost controls attach there. Write resolvers asynchronous end to end (
Union Quarkus,async defon Python) so siblings overlap and latency follows the depth of the tree, not the call count. Keep ordered, dependent writes in one mutation, never in chained queries. SendoperationNameon every multi-operation document, and treaterrorswith absent or nulldataas the request-error signal, never the status line.
For deeper coverage
- Samer Buna, GraphQL in Action (Manning, 2021): “Implementing schema resolvers” covers the resolver execution model. Listed on the Bibliography page.
- GraphQL specification: “Executing Requests”, “Coercing Variable Values”, “Executing Selection Sets” with “Normal and Serial Execution” and “Field Collection”, and “Handling Execution Errors”, in that order. The GraphQL over HTTP draft covers “Status Codes”.
- Cloud Native Design Patterns, Protocols in Depth: the HTTP semantics and status-code conventions behind the GraphQL-over-HTTP comparison.
- SmallRye GraphQL: the Mutiny (
Uni) resolver model the Quarkus examples use.