From a41ebb28dd71b1c57f783fac9929ea57326d103c Mon Sep 17 00:00:00 2001 From: lijok <28689084+lijok@users.noreply.github.com> Date: Mon, 6 Sep 2021 17:15:26 +0100 Subject: [PATCH] Add support for enabling subscriptions in GraphiQL on Django through a flag on the BaseView class --- RELEASE.md | 11 +++++++++++ strawberry/django/views.py | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..82f565aede --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,11 @@ +Release type: minor + +This release adds support for enabling subscriptions in GraphiQL +on Django by setting a flag `subscriptions_enabled` on the BaseView class. +```python +from strawberry.django.views import AsyncGraphQLView + +from .schema import schema + +urlpatterns = [path("graphql", AsyncGraphQLView.as_view(schema=schema, graphiql=True, subscriptions_enabled=True))] +``` diff --git a/strawberry/django/views.py b/strawberry/django/views.py index 1ba546c4e7..2c0319685d 100644 --- a/strawberry/django/views.py +++ b/strawberry/django/views.py @@ -37,12 +37,14 @@ def __init__(self) -> None: class BaseView(View): + subscriptions_enabled = False graphiql = True schema: Optional[BaseSchema] = None - def __init__(self, schema: BaseSchema, graphiql=True): + def __init__(self, schema: BaseSchema, graphiql=True, subscriptions_enabled=False): self.schema = schema self.graphiql = graphiql + self.subscriptions_enabled = subscriptions_enabled def parse_body(self, request) -> Dict[str, Any]: if request.content_type.startswith("multipart/form-data"): @@ -92,7 +94,7 @@ def _render_graphiql(self, request: HttpRequest, context=None): ) context = context or {} - context.update({"SUBSCRIPTION_ENABLED": "false"}) + context.update({"SUBSCRIPTION_ENABLED": json.dumps(self.subscriptions_enabled)}) response = TemplateResponse(request=request, template=None, context=context) response.content = template.render(RequestContext(request, context))