docs/guides/query-batching.md
Query batching is a feature in Strawberry GraphQL that allows clients to send multiple queries, mutations, or a combination of both in a single HTTP request. This can help optimize network usage and improve performance for applications that make frequent GraphQL requests.
This document explains how to enable query batching, its configuration options, and how to integrate it into your application with an example using FastAPI.
To enable query batching in Strawberry, you need to configure the
StrawberryConfig when defining your GraphQL schema. The batching configuration
is provided as a typed dictionary. Batching is disabled by default, if no
configuration is provided.
from strawberry.schema.config import StrawberryConfig
config = StrawberryConfig(batching_config={"max_operations": 10})
To set a limit on the number of operations in a batch request, use the
max_operations key:
from strawberry.schema.config import StrawberryConfig
config = StrawberryConfig(batching_config={"max_operations": 5})
When batching is enabled, the server can handle a list of operations (queries/mutations) in a single request and return a list of responses.
Query Batching is supported on all Strawberry GraphQL framework integrations. Below is an example of how to enable query batching in a FastAPI application:
import strawberry
from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter
from strawberry.schema.config import StrawberryConfig
@strawberry.type
class Query:
@strawberry.field
def hello(self) -> str:
return "Hello World"
schema = strawberry.Schema(
Query,
config=StrawberryConfig(batching_config={"max_operations": 5}),
)
graphql_app = GraphQLRouter(schema)
app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")
app.py).uvicorn app:app --reload
http://127.0.0.1:8000/graphql.You can test query batching by sending a single HTTP request with multiple GraphQL operations. For example:
curl -X POST -H "Content-Type: application/json" \
-d '[{"query": "{ hello }"}, {"query": "{ hello }"}]' \
http://127.0.0.1:8000/graphql
[{ "data": { "hello": "Hello World" } }, { "data": { "hello": "Hello World" } }]
If batching is not enabled in the server configuration and a batch request is sent, the server will respond with a 400 status code and an error message:
Batching is not enabled
If the number of operations in a batch exceeds the max_operations limit, the
server will return a 400 status code and an error message:
Too many operations
Query batching does not support multipart subscriptions. Attempting to batch such operations will result in a 400 error with a relevant message.
Query batching is particularly useful for clients that need to perform multiple operations simultaneously, reducing the overhead of multiple HTTP requests. Ensure your client library supports query batching before enabling it on the server.