Skip to content

Commit e2639f0

Browse files
committed
feat: add support for jinja graphigl render for flask, quart and webob
1 parent 559d4ae commit e2639f0

13 files changed

+123
-118
lines changed

graphql_server/flask/graphqlview.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class GraphQLView(View):
3939
validation_rules = None
4040
execution_context_class = None
4141
batch = False
42+
jinja_env = None
4243
subscriptions = None
4344
headers = None
4445
default_query = None
@@ -131,7 +132,7 @@ def dispatch_request(self):
131132
graphiql_version=self.graphiql_version,
132133
graphiql_template=self.graphiql_template,
133134
graphiql_html_title=self.graphiql_html_title,
134-
jinja_env=None,
135+
jinja_env=self.jinja_env,
135136
)
136137
graphiql_options = GraphiQLOptions(
137138
default_query=self.default_query,

graphql_server/quart/graphqlview.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class GraphQLView(View):
3939
validation_rules = None
4040
execution_context_class = None
4141
batch = False
42+
jinja_env = None
4243
enable_async = False
4344
subscriptions = None
4445
headers = None
@@ -140,7 +141,7 @@ async def dispatch_request(self):
140141
graphiql_version=self.graphiql_version,
141142
graphiql_template=self.graphiql_template,
142143
graphiql_html_title=self.graphiql_html_title,
143-
jinja_env=None,
144+
jinja_env=self.jinja_env,
144145
)
145146
graphiql_options = GraphiQLOptions(
146147
default_query=self.default_query,

graphql_server/webob/graphqlview.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class GraphQLView:
3838
validation_rules = None
3939
execution_context_class = None
4040
batch = False
41+
jinja_env = None
4142
enable_async = False
4243
subscriptions = None
4344
headers = None
@@ -133,7 +134,7 @@ def dispatch_request(self, request):
133134
graphiql_version=self.graphiql_version,
134135
graphiql_template=self.graphiql_template,
135136
graphiql_html_title=self.graphiql_html_title,
136-
jinja_env=None,
137+
jinja_env=self.jinja_env,
137138
)
138139
graphiql_options = GraphiQLOptions(
139140
default_query=self.default_query,

tests/flask/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
3+
from .app import create_app
4+
5+
6+
@pytest.fixture
7+
def app():
8+
# import app factory pattern
9+
app = create_app()
10+
11+
# pushes an application context manually
12+
ctx = app.app_context()
13+
ctx.push()
14+
return app
15+
16+
17+
@pytest.fixture
18+
def client(app):
19+
return app.test_client()

tests/flask/test_graphiqlview.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
import pytest
22
from flask import url_for
3+
from jinja2 import Environment
34

45
from .app import create_app
56

67

7-
@pytest.fixture
8-
def app():
9-
# import app factory pattern
10-
app = create_app(graphiql=True)
11-
12-
# pushes an application context manually
13-
ctx = app.app_context()
14-
ctx.push()
15-
return app
16-
17-
18-
@pytest.fixture
19-
def client(app):
20-
return app.test_client()
21-
22-
8+
@pytest.mark.parametrize(
9+
"app",
10+
[create_app(graphiql=True), create_app(graphiql=True, jinja_env=Environment())],
11+
)
2312
def test_graphiql_is_enabled(app, client):
2413
with app.test_request_context():
2514
response = client.get(
@@ -28,6 +17,10 @@ def test_graphiql_is_enabled(app, client):
2817
assert response.status_code == 200
2918

3019

20+
@pytest.mark.parametrize(
21+
"app",
22+
[create_app(graphiql=True), create_app(graphiql=True, jinja_env=Environment())],
23+
)
3124
def test_graphiql_renders_pretty(app, client):
3225
with app.test_request_context():
3326
response = client.get(
@@ -45,14 +38,24 @@ def test_graphiql_renders_pretty(app, client):
4538
assert pretty_response in response.data.decode("utf-8")
4639

4740

41+
@pytest.mark.parametrize(
42+
"app",
43+
[create_app(graphiql=True), create_app(graphiql=True, jinja_env=Environment())],
44+
)
4845
def test_graphiql_default_title(app, client):
4946
with app.test_request_context():
5047
response = client.get(url_for("graphql"), headers={"Accept": "text/html"})
5148
assert "<title>GraphiQL</title>" in response.data.decode("utf-8")
5249

5350

5451
@pytest.mark.parametrize(
55-
"app", [create_app(graphiql=True, graphiql_html_title="Awesome")]
52+
"app",
53+
[
54+
create_app(graphiql=True, graphiql_html_title="Awesome"),
55+
create_app(
56+
graphiql=True, graphiql_html_title="Awesome", jinja_env=Environment()
57+
),
58+
],
5659
)
5760
def test_graphiql_custom_title(app, client):
5861
with app.test_request_context():

tests/flask/test_graphqlview.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,11 @@
99
from .app import create_app
1010

1111

12-
@pytest.fixture
13-
def app():
14-
# import app factory pattern
15-
app = create_app()
16-
17-
# pushes an application context manually
18-
ctx = app.app_context()
19-
ctx.push()
20-
return app
21-
22-
23-
@pytest.fixture
24-
def client(app):
25-
return app.test_client()
26-
27-
2812
def url_string(app, **url_params):
2913
with app.test_request_context():
30-
string = url_for("graphql")
31-
32-
if url_params:
33-
string += "?" + urlencode(url_params)
14+
url = url_for("graphql")
3415

35-
return string
16+
return f"{url}?{urlencode(url_params)}" if url_params else url
3617

3718

3819
def response_json(response):

tests/quart/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
import pytest
2+
from quart import Quart
13
from quart.typing import TestClientProtocol
24

5+
from .app import create_app
6+
37
TestClientProtocol.__test__ = False # type: ignore
8+
9+
10+
@pytest.fixture
11+
def app() -> Quart:
12+
# import app factory pattern
13+
app = create_app()
14+
15+
# pushes an application context manually
16+
# ctx = app.app_context()
17+
# await ctx.push()
18+
return app
19+
20+
21+
@pytest.fixture
22+
def client(app: Quart) -> TestClientProtocol:
23+
return app.test_client()

tests/quart/test_graphiqlview.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
11
from typing import Optional
22

33
import pytest
4+
from jinja2 import Environment
45
from quart import Quart, Response, url_for
56
from quart.typing import TestClientProtocol
67
from werkzeug.datastructures import Headers
78

89
from .app import create_app
910

1011

11-
@pytest.fixture
12-
def app() -> Quart:
13-
# import app factory pattern
14-
app = create_app(graphiql=True)
15-
16-
# pushes an application context manually
17-
# ctx = app.app_context()
18-
# await ctx.push()
19-
return app
20-
21-
22-
@pytest.fixture
23-
def client(app: Quart) -> TestClientProtocol:
24-
return app.test_client()
25-
26-
2712
@pytest.mark.asyncio
2813
async def execute_client(
2914
app: Quart,
@@ -39,6 +24,10 @@ async def execute_client(
3924

4025

4126
@pytest.mark.asyncio
27+
@pytest.mark.parametrize(
28+
"app",
29+
[create_app(graphiql=True), create_app(graphiql=True, jinja_env=Environment())],
30+
)
4231
async def test_graphiql_is_enabled(app: Quart, client: TestClientProtocol):
4332
response = await execute_client(
4433
app, client, headers=Headers({"Accept": "text/html"}), externals=False
@@ -47,6 +36,10 @@ async def test_graphiql_is_enabled(app: Quart, client: TestClientProtocol):
4736

4837

4938
@pytest.mark.asyncio
39+
@pytest.mark.parametrize(
40+
"app",
41+
[create_app(graphiql=True), create_app(graphiql=True, jinja_env=Environment())],
42+
)
5043
async def test_graphiql_renders_pretty(app: Quart, client: TestClientProtocol):
5144
response = await execute_client(
5245
app, client, headers=Headers({"Accept": "text/html"}), query="{test}"
@@ -64,6 +57,10 @@ async def test_graphiql_renders_pretty(app: Quart, client: TestClientProtocol):
6457

6558

6659
@pytest.mark.asyncio
60+
@pytest.mark.parametrize(
61+
"app",
62+
[create_app(graphiql=True), create_app(graphiql=True, jinja_env=Environment())],
63+
)
6764
async def test_graphiql_default_title(app: Quart, client: TestClientProtocol):
6865
response = await execute_client(
6966
app, client, headers=Headers({"Accept": "text/html"})
@@ -74,7 +71,13 @@ async def test_graphiql_default_title(app: Quart, client: TestClientProtocol):
7471

7572
@pytest.mark.asyncio
7673
@pytest.mark.parametrize(
77-
"app", [create_app(graphiql=True, graphiql_html_title="Awesome")]
74+
"app",
75+
[
76+
create_app(graphiql=True, graphiql_html_title="Awesome"),
77+
create_app(
78+
graphiql=True, graphiql_html_title="Awesome", jinja_env=Environment()
79+
),
80+
],
7881
)
7982
async def test_graphiql_custom_title(app: Quart, client: TestClientProtocol):
8083
response = await execute_client(

tests/quart/test_graphqlview.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@
1111
from .app import create_app
1212

1313

14-
@pytest.fixture
15-
def app() -> Quart:
16-
# import app factory pattern
17-
app = create_app(graphiql=True)
18-
19-
# pushes an application context manually
20-
# ctx = app.app_context()
21-
# await ctx.push()
22-
return app
23-
24-
25-
@pytest.fixture
26-
def client(app: Quart) -> TestClientProtocol:
27-
return app.test_client()
28-
29-
3014
@pytest.mark.asyncio
3115
async def execute_client(
3216
app: Quart,

tests/webob/app.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
from webob import Request
44

55
from graphql_server.webob import GraphQLView
6-
from tests.webob.schema import Schema
76

7+
from .schema import Schema
88

9-
def url_string(**url_params):
10-
string = "/graphql"
119

12-
if url_params:
13-
string += "?" + urlencode(url_params)
14-
15-
return string
10+
def url_string(url="/graphql", **url_params):
11+
return f"{url}?{urlencode(url_params)}" if url_params else url
1612

1713

1814
class Client(object):

tests/webob/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
3+
from .app import Client
4+
5+
6+
@pytest.fixture
7+
def settings():
8+
return {}
9+
10+
11+
@pytest.fixture
12+
def client(settings):
13+
return Client(settings=settings)

tests/webob/test_graphiqlview.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
11
import pytest
2+
from jinja2 import Environment
23

3-
from .app import Client, url_string
4+
from .app import url_string
45

56

6-
@pytest.fixture
7-
def settings():
8-
return {}
9-
10-
11-
@pytest.fixture
12-
def client(settings):
13-
return Client(settings=settings)
7+
@pytest.mark.parametrize(
8+
"settings", [dict(graphiql=True), dict(graphiql=True, jinja_env=Environment())]
9+
)
10+
def test_graphiql_is_enabled(client):
11+
response = client.get(url_string(query="{test}"), headers={"Accept": "text/html"})
12+
assert response.status_code == 200
1413

1514

16-
@pytest.fixture
17-
def pretty_response():
18-
return (
15+
@pytest.mark.parametrize(
16+
"settings", [dict(graphiql=True), dict(graphiql=True, jinja_env=Environment())]
17+
)
18+
def test_graphiql_simple_renderer(client):
19+
response = client.get(url_string(query="{test}"), headers={"Accept": "text/html"})
20+
assert response.status_code == 200
21+
pretty_response = (
1922
"{\n"
2023
' "data": {\n'
2124
' "test": "Hello World"\n'
2225
" }\n"
2326
"}".replace('"', '\\"').replace("\n", "\\n")
2427
)
25-
26-
27-
@pytest.mark.parametrize("settings", [dict(graphiql=True)])
28-
def test_graphiql_is_enabled(client, settings):
29-
response = client.get(url_string(query="{test}"), headers={"Accept": "text/html"})
30-
assert response.status_code == 200
31-
32-
33-
@pytest.mark.parametrize("settings", [dict(graphiql=True)])
34-
def test_graphiql_simple_renderer(client, settings, pretty_response):
35-
response = client.get(url_string(query="{test}"), headers={"Accept": "text/html"})
36-
assert response.status_code == 200
3728
assert pretty_response in response.body.decode("utf-8")
3829

3930

40-
@pytest.mark.parametrize("settings", [dict(graphiql=True)])
41-
def test_graphiql_html_is_not_accepted(client, settings):
31+
@pytest.mark.parametrize(
32+
"settings", [dict(graphiql=True), dict(graphiql=True, jinja_env=Environment())]
33+
)
34+
def test_graphiql_html_is_not_accepted(client):
4235
response = client.get(url_string(), headers={"Accept": "application/json"})
4336
assert response.status_code == 400

0 commit comments

Comments
 (0)