Skip to content

Commit

Permalink
Add support for enabling subscriptions in GraphiQL on Django through …
Browse files Browse the repository at this point in the history
…a flag on the BaseView class
  • Loading branch information
lijok committed Sep 6, 2021
1 parent 503d906 commit a41ebb2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -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))]
```
6 changes: 4 additions & 2 deletions strawberry/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit a41ebb2

Please sign in to comment.