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 / http-and-asgi-transport

HTTP and ASGI transport

The HTTP and ASGI transport layer turns web requests into GraphQL operations and turns execution results into web responses. It gives the framework adapters a shared contract for request formats, response data, GraphQL IDE pages, uploads, batching, and streaming while leaving framework-specific request and response objects to the adapter layer. See Web framework integration for the larger serving model and Framework adapters for the bindings that use this layer.

Request and response boundary

The transport boundary separates GraphQL concerns from web framework concerns. A serving integration supplies a request adapter and response builder, while the shared views decide whether the request is valid, how to read its GraphQL data, and which result form the client receives.

This diagram shows how a framework request becomes normalized GraphQL input and then returns as a complete or streaming response.

flowchart LR
    A[Framework request] --> B[Request adapter]
    B --> C[Shared HTTP view]
    C --> D[GraphQL request data]
    D --> E[Schema execution]
    E --> F[GraphQL response data]
    F --> G[Response builder]
    G --> H[Framework response]

Request data

GraphQLRequestData is the common form of one incoming operation. It carries optional query text, variables, the selected operation name, extensions, and the transport used for a streaming request. A JSON object or a query string becomes this form before the schema executes it. An extension can provide the query when the request does not include query text.

The request parser accepts one operation or a list of operations. A list is a batch and is checked against the schema batching settings before execution. Each item keeps its own variables, operation name, and extensions so one HTTP request can carry separate operations without merging their inputs.

References: strawberry/…/__init__.py• L40-51 strawberry/…/base.py• L133-144 strawberry/…/async_base_view.py• L252-302

Response data

GraphQLHTTPResponse is the common result shape sent to an HTTP client. Normal results contain data and may contain formatted errors and extensions; incremental results can also report pending work, completed work, and later data. The framework adapter wraps this data in its native response object and carries over status, headers, and other response state supplied during request handling.

GraphQL execution errors are normally part of the GraphQL response rather than transport failures. Invalid HTTP methods, malformed bodies, unsupported media types, and invalid request parameters are reported as HTTP errors before or around execution.

References: strawberry/…/__init__.py• L13-37 strawberry/…/sync_base_view.py• L225-285 strawberry/…/async_base_view.py• L504-523 strawberry/…/__init__.py• L169-188

HTTP request formats

The shared views support the common GraphQL over HTTP entry points and normalize their differences before they call the schema. This lets an adapter expose the same GraphQL behavior even when its framework uses a different request API.

GET and POST requests

Only GET and POST are accepted by the shared HTTP boundary. GET reads the operation from query parameters and may carry JSON-encoded variables and extensions; POST reads a JSON body or an enabled multipart form. Query execution through GET is configurable, and the default serving integrations allow queries but reject mutations through that method.

The parser checks the shape of query, variables, and extensions before execution. A missing query or an operation that is not allowed for the HTTP method produces a client error instead of an ambiguous schema call.

References: strawberry/…/base.py• L43-60 strawberry/…/base.py• L68-83 strawberry/…/sync_base_view.py• L82-143 strawberry/…/async_base_view.py• L446-457 strawberry/…/async_base_view.py• L650-706

JSON bodies

JSON is the normal request format for POST. The view reads the body through the adapter, decodes it, and maps its GraphQL fields into GraphQLRequestData; the same decoder can be replaced by an integration when an application needs a different JSON library.

Malformed JSON is a request error with status 400. The encoder is also shared by normal responses and streaming messages, so a custom encoder must produce text or bytes that the selected response path can send.

References: strawberry/…/base.py• L56-66 strawberry/…/sync_base_view.py• L154-182 strawberry/…/async_base_view.py• L650-665 strawberry/…/__init__.py• L75-83

Multipart uploads

Multipart upload handling combines a JSON operations value with a JSON map value and uploaded files. The map names paths such as a variable location, and the parser places each file at those paths before the operation reaches GraphQL. This supports one file, lists of files, and files nested inside input values.

Uploads are disabled by default. An integration must enable them and provide form-data support; malformed forms and missing mapped files become 400 errors. Applications that enable uploads must also review the security requirements of the GraphQL multipart request format.

References: strawberry/…/sync_base_view.py• L145-152 strawberry/…/sync_base_view.py• L168-182 strawberry/…/async_base_view.py• L339-358 strawberry/…/async_base_view.py• L650-665 strawberry/…/utils.py• L6-33

Batched requests

A batched request is a JSON list of independent GraphQL request objects. Batching is available only when the schema has batching enabled and the list does not exceed its configured operation limit. The async view executes the items together, while the sync view processes each item in its sync execution path.

Streaming transports do not accept batches because one request must map to one live stream. The transport layer rejects that combination before execution and returns a clear client error.

References: strawberry/…/base.py• L133-144 strawberry/…/sync_base_view.py• L87-108 strawberry/…/async_base_view.py• L252-302 strawberry/…/async_base_view.py• L650-678

GraphQL IDE selection

The transport layer can serve a browser-based GraphQL IDE when a client asks for HTML instead of sending a query. The view uses the configured IDE name to select the matching static page and returns 404 when IDE serving is disabled.

IDE negotiation

An IDE request is a GET with no query parameter and an Accept header containing text/html or */*. The supported choices are GraphiQL, Apollo Sandbox, and Pathfinder. The framework integration can use the shared HTML or override rendering when it needs a custom page or response type.

This check happens before normal GraphQL body parsing. A browser visit can therefore receive an IDE page without a GraphQL operation, while a GET with a query continues through the normal request path.

References: strawberry/…/base.py• L43-51 strawberry/…/base.py• L85-87 strawberry/…/ides.py• L4-19 strawberry/…/sync_base_view.py• L230-245 strawberry/…/async_base_view.py• L446-452

Streaming responses

A streaming transport is the wire format used for a response that arrives in more than one part. The async view selects an enabled transport from the request headers, restricts the operation type when needed, and sends each result until it sends a completion marker.

Streaming protocol selection

The view enables stream transports through its configured protocol list. An Accept value of text/event-stream selects Server-Sent Events (SSE), while the multipart subscription media type selects the multipart subscription format. If no matching transport is enabled, the request remains a normal HTTP request instead of silently becoming a stream.

SSE sends next events and a final complete event. Multipart subscriptions send JSON payload parts inside a multipart response. Both formats preserve GraphQL result data and errors, but their headers and framing differ so clients can choose the format they understand.

References: strawberry/…/base.py• L33-40 strawberry/…/base.py• L89-131 strawberry/…/streaming.py• L73-107 strawberry/…/streaming.py• L109-192 strawberry/…/async_base_view.py• L650-665

Streaming heartbeats

Long-lived streams use heartbeats to keep idle connections open through servers and proxies. The shared stream wrapper sends protocol-specific heartbeat messages while it waits for the next GraphQL result, preserves result order, and stops its background work when the source ends or the client disconnects.

SSE uses a comment heartbeat and does not send one at connection start. Multipart subscriptions send an initial empty part and then periodic heartbeat parts. The stream always emits completion after its result source finishes.

References: strawberry/…/streaming.py• L109-205 strawberry/…/streaming.py• L211-282 strawberry/…/async_base_view.py• L551-573

Incremental results

The async transport also sends incremental query results through multipart responses. The first response describes the initial data and pending work; later responses attach completed or incremental values to the paths announced by that first response.

This path is separate from subscription streaming. It uses the multipart response framing for a sequence of execution results, while subscription transports use the client-selected SSE or multipart subscription protocol.

References: strawberry/…/__init__.py• L13-20 strawberry/…/async_base_view.py• L480-502 strawberry/…/async_base_view.py• L575-640 strawberry/…/streaming.py• L36-71

Serving view modes

The shared serving layer has separate sync and async base views because a framework must match the execution model of its schema and response system. Both views expose the same high-level hooks for context, root value, response creation, IDE rendering, and result processing.

Synchronous view

SyncBaseHTTPView is the base for integrations that execute synchronous GraphQL resolvers and return a complete response from one call. It parses the request, calls synchronous schema execution, processes one result or a batch, and asks the adapter to build the final response.

Sync views do not produce live streaming responses. If a request selects SSE or multipart subscriptions, the view rejects it rather than returning a non-streaming result with the wrong meaning.

References: strawberry/…/base.py• L31-63 strawberry/…/sync_base_view.py• L65-143 strawberry/…/sync_base_view.py• L230-285 strawberry/…/streaming.py• L73-78

Asynchronous view

AsyncBaseHTTPView is the base for integrations that can await schema work and send response data over time. It handles complete HTTP responses, incremental results, SSE or multipart subscription streams, and WebSocket subscription handlers through framework-provided adapters.

Each integration supplies the request adapter and the native response operations. It also supplies context and root values, which lets application code connect authentication, request state, response headers, and other framework features to GraphQL execution without changing the shared transport rules.

Before a WebSocket handshake reaches application hooks, the view checks its Origin header. Same-origin requests and clients without an origin are accepted; cross-origin browser requests must match the normalized allowed_origins set or a custom origin policy. This protects cookie-backed subscriptions from cross-site WebSocket use.

The view keeps WebSocket negotiation separate from HTTP stream negotiation. When an integration enables SSE, the view does not advertise the SSE token as a WebSocket subprotocol; only the supported WebSocket protocols are offered during the WebSocket handshake.

References: strawberry/…/async_base_view.py• L84-250 strawberry/…/async_base_view.py• L381-435 strawberry/…/async_base_view.py• L446-523 strawberry/…/websocket_origin.py• L25-111

ASGI request handling

The ASGI GraphQL app is an async serving integration that maps ASGI HTTP and WebSocket scopes to the async base view. HTTP exceptions become plain-text HTTP responses, normal GraphQL results become JSON responses, and selected stream transports become native streaming responses.

The default ASGI context contains the request and response objects. The app exposes configuration for the schema, IDE, GET queries, uploads, subscription protocols, and allowed WebSocket origins, while the base view keeps parsing, origin checks, and execution behavior consistent with other async integrations.

The app negotiates WebSocket subprotocols from the protocols supported by the view. It rejects a disallowed cross-origin handshake before it accepts the connection, so the application does not create a subscription context for that request.

References: strawberry/…/__init__.py• L90-130 strawberry/…/__init__.py• L132-168 strawberry/…/__init__.py• L206-229 strawberry/…/websocket_origin.py• L57-83

Change checklist

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

  • Keep GraphQLRequestData.protocol aligned with the enabled stream transports. A mismatch can send a subscription to normal execution or select a transport that is not configured.
  • Keep the BaseView transport map aligned with protocol negotiation and content-type checks. A registered protocol must resolve to the transport that owns its headers, framing, and batch rule.
  • Keep the Accept and Content-Type matching rules aligned with stream headers and framing. Clients can fall back to a normal JSON response when a protocol is not enabled.
  • Keep multipart operations, map, and file-path replacement behavior aligned. A changed path rule can silently bind a file to the wrong variable.
  • Keep sync and async request validation aligned for method checks, parameter shapes, batch limits, and error status codes.
  • Keep response hooks and adapter response builders aligned so status codes and application-set headers survive normal and streaming responses.
  • Keep WebSocket origin normalization aligned with allowed_origins validation and same-origin checks. A change can reject trusted browser clients or allow an unintended origin.
  • Keep WebSocket subprotocol advertisement limited to WebSocket protocols when HTTP stream protocols change.
  • Keep non-WebSocket stream protocols out of WebSocket protocol negotiation when subscription protocol settings change.

Important failure modes

TriggerConsequenceGuard
A client sends a method other than GET or POSTThe request receives 405 and no GraphQL operation runsKeep method validation in the shared base view before body parsing
A POST uses malformed JSON or an unsupported content typeThe request receives a 400 transport errorSend application/json or enable and correctly format multipart uploads
Multipart uploads are used while uploads are disabledThe request receives 400 Unsupported content typeEnable multipart uploads only for integrations that support form data
The multipart map names a missing file or invalid pathFile binding fails with a 400 errorKeep the form file names and map paths consistent
A batch exceeds the schema limit or batching is disabledThe request receives 400 and no batch resultConfigure batching and its maximum operation count for clients that batch
A stream protocol is configured without its transport mappingThe request cannot negotiate the intended stream format and may use the wrong response pathKeep configured protocol names and the shared transport map in agreement
A sync client requests SSE or multipart subscription outputThe request receives a sync-not-supported 400 errorUse an async integration for live HTTP streams
A cross-origin browser opens a WebSocket without an allowed originThe handshake is rejected with a policy violation before application hooks runAdd only trusted origins to allowed_origins or provide a stricter custom policy
An allowed origin includes a path, query, fragment, or invalid hostASGI setup fails instead of silently using a broad or wrong origin rulePass * or an origin with a scheme, host, and optional port only
An SSE-enabled view advertises SSE during a WebSocket handshakeWebSocket clients may reject the handshake or select an invalid protocolAdvertise only the supported WebSocket protocol names
A stream source fails before completionThe stream can end without a valid completion sequenceKeep source errors and cancellation inside the streaming wrapper and inspect adapter disconnect behavior

Key files

FileWhy to read it
strawberry/…/base.pyShared method checks, JSON helpers, IDE detection, configured stream transport lookup, and batch validation
strawberry/…/__init__.pyCommon request and response data shapes
strawberry/…/sync_base_view.pyComplete sync request lifecycle and upload parsing
strawberry/…/async_base_view.pyAsync request lifecycle, streaming, incremental results, subscription handoff, origin checks, and WebSocket protocol selection
strawberry/…/websocket_origin.pyOrigin normalization and same-origin comparison used to protect WebSocket handshakes
strawberry/…/streaming.pySSE and multipart framing, headers, heartbeats, and completion behavior
strawberry/…/ides.pyGraphQL IDE choices and static page selection
strawberry/…/utils.pyMultipart file placement into GraphQL variables
strawberry/…/__init__.pyASGI HTTP and WebSocket binding to the shared async view
strawberry/…/http_handler.pyExample of an adapter that binds shared views to request parsing and response sending