Subscriptions¶
The graphene-django
project does not currently support GraphQL subscriptions out of the box. However, there are
several community-driven modules for adding subscription support, and the provided GraphiQL interface supports
running subscription operations over a websocket.
To implement websocket-based support for GraphQL subscriptions, you’ll need to do the following:
- Install and configure django-channels.
- Install and configure* a third-party module for adding subscription support over websockets. A few options include:
- Ensure that your application (or at least your GraphQL endpoint) is being served via an ASGI protocol server like
daphne (built in to
django-channels
), uvicorn, or hypercorn.
* Note: By default, the GraphiQL interface that comes with
graphene-django
assumes that you are handling subscriptions at the same path as any other operation (i.e., you configured bothurls.py
androuting.py
to handle GraphQL operations at the same path, like/graphql
).If these URLs differ, GraphiQL will try to run your subscription over HTTP, which will produce an error. If you need to use a different URL for handling websocket connections, you can configure
SUBSCRIPTION_PATH
in yoursettings.py
:GRAPHENE = { # ... "SUBSCRIPTION_PATH": "/ws/graphql" # The path you configured in `routing.py`, including a leading slash. }
Once your application is properly configured to handle subscriptions, you can use the GraphiQL interface to test subscriptions like any other operation.