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

[3276] Fix incorrect template rendering #3279

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: patch

This release addresses an issue in Django's GraphQLView. Previously, variables were consumed too early when overriding the template, leading to a syntax error in the JavaScript code.
6 changes: 3 additions & 3 deletions strawberry/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from django.http.response import HttpResponse
from django.template import RequestContext, Template
from django.template.exceptions import TemplateDoesNotExist
from django.template.loader import render_to_string
from django.template.loader import get_template
from django.template.response import TemplateResponse
from django.utils.decorators import classonlymethod, method_decorator
from django.views.decorators.csrf import csrf_exempt
Expand Down Expand Up @@ -218,7 +218,7 @@ def dispatch(

def render_graphql_ide(self, request: HttpRequest) -> HttpResponse:
try:
template = Template(render_to_string("graphql/graphiql.html"))
template = get_template("graphql/graphiql.html").template
except TemplateDoesNotExist:
template = Template(self.graphql_ide_html)

Expand Down Expand Up @@ -277,7 +277,7 @@ async def dispatch( # pyright: ignore

async def render_graphql_ide(self, request: HttpRequest) -> HttpResponse:
try:
template = Template(render_to_string("graphql/graphiql.html"))
template = get_template("graphql/graphiql.html").template
except TemplateDoesNotExist:
template = Template(self.graphql_ide_html)

Expand Down
1 change: 1 addition & 0 deletions tests/django/templates/graphql/graphiql.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script>JSON.parse("{{ SUBSCRIPTION_ENABLED }}")</script>
28 changes: 28 additions & 0 deletions tests/django/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from pathlib import Path

from django.http import HttpResponse
from django.test import Client, override_settings

BASE_DIR = Path(__file__).parent
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": True,
}
]


def run_sync_view():
client = Client()
response: HttpResponse = client.get("/graphql/", HTTP_ACCEPT="text/html")
assert 'JSON.parse("false")' in response.content.decode()


def test_render_graphiql_template():
run_sync_view()


@override_settings(TEMPLATES=TEMPLATES)
def test_subscription_enabled_not_empty():
run_sync_view()