GraphQL execution
Strawberry turns a GraphQL request into a checked operation, invokes the selected fields, and returns data with any errors. The execution model supports queries, mutations, subscriptions, synchronous work, asynchronous work, and streamed results.
Execution lifecycle
An operation moves through request setup before it reaches application code. Strawberry keeps request state in an execution context so the selected operation, variables, context, errors, and result remain available to the execution flow and its extensions.
The following flow shows the main path from a request to a result.
flowchart LR
A[GraphQL request] --> B[Parse document]
B --> C[Select operation]
C --> D[Check allowed operation type]
D --> E[Validate document]
E --> F[Invoke field resolvers]
F --> G[Resolve async values]
G --> H[Build execution result]
B -. invalid request .-> I[Pre-execution error]
C -. no matching operation .-> I
D -. disallowed operation .-> I
E -. invalid document .-> I
Operation inputs
The execution entry points accept the GraphQL document plus values that connect the request to application code. Variables supply client input. The context value carries shared request state. The root value supplies the starting object for root fields. An operation name selects one operation when a document defines more than one.
The caller can also restrict which operation types are allowed. HTTP and streaming adapters use this boundary to enforce transport rules before resolver work starts.
| Input | Role in execution |
|---|---|
| Query document | Defines the operation and selected fields |
| Variables | Supplies values for declared operation variables |
| Context value | Shares request-scoped state with resolvers |
| Root value | Supplies the root resolver source |
| Operation name | Selects a named operation |
| Allowed operation types | Limits the operations that may run |
References: strawberry/…/base.py• L43-92 strawberry/…/schema.py• L488-507 strawberry/…/execution.py• L50-73 strawberry/…/info.py• L93-111
Operation preparation
Preparation parses the document, finds the requested operation, checks its type, and validates its fields against the schema. This order keeps malformed or disallowed requests away from resolvers.
The operation name identifies a named operation. If no name is given, Strawberry uses the first operation definition. A missing query, invalid syntax, unknown operation, or disallowed operation stops the normal execution path before field resolution.
References: strawberry/…/schema.py• L148-194 strawberry/…/schema.py• L560-596 strawberry/…/operation.py• L13-41 strawberry/…/execution.py• L75-105
Schema ordering
StrawberryConfig.sort_schema controls whether Strawberry sorts the schema by name when it builds the schema. This changes the order seen in introspection and exported schema text, while Strawberry keeps the execution link attached to the sorted schema.
References: strawberry/…/config.py• L20-52 strawberry/…/schema.py• L488-499
Validation boundary
Validation is the safety boundary between a parsed request and application work. Strawberry checks the document against the built schema before it invokes a resolver.
Document validation
Document validation applies the standard GraphQL rules and Strawberry-specific input rules. Extensions can add validation behavior or provide a parsed document before the normal parse step.
Strawberry also checks the meaning of optional input values at this boundary. Maybe[T] is an input-only marker that means a value may be omitted but may not be set to explicit null; Maybe[T | None] also permits explicit null. The rule applies to field arguments and ordinary input-object fields, and it uses GraphQL field names when it reports a violation. Input types that build their own values, such as Pydantic-backed inputs, handle this check with their own input validation so the error stays with the other input errors.
Validation errors are pre-execution errors. They return without resolver data because the request did not reach the field execution stage. Schema construction performs separate checks when the schema is created, so schema definition faults are not request-time resolver errors. In particular, a Maybe field is not valid on an output type because its presence semantics apply to input values, so schema construction rejects that definition before requests run.
References: strawberry/…/schema.py• L184-215 strawberry/…/schema.py• L704-712 strawberry/…/maybe_null.py• L17-119 strawberry/…/schema_converter.py• L439-460 strawberry/…/schema_converter.py• L633-674
Input coercion
Input coercion converts client values to the scalar and input types declared by the schema before the resolver receives them. Built-in date, time, and UUID scalars reject values with the wrong shape as GraphQL input errors. Decimal input accepts either text or numeric values by design, while invalid values still stop normal resolver execution.
Schema preparation classifies declared values by their GraphQL role before this contract is used. Shared type checks identify input objects, interfaces, scalars, directives, and generic GraphQL types so schema conversion can apply the matching rules.
Schema definition preparation also protects this contract. When a field annotation cannot yet be evaluated, Strawberry checks whether its metadata may contain field options. It fails schema setup when those options cannot be read, rather than silently building a field without permissions or other execution rules.
References: strawberry/…/compat.py• L18-49 strawberry/…/base_scalars.py• L13-44 strawberry/…/base_scalars.py• L50-103 strawberry/…/scalar.py• L48-100 strawberry/…/object_type.py• L51-102 strawberry/…/object_type.py• L104-199 strawberry/…/unresolved_annotations.py• L136-219
Resolver boundary
After validation succeeds, the GraphQL execution engine walks the selected fields. It starts with the root operation type and uses values returned by parent fields to resolve nested selections.
The boundary matters because errors have different meanings on each side. A parse or validation error rejects the operation as a whole. A resolver error belongs to a field and follows GraphQL's data and null rules.
References: strawberry/…/schema.py• L679-723 strawberry/…/schema.py• L764-797 strawberry/…/schema_converter.py• L633-695
Resolver invocation
Resolvers are the application-facing part of execution. Strawberry maps GraphQL field arguments to Python resolver inputs and adds request information without exposing that internal request state as client arguments.
Resolver inputs
A resolver receives the arguments declared by its GraphQL field. It can also receive Info, which is Strawberry's view of the current field and request. Info provides the context, variables, root value, operation, selected fields, return type, and field path needed for request-aware work.
Field extensions wrap the resolver path. They can reshape converted arguments and apply field policies before and around the resolver call.
The field definition supplies the type contract used by argument conversion and result completion. Generic object types can become concrete definitions when a schema uses specific type arguments, so resolver invocation uses specialized field types rather than unresolved type variables.
References: strawberry/…/schema_converter.py• L334-380 strawberry/…/schema_converter.py• L1111-1336 strawberry/…/info.py• L77-142 strawberry/…/field.py• L79-165 strawberry/…/base.py• L298-421
Synchronous and asynchronous work
execute is the asynchronous entry point. It accepts ordinary values and awaitable values, then waits for asynchronous results before it builds the response. This allows one operation to use both synchronous and asynchronous resolvers.
execute_sync has a stricter contract. If any resolver or execution step produces an awaitable, the operation cannot complete through this entry point and Strawberry returns an error instead of silently leaving asynchronous work unresolved.
Extensions follow the same split. Asynchronous execution can await asynchronous extension hooks. Synchronous execution rejects an asynchronous hook because it cannot satisfy the synchronous contract.
References: strawberry/…/schema.py• L679-723 strawberry/…/schema.py• L725-904 strawberry/…/await_maybe.py• L7-15 strawberry/…/runner.py• L49-66 strawberry/…/field.py• L421-427
Resolver errors
An exception from a resolver becomes a GraphQL error. GraphQL can keep data from other nullable fields, while the failed field becomes null. A failure in a non-null field can remove data from its enclosing result through GraphQL null propagation.
For fields that return a declared error union, configured exception handlers can turn matching resolver exceptions into a typed result. Strawberry decides whether a handler applies from the final field type after field extensions have run. A handler may return a value or decline the exception, in which case the original exception follows the normal GraphQL error path. Subscription fields do not use these handlers to turn event failures into union results.
Strawberry processes errors before it returns them. The default policy logs the errors. A schema can change that policy while the GraphQL result still describes the client-visible failure.
References: strawberry/…/schema_converter.py• L204-226 strawberry/…/schema_converter.py• L1093-1109 strawberry/…/schema_converter.py• L1111-1230 strawberry/…/schema.py• L638-661 strawberry/…/base.py• L122-140
Execution results
The result is the contract between execution and the transport layer. It carries selected data, GraphQL errors, and optional extension data.
Result shape
An ordinary operation returns an ExecutionResult with data, errors, and optional extensions. The fields are independent: a response can contain useful data and errors at the same time.
Streaming execution uses the same result concept for normal frames. A query or mutation sent through stream yields one result. A subscription yields multiple results. Incremental delivery can yield initial and later frames that the transport must format.
References: strawberry/…/execution.py• L108-131 strawberry/…/schema.py• L1032-1088 strawberry/…/schema.py• L1120-1145
Data and errors
Successful fields appear under data. Request errors found before resolver work produce no resolver data. Field errors can produce partial data, so clients must inspect both data and errors.
Strawberry converts Python exceptions into GraphQL errors and retains the original cause for server-side error handling. This keeps the response in GraphQL form without losing the source of the failure to logs or custom error processing.
References: strawberry/…/execution.py• L108-124 strawberry/…/schema.py• L176-194 strawberry/…/schema.py• L782-797 tests/…/test_execution.py• L175-220 tests/…/test_execution.py• L464-488
Extension results
Schema extensions observe operation, parsing, validation, execution, and streamed-result stages. Their output is merged into the response's extensions field after the operation or result frame is handled.
This lifecycle gives cross-cutting features a place to inspect or measure execution without changing resolver code. Each request receives its own extension runner, which keeps request state separate when operations run at the same time.
References: strawberry/…/runner.py• L21-66 strawberry/…/context.py• L145-213 strawberry/…/schema.py• L456-486 strawberry/…/schema.py• L754-782
Streaming and subscriptions
Streaming changes the result delivery pattern, not the preparation rules. The stream entry point parses and validates once, then yields one or more results based on the operation type.
Streamed operations
Queries and mutations produce one result over a streaming transport and then complete. Subscriptions produce an asynchronous sequence. Incremental delivery can add later frames for a query or mutation when the feature is enabled.
The transport owns wire formatting. It must handle an initial pre-execution error as an operation-level failure and must format any supported incremental frames for its protocol.
References: strawberry/…/schema.py• L906-1088 strawberry/…/schema.py• L1120-1171 tests/…/test_stream.py• L41-85 tests/…/test_stream.py• L175-180
Subscription operations
A subscription starts with a validated subscription field that produces an asynchronous iterator. Each event from that iterator runs through field execution and becomes a result for the client. The stream ends when the iterator completes or the client cancels the operation.
WebSocket handlers pass request values to Schema.stream, send each ExecutionResult, and send a completion message when the stream ends. They treat immediate errors differently from later event errors so the protocol can close or report the operation at the correct stage.
See Subscriptions and streaming for protocol behavior and transport-specific lifecycle rules.
References: strawberry/…/schema.py• L945-1030 strawberry/…/schema.py• L1090-1118 strawberry/…/schema_converter.py• L633-695 strawberry/…/handlers.py• L251-327 strawberry/…/handlers.py• L186-218
Batched data access
Resolvers often need related records for several selected fields. Strawberry's DataLoader groups loads made during the same event-loop turn and sends their keys to one asynchronous load function.
DataLoader coordination
The loader returns an awaitable for each requested key. It schedules a batch for the next event-loop turn, then maps the load function's values back to the waiting callers in key order. This reduces repeated downstream reads while keeping resolver code focused on the record it needs.
The loader can cache pending and completed values. It can also split large batches, clear or prime cache entries, and report a failure to the callers whose keys were in the batch.
See Dataloading and batching for data access patterns and cache choices.
References: strawberry/dataloader.py• L106-220 strawberry/dataloader.py• L223-292 tests/…/test_dataloaders.py• L9-45
Batch result contract
The load function must return one value or exception for each input key. A different result count makes the whole batch invalid because Strawberry cannot map values to callers safely.
A per-key exception affects that key's awaitable. An exception from the load function affects each non-cancelled caller in the batch. This preserves the link between a downstream data failure and the resolver that requested it.
References: strawberry/dataloader.py• L251-292 tests/test_dataloaders.py• L74-129
Related execution pages
These pages describe focused parts of the same execution model:
- Executing GraphQL operations covers the standard query and mutation request path.
- Subscriptions and streaming covers repeated results and streaming transports.
- Dataloading and batching covers grouped and cached data reads from resolvers.
- Schema and type system covers the schema contract that validation and execution use.
- Extensibility covers extension hooks that observe or change execution.
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to parsing, operation selection, or allowed operation types must keep request adapters and all
Schemaentry points aligned. - Changes to validation rules must preserve the difference between omitted
Maybe[T]input and explicitnull, including GraphQL name conversion for input fields and the separate path for inputs that build their own values. - Changes to schema conversion must keep
Mayberestricted to input types and reject it on output fields before resolver execution. - Changes to scalar parsing must preserve GraphQL input errors for invalid values and the accepted input forms of built-in scalars.
- Changes to type compatibility checks must keep schema conversion aligned with the distinction between input objects, interfaces, scalars, directives, and generic GraphQL types.
- Changes to deferred annotation handling must not silently drop field options such as permissions or extensions before schema construction.
- Changes to schema ordering must preserve the link between the sorted GraphQL schema and Strawberry's execution context, including introspection and exported schema output.
- Changes to pre-execution errors must preserve the difference between an operation-level failure and an error from a field or subscription event.
- Changes to async detection must preserve the no-awaitable contract of
execute_syncand the mixed sync/async contract ofexecute. - Changes to field extensions or return types must keep typed exception-handler matching aligned with the final field type.
- Changes to result handling must keep
data,errors, andextensionsconsistent across ordinary results, stream frames, and WebSocket messages. - Changes to subscription iteration must preserve cancellation, iterator cleanup, and the completion message for each supported protocol.
- Changes to
DataLoaderbatching must preserve one output per input key and the mapping of per-key exceptions to their waiting resolvers.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The document has invalid syntax or fails validation | Resolver work does not start and the client receives a pre-execution error | Send a complete document that matches the current schema |
A regular Maybe[T] argument or input field receives explicit null | Validation rejects the operation even though the GraphQL type is nullable | Omit the field or use `Maybe[T |
A Pydantic-backed input receives null for a Maybe[T] field | The input reports the problem with its own validation errors instead of the shared operation-level rule | Keep the model's input contract and null handling aligned with the GraphQL type |
A Maybe field is declared on an output type | Schema construction fails before the schema can serve requests | Use a normal nullable output type and reserve Maybe for input presence semantics |
| A built-in scalar receives a value with the wrong input shape | Input coercion fails before the resolver receives the value | Send the scalar's accepted GraphQL representation |
| A type is classified under the wrong GraphQL role during schema setup | Schema conversion can apply the wrong input, output, or directive rules | Keep compatibility checks aligned with the type definition metadata |
| Field options use names that are not yet defined in a deferred annotation | Schema setup fails instead of silently omitting permissions or other field rules | Define option dependencies before the type or use resolvable annotations |
| The operation name is missing or does not identify a usable operation | Strawberry cannot route the request to a root operation | Use the exact name for a multi-operation document or send one intended operation |
| The transport disallows the selected operation type | The request is rejected before resolver invocation | Match the request method or stream transport to the operation type |
sort_schema is enabled but code relies on the pre-sort schema object | Introspection or resolver metadata can refer to a schema without Strawberry's execution link | Keep the sorted schema attached to the owning Schema instance |
execute_sync reaches an awaitable resolver or extension | Synchronous execution fails instead of returning completed data | Use execute when downstream work can be asynchronous |
| A resolver raises for a nullable field | The field becomes null and the response includes an error | Inspect both data and errors and apply server-side error handling |
| A configured exception handler declines a matching resolver error | The original exception becomes a normal GraphQL field error | Return a typed error value when the exception should be part of the field result |
| A subscription iterator is cancelled without cleanup | The operation can remain active and resources can stay open | Close the result stream and cancel the operation task |
| A batch load function returns the wrong number of values | The batch cannot map results to keys and waiting loads fail | Return one value or exception for every input key |
Key files
| File | Why to read it |
|---|---|
| strawberry/…/schema.py | Main lifecycle for parsing, validation, resolver execution, results, subscriptions, and streams |
| strawberry/…/config.py | Schema settings that affect execution and the order of introspection and exported schema output |
| strawberry/…/execution.py | Request state and result types shared by normal and streamed execution |
| strawberry/…/schema_converter.py | Bridge from GraphQL field calls to Strawberry resolver calls, input rules, output-type checks, and field error handling |
| strawberry/…/maybe_null.py | Validation boundary for explicit null on Maybe input arguments and fields |
| strawberry/…/compat.py | Shared checks that classify schema types before conversion and execution can use them |
| strawberry/…/base_scalars.py | Input and output rules for built-in date, time, decimal, and UUID scalars |
| strawberry/…/scalar.py | Registry that connects Python scalar types to GraphQL scalar behavior |
| strawberry/…/field.py | Field contracts for resolver inputs, type resolution, extensions, and async status |
| strawberry/…/object_type.py | Type preparation and field annotation checks before schema construction |
| strawberry/…/base.py | Object definitions and generic type specialization used by field execution |
| strawberry/…/unresolved_annotations.py | Checks that deferred annotations do not hide field options |
| strawberry/…/info.py | Request and field data available to a resolver through Info |
| strawberry/…/runner.py | Extension stages and response metadata collection |
| strawberry/…/handlers.py | Transport handling for streamed results and immediate subscription errors |
| strawberry/dataloader.py | Grouping, caching, dispatch, and error rules for batched data access |
| tests/…/test_stream.py | Guarantees for one-result operations, subscriptions, pre-execution errors, and streamed output |
| tests/…/test_dataloaders.py | Guarantees that resolvers can use grouped data loads during execution |