Skip to main content
Version: 0.22

ASGI application

Ariadne provides a GraphQL class that implements a production-ready ASGI application.

Using with an ASGI server

First create an application instance pointing it to the schema to serve:

# in myasgi.py
import os

from ariadne import make_executable_schema
from ariadne.asgi import GraphQL
from mygraphql import type_defs, resolvers

schema = make_executable_schema(type_defs, resolvers)
application = GraphQL(schema)

Then point an ASGI server such as uvicorn at the above instance.

Example using uvicorn:

$ uvicorn myasgi:application

Configuration options

See the reference.

The request instance

The ASGI application creates its own request object, an instance of the Request class from the Starlette. It's scope and receive attributes are populated from the received request.

When writing the ASGI middleware, remember to rely on the request.scope dict for storing additional data on the request object, instead of mutating the request object directly (like it's done in Django). For example:

# This is wrong
request.app_data

# This is correct
request.scope["app_data"]

Customizing JSON responses

Ariadne's ASGI application uses Starlette's JSONResponse for its JSON responses.

You can customize response creation logic by implementing a custom HTTP handler strategy for your ASGI GraphQL app.

To star, create a custom class extending the GraphQLHTTPHandler from ariadne.asgi.handlers package:

from ariadne.asgi.handlers import GraphQLHTTPHandler


class CustomGraphQLHTTPHandler(GraphQLHTTPHandler):
pass

Next, implement a customized version of the create_json_response method:

import json
from http import HTTPStatus

from ariadne.asgi.handlers import GraphQLHTTPHandler
from starlette.requests import Request
from starlette.responses import Response


class CustomGraphQLHTTPHandler(GraphQLHTTPHandler):
async def create_json_response(
self,
request: Request, # pylint: disable=unused-argument
result: dict,
success: bool,
) -> Response:
status_code = HTTPStatus.OK if success else HTTPStatus.BAD_REQUEST
content = json.dumps(
result,
ensure_ascii=False,
allow_nan=False,
indent=None,
separators=(",", ":"),
).encode("utf-8")

return Response(
content,
status_code=status_code,
media_type="application/json"
)

Finally, update the GraphQL instance used in your project to use the CustomGraphQLHTTPHandler:

from ariadne.asgi import GraphQL

# Rest of code...

app = GraphQL(
schema,
http_handler=CustomGraphQLHTTPHandler(),
)

Your GraphQL will now use the CustomGraphQLHTTPHandler strategy that we've just implemented to create JSON responses.

Note: the GraphQLHTTPHandler class implements many other methods that can be customized through inheritance.

See the API reference for a completed list.