Introducing a new way to review your PRs.

Read the announcement

strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓

View on GitHub

21 documents · Updated Nov 18, 2026

docs / extensibility

Extensibility and cross-cutting behavior

Strawberry provides shared ways to change schema meaning and request handling without putting that work in every resolver. Extensions act on the request lifecycle or on individual fields. Directives add reusable schema or operation rules. Permissions protect fields. Tracing reports request work. File uploads connect multipart HTTP requests to GraphQL mutation inputs.

Request processing model

Request lifecycle

The request lifecycle turns an HTTP payload into a GraphQL result. The HTTP integration first reads JSON or multipart data. The schema then parses and validates the operation before it executes resolvers. Cross-cutting behavior can observe or change each stage through the same execution context.

The main stages are:

  1. The integration parses the request and creates the operation context.
  2. The schema parses the GraphQL document and validates it.
  3. Field extensions and resolvers produce field values.
  4. The schema returns the result. Extension data can be placed in the GraphQL extensions response field.

This diagram shows where the main extensibility points sit in the request path.

flowchart LR
    A[HTTP request] --> B[Request parsing]
    B --> C[Operation context]
    C --> D[Parse and validate]
    D --> E[Field extensions]
    E --> F[Permissions and resolver]
    F --> G[Directive value changes]
    G --> H[GraphQL result]
    H --> I[Tracing and extension data]
    I --> J[HTTP response]

References: strawberry/…/base.py• L33-83 strawberry/…/schema.py• L730-797 strawberry/…/execution.py• L50-73

Extension scopes

Strawberry has schema extensions and field extensions. A schema extension applies to request stages such as the operation, parse, validation, execution, or streamed results. A field extension forms a wrapper around one field resolver and can also change converted arguments before the resolver runs. See the extensions guide and the related extensions, directives, and permissions page.

Schema extensions are supplied to strawberry.Schema as classes or factories. Strawberry creates a fresh extension for each request so request state does not pass between concurrent requests. An extension may also return data for the result's extensions field.

References: strawberry/…/schema.py• L260-299 strawberry/…/schema.py• L442-486 strawberry/…/base_extension.py• L16-76

Hook ordering

Extension hooks run around a lifecycle stage. The hook's setup runs before the stage and its cleanup runs after the stage. The same model supports synchronous and asynchronous execution. Streaming results add one more hook point so an extension can inspect each result frame before the transport sends it.

Field extensions use a separate resolver chain. Their order matters because each extension receives the next step in the chain. Async-only and sync-only extensions must be ordered so that the selected execution mode can support the full chain.

References: strawberry/…/context.py• L34-142 strawberry/…/context.py• L145-213 strawberry/…/field_extension.py• L90-181

Parse and validation caches

Parse and validation caches are optional schema extensions that reuse work for repeated GraphQL documents. They keep extension instances safe to create per request while sharing bounded LRU cache data across requests that use the same cache size.

Both caches use 128 entries by default. An unbounded cache is available for a trusted and bounded set of query texts, but a public endpoint with many unique queries can otherwise grow its memory use without limit.

References: strawberry/…/parser_cache.py• L9-20 strawberry/…/parser_cache.py• L23-61 strawberry/…/validation_cache.py• L7-22 strawberry/…/validation_cache.py• L25-67

Schema customization

Operation directives

An operation directive is a named rule that a client puts in a query, mutation, or subscription. Strawberry registers the directive with the schema and applies its resolver to the selected field value. A directive resolver can receive its declared arguments, the current Info, and the value returned by the next resolver.

Strawberry resolves custom directives after the field resolver returns. It gets directive arguments from operation variables, converts them to Strawberry types, and then passes the field value to the directive when the resolver declares a value parameter. The sync and async execution paths apply the same rule with matching resolver behavior. The built-in @include and @skip directives remain GraphQL control rules and are not passed through custom directive resolvers. Input object arguments can also require Strawberry to build the Info value used during argument conversion. See the operation directives page.

References: strawberry/directive.py• L65-140 strawberry/…/directives.py• L26-70 strawberry/…/directives.py• L73-124 strawberry/…/schema.py• L349-400

Schema directives

A schema directive describes a rule or piece of metadata attached to the schema definition. It can target schema elements such as object types, fields, arguments, inputs, enums, or unions. Strawberry records the directive's fields and allowed locations. It can also mark the directive as repeatable or control whether its definition is printed.

Schema directives and operation directives serve different sides of the system. A schema directive describes the published contract. An operation directive changes how a client request is evaluated. See the schema directives page.

References: strawberry/schema_directive.py• L14-73 strawberry/…/schema.py• L349-410

Access control

Permission checks

A permission is a field-level access check that runs before the field resolver. It receives the source object, Info, and field arguments. The check can be synchronous or asynchronous. It must produce a real boolean: synchronous execution rejects awaitables, and both execution modes reject other non-boolean results.

Permission checks read authentication state from the request context. Strawberry supplies the context but does not decide how an application authenticates a user. A failed check raises a GraphQL error by default and can use a custom error type or error data. A permission can instead fail silently by returning None or an empty list, but only for nullable or list fields.

Each permission class also has a field-definition schema directive based on its class identity. The permission extension can add these directives to fields, while avoiding duplicate entries. This lets the schema describe permission usage without changing the access check itself. See the permissions guide.

References: strawberry/permission.py• L39-141 strawberry/permission.py• L144-201 strawberry/permission.py• L203-278 strawberry/…/field.py• L149-165

Observability

Tracing adapters

Tracing extensions measure the work done by a GraphQL request and expose it to an observability system. They can record parse and validation time as well as resolver timing. Strawberry provides adapters for Apollo tracing, Apollo Federation trace data, Datadog, and OpenTelemetry. See the tracing guide.

Tracing is part of the extension system, so it follows the same sync or async execution rules as other schema extensions. Tracing skips introspection and default field resolution where there is no application resolver work to measure. Apollo tracing reports operation, parsing, validation, and resolver timing in response extension data. Apollo Federation tracing is opt-in through the apollo-federation-include-trace: ftv1 request header and returns a base64-encoded trace under extensions.ftv1. It records resolver paths and errors for trusted gateway requests. The federation adapter requires the optional protobuf package; without it, that trace is disabled.

References: strawberry/…/__init__.py• L16-43 strawberry/…/utils.py• L14-22 strawberry/…/apollo.py• L84-219 strawberry/…/apollo_federation.py• L289-374 strawberry/…/apollo_federation.py• L460-498 strawberry/…/utils.py• L10-38

File transfer

Multipart upload boundary

File uploads connect the GraphQL multipart request specification to mutation arguments. The request contains an operations document, a map of file parts to variable paths, and the file data. The HTTP integration replaces the matching null placeholders before Strawberry executes the operation. A mutation can then accept the Upload scalar or a list of uploads.

Multipart uploads are disabled by default because an enabled multipart endpoint needs protection against the security risks described by the multipart request specification. The runtime file object comes from the selected web integration, so resolver code and type overrides must match that integration. See the file upload guide.

References: strawberry/…/base.py• L33-40 strawberry/…/async_base_view.py• L274-293 strawberry/…/sync_base_view.py• L145-152 strawberry/…/scalars.py• L3-12 strawberry/…/utils.py• L6-33

Change checklist

When these files or sections are changed, remember to consider these:

  • When adding a schema extension, check its per-request construction and both sync and async hook behavior. Also check whether its result data changes the GraphQL response contract.
  • When changing field extension order, check sync-only and async-only compatibility across the full resolver chain.
  • When adding an operation directive, register it on the schema and keep its declared locations aligned with the query positions where clients will use it.
  • When changing directive argument or value handling, check variable coercion, input object conversion, and the Info and field-value inputs expected by directive resolvers.
  • When changing schema directives, check that their allowed locations, repeatable setting, and printed definition match the schema contract.
  • When changing permissions, check that every check returns a real boolean in the selected execution mode. Also check the request context contract, the field-definition directive identity, and the return type required by fail_silently.
  • When changing parser or validation caching, check cache bounds and all inputs that affect the parsed document or validation result.
  • When enabling multipart uploads, check CSRF protection, the integration's upload object type, and any scalar override used for type checking.
  • When enabling federation tracing, check the request header, optional protobuf dependency, response extension format, and trusted gateway boundary.
  • When changing Apollo tracing paths or skip rules, check that introspection and default field resolution remain excluded while application resolver paths stay complete.

Important failure modes

TriggerConsequenceGuard
A shared extension instance stores request stateConcurrent requests can observe or overwrite each other's statePass an extension class or factory so each request gets a fresh instance
A sync execution uses an async schema hookExecution raises a synchronous-completion errorUse the async execution path or a sync extension variant
Sync-only and async-only field extensions are mixed in an invalid orderStrawberry cannot build a valid resolver chainPut sync-only extensions before async-only extensions or provide both modes
A parser or validation cache is unbounded on a public endpointUnique query texts can grow process memory without limitUse the bounded default or set a trusted, bounded query set before using maxsize=None
A synchronous permission check returns an awaitableAccess is rejected with a sync-context error instead of being granted by a truthy awaitableUse an ordinary boolean in sync execution or use the async execution path
A permission check returns a non-boolean resultStrawberry rejects the check instead of using truthiness to grant or deny accessReturn True or False from every permission check
A custom directive is not registered with the schemaA query can name the directive but Strawberry cannot resolve itAdd the directive to the schema and keep its GraphQL name consistent
A custom directive has an input object argument that cannot be convertedDirective execution cannot build the expected typed argument or Info valueKeep directive input types aligned with the schema and use the supported conversion path
A directive expects a value or Info input that its resolver does not declareThe directive cannot apply the intended field transformation or context-aware ruleMatch the directive resolver parameters to the values it needs
Silent permission failure is used on a non-null scalar fieldSchema setup raises an error because no safe empty value existsUse a nullable or list field when silent failure is required
Multipart upload support is enabled without request protectionCross-site requests can reach upload mutationsEnable suitable CSRF protection before enabling multipart uploads
An upload map names a file that is absent from the formThe integration returns an HTTP 400 errorKeep each map entry matched to a submitted file part
Federation tracing is requested without the optional protobuf packageThe request completes without extensions.ftv1 trace dataInstall the federation tracing extra when the response trace is required
Federation tracing is exposed to public clientsResolver timing and error details can leak through the responseRestrict the federation trace header to trusted gateway traffic

Key files

FileWhy to read it
strawberry/…/schema.pyShows how schema options become request extensions and how execution stages are connected.
strawberry/…/base_extension.pyDefines the shared lifecycle model for schema extensions.
strawberry/…/context.pyShows how extension hooks surround each lifecycle stage and streamed result.
strawberry/…/field_extension.pyDefines the field-level resolver chain and its sync/async rules.
strawberry/…/parser_cache.py and strawberry/…/validation_cache.pyShow the optional caches for repeated document parsing and validation work.
strawberry/directive.py and strawberry/…/directives.pyExplain operation directive definitions and how directive resolvers receive converted arguments, field values, and request information.
strawberry/schema_directive.pyDefines schema directives, their allowed schema locations, and their schema-printing options.
strawberry/permission.pyDefines permission checks, strict boolean results, automatic field-definition directives, and field-level failure behavior.
strawberry/…/apollo.py and strawberry/…/apollo_federation.pyShow the response formats and request signals for Apollo and Federation tracing.
strawberry/…/utils.pyDefines the shared path and introspection checks used by tracing.
strawberry/file_uploads/ and strawberry/…/async_base_view.pyConnect the upload scalar and multipart placeholder mapping to HTTP request handling.