ASGI app

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

Customizing context or root

GraphQL defines two methods that you can redefine in inheriting classes:

GraphQL.root_value_for_document(query, variables)
Parameters:
  • queryDocumentNode representing the query sent by the client.
  • variables – an optional dict representing the query variables.
Returns:

value that should be passed to root resolvers as the parent (first argument).

GraphQL.context_for_request(request)
Parameters:request – either a Request sent by the client or a message sent over a WebSocket.
Returns:value that should be passed to resolvers as context attribute on the info argument.

The following example shows custom a GraphQL server that defines its own root and context:

from ariadne.asgi import GraphQL:
from . import DataLoader, MyContext


class MyGraphQL(GraphQL):
    def root_value_for_document(self, query, variables):
        return DataLoader()

    def context_for_request(self, request):
        return MyContext(request)