Skip to content

Commit accfef4

Browse files
authored
refactor: graphiql template shared across servers (#49)
* refactor: graphiql template shared across servers * chore: add typing-extensions to setup py * feat: add headers and should persist headers props * chore: mypy issues * fix: pass config to webob render graphiql * refactor: pass arguments instead of spread * chore: add pytest-asyncio and bump pytest dep
1 parent eaf75e6 commit accfef4

12 files changed

+448
-767
lines changed

graphql_server/aiohttp/graphqlview.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import copy
22
from collections.abc import MutableMapping
33
from functools import partial
4+
from typing import List
45

56
from aiohttp import web
67
from graphql import GraphQLError
78
from graphql.type.schema import GraphQLSchema
89

910
from graphql_server import (
11+
GraphQLParams,
1012
HttpQueryError,
1113
encode_execution_results,
1214
format_error_default,
1315
json_encode,
1416
load_json_body,
1517
run_http_query,
1618
)
17-
18-
from .render_graphiql import render_graphiql
19+
from graphql_server.render_graphiql import (
20+
GraphiQLConfig,
21+
GraphiQLData,
22+
render_graphiql_async,
23+
)
1924

2025

2126
class GraphQLView:
@@ -26,12 +31,14 @@ class GraphQLView:
2631
graphiql = False
2732
graphiql_version = None
2833
graphiql_template = None
34+
graphiql_html_title = None
2935
middleware = None
3036
batch = False
3137
jinja_env = None
3238
max_age = 86400
3339
enable_async = False
3440
subscriptions = None
41+
headers = None
3542

3643
accepted_methods = ["GET", "POST", "PUT", "DELETE"]
3744

@@ -88,16 +95,6 @@ async def parse_body(self, request):
8895

8996
return {}
9097

91-
def render_graphiql(self, params, result):
92-
return render_graphiql(
93-
jinja_env=self.jinja_env,
94-
params=params,
95-
result=result,
96-
graphiql_version=self.graphiql_version,
97-
graphiql_template=self.graphiql_template,
98-
subscriptions=self.subscriptions,
99-
)
100-
10198
# TODO:
10299
# use this method to replace flask and sanic
103100
# checks as this is equivalent to `should_display_graphiql` and
@@ -135,6 +132,7 @@ async def __call__(self, request):
135132
if request_method == "options":
136133
return self.process_preflight(request)
137134

135+
all_params: List[GraphQLParams]
138136
execution_results, all_params = run_http_query(
139137
self.schema,
140138
request_method,
@@ -162,7 +160,24 @@ async def __call__(self, request):
162160
)
163161

164162
if is_graphiql:
165-
return await self.render_graphiql(params=all_params[0], result=result)
163+
graphiql_data = GraphiQLData(
164+
result=result,
165+
query=getattr(all_params[0], "query"),
166+
variables=getattr(all_params[0], "variables"),
167+
operation_name=getattr(all_params[0], "operation_name"),
168+
subscription_url=self.subscriptions,
169+
headers=self.headers,
170+
)
171+
graphiql_config = GraphiQLConfig(
172+
graphiql_version=self.graphiql_version,
173+
graphiql_template=self.graphiql_template,
174+
graphiql_html_title=self.graphiql_html_title,
175+
jinja_env=self.jinja_env,
176+
)
177+
source = await render_graphiql_async(
178+
data=graphiql_data, config=graphiql_config
179+
)
180+
return web.Response(text=source, content_type="text/html")
166181

167182
return web.Response(
168183
text=result, status=status_code, content_type="application/json",

graphql_server/aiohttp/render_graphiql.py

-208
This file was deleted.

graphql_server/flask/graphqlview.py

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
from functools import partial
2+
from typing import List
23

3-
from flask import Response, request
4+
from flask import Response, render_template_string, request
45
from flask.views import View
56
from graphql.error import GraphQLError
67
from graphql.type.schema import GraphQLSchema
78

89
from graphql_server import (
10+
GraphQLParams,
911
HttpQueryError,
1012
encode_execution_results,
1113
format_error_default,
1214
json_encode,
1315
load_json_body,
1416
run_http_query,
1517
)
16-
17-
from .render_graphiql import render_graphiql
18+
from graphql_server.render_graphiql import (
19+
GraphiQLConfig,
20+
GraphiQLData,
21+
render_graphiql_sync,
22+
)
1823

1924

2025
class GraphQLView(View):
@@ -27,6 +32,8 @@ class GraphQLView(View):
2732
graphiql_html_title = None
2833
middleware = None
2934
batch = False
35+
subscriptions = None
36+
headers = None
3037

3138
methods = ["GET", "POST", "PUT", "DELETE"]
3239

@@ -50,15 +57,6 @@ def get_context_value(self):
5057
def get_middleware(self):
5158
return self.middleware
5259

53-
def render_graphiql(self, params, result):
54-
return render_graphiql(
55-
params=params,
56-
result=result,
57-
graphiql_version=self.graphiql_version,
58-
graphiql_template=self.graphiql_template,
59-
graphiql_html_title=self.graphiql_html_title,
60-
)
61-
6260
format_error = staticmethod(format_error_default)
6361
encode = staticmethod(json_encode)
6462

@@ -72,6 +70,7 @@ def dispatch_request(self):
7270

7371
pretty = self.pretty or show_graphiql or request.args.get("pretty")
7472

73+
all_params: List[GraphQLParams]
7574
execution_results, all_params = run_http_query(
7675
self.schema,
7776
request_method,
@@ -88,11 +87,28 @@ def dispatch_request(self):
8887
execution_results,
8988
is_batch=isinstance(data, list),
9089
format_error=self.format_error,
91-
encode=partial(self.encode, pretty=pretty),
90+
encode=partial(self.encode, pretty=pretty), # noqa
9291
)
9392

9493
if show_graphiql:
95-
return self.render_graphiql(params=all_params[0], result=result)
94+
graphiql_data = GraphiQLData(
95+
result=result,
96+
query=getattr(all_params[0], "query"),
97+
variables=getattr(all_params[0], "variables"),
98+
operation_name=getattr(all_params[0], "operation_name"),
99+
subscription_url=self.subscriptions,
100+
headers=self.headers,
101+
)
102+
graphiql_config = GraphiQLConfig(
103+
graphiql_version=self.graphiql_version,
104+
graphiql_template=self.graphiql_template,
105+
graphiql_html_title=self.graphiql_html_title,
106+
jinja_env=None,
107+
)
108+
source = render_graphiql_sync(
109+
data=graphiql_data, config=graphiql_config
110+
)
111+
return render_template_string(source)
96112

97113
return Response(result, status=status_code, content_type="application/json")
98114

0 commit comments

Comments
 (0)