Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enabling subscriptions in GraphiQL on Django through a flag on the BaseView class #1215

Merged
merged 1 commit into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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