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 / web-framework-integration

Web framework integration

Strawberry gives Python web applications one GraphQL serving model across sync views, async views, and ASGI apps. The shared layer turns framework requests into schema operations and turns schema results into framework responses, while small adapters connect that model to each web framework. See HTTP and ASGI transport for the wire rules and framework adapters for the framework boundary.

Shared serving layer

The shared serving layer owns the behavior that should remain the same in every integration. It accepts GraphQL requests, builds the execution inputs, runs the schema, and formats normal, incremental, or streamed results. A framework adapter supplies the request and response types that the host application uses.

HTTP views

HTTP views support GraphQL over GET and POST. They read query parameters or request bodies, support JSON input, and can support multipart file uploads when enabled. A view can also serve a GraphQL IDE when a browser requests the endpoint without a query. The sync path executes one operation or a permitted batch and returns one JSON response for each result. It rejects streaming subscription requests because a sync view cannot produce an async stream. Batching must be enabled by the schema and stay within its operation limit.

References: strawberry/…/base.py• L33-83 strawberry/…/base.py• L133-146 strawberry/…/sync_base_view.py• L65-108 strawberry/…/sync_base_view.py• L154-223

ASGI applications

The ASGI layer handles both HTTP and WebSocket scopes. HTTP operations use the same request rules as other async views, while WebSocket connections check the request origin and negotiate a supported GraphQL subscription protocol before Strawberry starts subscription handling. Async HTTP views can return streamed subscription data and incremental results through the selected HTTP transport.

References: strawberry/…/__init__.py• L89-146 strawberry/…/async_base_view.py• L188-250 strawberry/…/async_base_view.py• L387-450

WebSocket origin policy

The WebSocket origin policy protects browser sessions from unwanted cross-site connections. Same-origin requests and clients without an Origin header are allowed, while cross-origin browser requests must match a configured allowed origin or the wildcard; the check runs before application hooks and the handshake is rejected when it fails.

Allowed origins are compared in a normalized form. The policy accepts an origin with its scheme, host, and optional port, but not a path or query string, so configuration errors fail early and equivalent browser origin forms match consistently.

References: strawberry/…/async_base_view.py• L188-236 strawberry/…/async_base_view.py• L387-392 strawberry/…/websocket_origin.py• L1-7 strawberry/…/websocket_origin.py• L25-83

Request and response flow

Every integration follows the same broad path even when its public entry point looks different. The host framework first supplies a native request, then Strawberry adds the framework context around schema execution, and finally the adapter turns the result back into a native response.

This diagram shows how a framework request becomes a schema result and then a framework response.

flowchart LR
    A[Framework request] --> B[Request adapter]
    B --> C[GraphQL request data]
    C --> D[Schema execution]
    D --> E{Result type}
    E -->|JSON result| F[Response formatter]
    E -->|Subscription or incremental result| G[HTTP stream or WebSocket protocol]
    F --> H[Framework response]
    G --> H

Request normalization

A request adapter exposes the small set of values that the shared layer needs: method, headers, query parameters, content type, body, and form data. This keeps framework-specific request objects out of GraphQL parsing and lets the same rules apply to all integrations. GET carries its operation in query parameters. POST normally carries JSON or an enabled multipart form.

References: strawberry/…/base.py• L22-31 strawberry/…/async_base_view.py• L650-706 strawberry/…/sync_base_view.py• L145-223 strawberry/…/http_handler.py• L58-117

Context and response state

The integration creates a context for each operation. The context normally exposes the native request and a response object or response state, so resolvers and application code can use request data and add response effects such as headers, cookies, status changes, or background work. FastAPI and Litestar also connect this step to their dependency systems, which lets an application provide custom context and root values.

References: strawberry/…/__init__.py• L148-164 strawberry/…/router.py• L68-119 strawberry/…/controller.py• L108-148 strawberry/…/views.py• L151-158 strawberry/…/http_handler.py• L308-317

Result delivery

Normal results use a JSON response with data, optional errors, and optional extensions. Incremental results use the multipart stream contract and carry progress fields such as hasNext, pending, completed, and incremental instead of being reduced to one JSON result. Async integrations can also deliver subscription events through a stream or WebSocket. The shared layer adds the transport headers and heartbeat behavior, while the framework adapter chooses the native response object and copies application response state into it. Channels sends ordinary results as one response and multipart results as a chunked body; Django uses a native streaming response for async HTTP streams.

References: strawberry/…/__init__.py• L13-51 strawberry/…/async_base_view.py• L470-523 strawberry/…/async_base_view.py• L533-573 strawberry/…/streaming.py• L211-282 strawberry/…/http_handler.py• L227-268 strawberry/…/views.py• L96-133

Framework entry points

Strawberry exposes a native entry point for each supported framework. The entry point is shaped like the framework expects: a router, controller, class-based view, consumer, or generic ASGI callable. The GraphQL behavior stays in the shared serving layer, so the main choice is how the endpoint fits into the host application's routing and request context.

Framework coverage

The table shows the main public entry point and the transport scope that each integration provides.

FrameworkPublic entry pointHTTPWebSocket or HTTP streaming
Generic ASGI / Starlettestrawberry.asgi.GraphQLYesWebSocket and streaming
FastAPIstrawberry.fastapi.GraphQLRouterYesWebSocket and streaming
Litestarstrawberry.litestar.make_graphql_controllerYesWebSocket and streaming
Djangostrawberry.django.GraphQLView or AsyncGraphQLViewYesNo WebSocket in the views
Django ChannelsGraphQLProtocolTypeRouter, GraphQLHTTPConsumer, and GraphQLWSConsumerYesWebSocket and async streaming
FlaskGraphQLView or AsyncGraphQLViewYesNo WebSocket
Quartstrawberry.quart.GraphQLViewYesWebSocket and streaming
Sanicstrawberry.sanic.GraphQLViewYesHTTP streaming
Aiohttpstrawberry.aiohttp.GraphQLViewYesWebSocket and streaming
Chalicestrawberry.chalice.GraphQLViewYesNo WebSocket or sync streaming

References: strawberry/…/__init__.py• L90-229 strawberry/…/router.py• L58-66 strawberry/…/controller.py• L193-215 strawberry/…/views.py• L139-252 strawberry/…/__init__.py• L1-16 strawberry/…/http_handler.py• L201-413 strawberry/…/views.py• L62-137 strawberry/…/views.py• L72-177 strawberry/…/views.py• L29-167 strawberry/…/views.py• L73-194 strawberry/…/views.py• L18-81

Framework boundary

Each adapter maps four host concerns into the shared model: request reading, context creation, response creation, and optional WebSocket behavior. The adapter also catches shared HTTP errors and expresses them as the framework's normal error response. This boundary lets application middleware, headers, cookies, status codes, and background work remain part of the host framework instead of becoming GraphQL-specific state.

References: strawberry/…/sync_base_view.py• L40-63 strawberry/…/async_base_view.py• L120-186 strawberry/…/views.py• L96-133 strawberry/…/http_handler.py• L227-268 strawberry/…/router.py• L196-254 strawberry/…/views.py• L139-191

Transport choices

The serving layer separates the GraphQL operation from the way its result travels to the client. Plain HTTP is the default for queries and mutations. Async integrations add WebSocket subscriptions and can use server-sent events or multipart HTTP streams for long-lived subscription results.

HTTP and ASGI transport

Transport selection is based on the request method and the relevant Accept or Content-Type value. WebSocket clients select one of the configured GraphQL WebSocket subprotocols during connection setup; non-WebSocket transport names are not offered in that handshake. HTTP streaming clients select SSE or multipart subscription transport, and the selected transport controls event framing, completion, and heartbeat messages. The shared request data records the selected protocol so execution and response delivery use the same transport choice.

Read HTTP and ASGI transport for the request formats, protocol rules, streaming behavior, and failure rules shared by all adapters.

References: strawberry/…/__init__.py• L40-51 strawberry/…/base.py• L89-131 strawberry/…/async_base_view.py• L238-250 strawberry/…/async_base_view.py• L282-302 strawberry/…/async_base_view.py• L650-706 strawberry/…/streaming.py• L73-205 strawberry/…/__init__.py• L1-12

Framework-specific adapters

Framework-specific adapters are the integration layer for native request, response, routing, dependency, and WebSocket APIs. They do not define a separate GraphQL execution model. They select the shared sync or async view and provide the host behavior needed to expose it at an application route.

Read framework adapters for the adapter responsibilities and framework-level integration choices.

References: strawberry/…/sync_base_view.py• L31-38 strawberry/…/async_base_view.py• L84-186 strawberry/…/router.py• L26-87 strawberry/…/http_handler.py• L120-225

Change checklist

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

  • Changes to AsyncBaseHTTPView can affect plain HTTP, HTTP streaming, and WebSocket handling at the same time. Check each response path because they use different host response types.
  • Changes to GraphQLHTTPResponse, GraphQLRequestData, or the protocol values in strawberry/…/__init__.py must remain compatible with result processing, transport selection, every request adapter, and every framework response builder.
  • Changes to incremental result fields must preserve the client contract for initial data, pending work, completed work, subsequent data, and the hasNext state.
  • Changes to the configured subscription protocol list must keep WebSocket negotiation and HTTP stream selection aligned. Non-WebSocket protocols must not be advertised in a WebSocket handshake.
  • Changes to WebSocket origin checks or allowed-origin normalization must preserve same-origin access, trusted cross-origin access, and rejection before application hooks.
  • Changes to context creation must preserve both the framework-native request and the response state that application code can modify.
  • Changes to GraphQLProtocolTypeRouter must preserve separate routing for HTTP and WebSocket scopes, including the fallback route for non-GraphQL Django traffic.
  • Changes to a framework response builder must preserve status, headers, cookies, and stream framing when the shared view returns normal or streaming results.

Important failure modes

TriggerConsequenceGuard
A client sends a method other than GET or POSTThe endpoint returns an HTTP 405 error instead of executing the operationKeep endpoint routing and client use within the shared method contract
A client sends malformed JSON, invalid variables, or an unsupported content typeThe endpoint returns an HTTP 400 error before schema executionUse the supported query, JSON, and enabled multipart request shapes
A sync view receives an SSE or multipart subscription requestThe endpoint rejects the request because it cannot produce a streaming responseUse an async integration for streaming subscriptions
A client requests a WebSocket protocol that the endpoint does not supportThe WebSocket handshake is not accepted for a GraphQL protocolConfigure the same supported protocol set in the client and endpoint
A browser opens a WebSocket from an untrusted cross-origin pageThe handshake is rejected before GraphQL or application hooks runConfigure only trusted origins in allowed_origins or use the default same-origin policy
A batch includes a streaming protocol or exceeds the schema batch limitThe request is rejected instead of returning a mixed batch resultSend streaming subscriptions as single operations and keep batches within schema limits
A client requests HTTP streaming but the selected protocol is not enabled by the viewThe endpoint cannot create the requested stream transportEnable the matching subscription protocol and send the matching Accept value
A Channels deployment omits the non-GraphQL fallback routeOther Django HTTP routes are sent to no suitable applicationInclude the Django ASGI application after the GraphQL route

Key files

FileWhy to read it
strawberry/…/__init__.pyDefines the shared request protocol values and the JSON result shape for normal and incremental execution.
strawberry/…/base.pyDefines the common request checks, IDE behavior, stream transport selection, and batch limits shared by views.
strawberry/…/sync_base_view.pyShows the sync request-to-schema-to-response path and its limits.
strawberry/…/async_base_view.pyShows the async path for normal results, incremental results, HTTP streams, and WebSocket subscriptions.
strawberry/…/websocket_origin.pyDefines the origin normalization and same-origin checks used to protect WebSocket handshakes.
strawberry/…/__init__.pyShows the generic ASGI entry point and the native HTTP/WebSocket bridge.
strawberry/…/streaming.pyDefines the common SSE and multipart stream contracts and heartbeat behavior.
strawberry/…/router.pyShows how a framework router adds dependency injection around the shared async view.
strawberry/…/controller.pyShows the controller and dependency model for an async framework integration.
strawberry/…/views.pyShows both sync and async Django view entry points and their native response handling.
strawberry/…/router.pyShows how one ASGI application routes GraphQL HTTP and WebSocket traffic separately.
strawberry/…/http_handler.pyShows how Django Channels adapts request data and sends normal or multipart HTTP responses.
strawberry/…/views.pyShows the sync and async Flask view boundary.
strawberry/…/views.pyShows an async Flask-style integration with WebSocket support.
strawberry/…/views.pyShows an async HTTP integration with native streaming response handling.
strawberry/…/views.pyShows an async integration that supports both HTTP and WebSocket requests.
strawberry/…/views.pyShows the sync HTTP integration for an event-driven serverless framework.