Skip to content

Commit cf6d1d4

Browse files
authored
Add graphiql options and missing flask context (#55)
* Add graphiql options and missing flask context * Possible custom context test fix * Provide same context tests to other integration
1 parent e8f3a89 commit cf6d1d4

13 files changed

+263
-114
lines changed

graphql_server/aiohttp/graphqlview.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from graphql_server.render_graphiql import (
2020
GraphiQLConfig,
2121
GraphiQLData,
22+
GraphiQLOptions,
2223
render_graphiql_async,
2324
)
2425

@@ -39,6 +40,9 @@ class GraphQLView:
3940
enable_async = False
4041
subscriptions = None
4142
headers = None
43+
default_query = None
44+
header_editor_enabled = None
45+
should_persist_headers = None
4246

4347
accepted_methods = ["GET", "POST", "PUT", "DELETE"]
4448

@@ -174,8 +178,13 @@ async def __call__(self, request):
174178
graphiql_html_title=self.graphiql_html_title,
175179
jinja_env=self.jinja_env,
176180
)
181+
graphiql_options = GraphiQLOptions(
182+
default_query=self.default_query,
183+
header_editor_enabled=self.header_editor_enabled,
184+
should_persist_headers=self.should_persist_headers,
185+
)
177186
source = await render_graphiql_async(
178-
data=graphiql_data, config=graphiql_config
187+
data=graphiql_data, config=graphiql_config, options=graphiql_options
179188
)
180189
return web.Response(text=source, content_type="text/html")
181190

graphql_server/flask/graphqlview.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import copy
2+
from collections.abc import MutableMapping
13
from functools import partial
24
from typing import List
35

@@ -18,13 +20,15 @@
1820
from graphql_server.render_graphiql import (
1921
GraphiQLConfig,
2022
GraphiQLData,
23+
GraphiQLOptions,
2124
render_graphiql_sync,
2225
)
2326

2427

2528
class GraphQLView(View):
2629
schema = None
2730
root_value = None
31+
context = None
2832
pretty = False
2933
graphiql = False
3034
graphiql_version = None
@@ -34,6 +38,9 @@ class GraphQLView(View):
3438
batch = False
3539
subscriptions = None
3640
headers = None
41+
default_query = None
42+
header_editor_enabled = None
43+
should_persist_headers = None
3744

3845
methods = ["GET", "POST", "PUT", "DELETE"]
3946

@@ -50,12 +57,18 @@ def __init__(self, **kwargs):
5057
self.schema, GraphQLSchema
5158
), "A Schema is required to be provided to GraphQLView."
5259

53-
# noinspection PyUnusedLocal
5460
def get_root_value(self):
5561
return self.root_value
5662

57-
def get_context_value(self):
58-
return request
63+
def get_context(self):
64+
context = (
65+
copy.copy(self.context)
66+
if self.context and isinstance(self.context, MutableMapping)
67+
else {}
68+
)
69+
if isinstance(context, MutableMapping) and "request" not in context:
70+
context.update({"request": request})
71+
return context
5972

6073
def get_middleware(self):
6174
return self.middleware
@@ -80,7 +93,7 @@ def dispatch_request(self):
8093
catch=catch,
8194
# Execute options
8295
root_value=self.get_root_value(),
83-
context_value=self.get_context_value(),
96+
context_value=self.get_context(),
8497
middleware=self.get_middleware(),
8598
)
8699
result, status_code = encode_execution_results(
@@ -105,8 +118,13 @@ def dispatch_request(self):
105118
graphiql_html_title=self.graphiql_html_title,
106119
jinja_env=None,
107120
)
121+
graphiql_options = GraphiQLOptions(
122+
default_query=self.default_query,
123+
header_editor_enabled=self.header_editor_enabled,
124+
should_persist_headers=self.should_persist_headers,
125+
)
108126
source = render_graphiql_sync(
109-
data=graphiql_data, config=graphiql_config
127+
data=graphiql_data, config=graphiql_config, options=graphiql_options
110128
)
111129
return render_template_string(source)
112130

graphql_server/render_graphiql.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class GraphiQLOptions(TypedDict):
201201
202202
default_query
203203
An optional GraphQL string to use when no query is provided and no stored
204-
query exists from a previous session. If undefined is provided, GraphiQL
204+
query exists from a previous session. If None is provided, GraphiQL
205205
will use its own default query.
206206
header_editor_enabled
207207
An optional boolean which enables the header editor when true.

graphql_server/sanic/graphqlview.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from graphql_server.render_graphiql import (
2222
GraphiQLConfig,
2323
GraphiQLData,
24+
GraphiQLOptions,
2425
render_graphiql_async,
2526
)
2627

@@ -41,6 +42,9 @@ class GraphQLView(HTTPMethodView):
4142
enable_async = False
4243
subscriptions = None
4344
headers = None
45+
default_query = None
46+
header_editor_enabled = None
47+
should_persist_headers = None
4448

4549
methods = ["GET", "POST", "PUT", "DELETE"]
4650

@@ -127,8 +131,15 @@ async def dispatch_request(self, request, *args, **kwargs):
127131
graphiql_html_title=self.graphiql_html_title,
128132
jinja_env=self.jinja_env,
129133
)
134+
graphiql_options = GraphiQLOptions(
135+
default_query=self.default_query,
136+
header_editor_enabled=self.header_editor_enabled,
137+
should_persist_headers=self.should_persist_headers,
138+
)
130139
source = await render_graphiql_async(
131-
data=graphiql_data, config=graphiql_config
140+
data=graphiql_data,
141+
config=graphiql_config,
142+
options=graphiql_options,
132143
)
133144
return html(source)
134145

graphql_server/webob/graphqlview.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from graphql_server.render_graphiql import (
2020
GraphiQLConfig,
2121
GraphiQLData,
22+
GraphiQLOptions,
2223
render_graphiql_sync,
2324
)
2425

@@ -38,6 +39,9 @@ class GraphQLView:
3839
enable_async = False
3940
subscriptions = None
4041
headers = None
42+
default_query = None
43+
header_editor_enabled = None
44+
should_persist_headers = None
4145
charset = "UTF-8"
4246

4347
format_error = staticmethod(format_error_default)
@@ -117,8 +121,17 @@ def dispatch_request(self, request):
117121
graphiql_html_title=self.graphiql_html_title,
118122
jinja_env=None,
119123
)
124+
graphiql_options = GraphiQLOptions(
125+
default_query=self.default_query,
126+
header_editor_enabled=self.header_editor_enabled,
127+
should_persist_headers=self.should_persist_headers,
128+
)
120129
return Response(
121-
render_graphiql_sync(data=graphiql_data, config=graphiql_config),
130+
render_graphiql_sync(
131+
data=graphiql_data,
132+
config=graphiql_config,
133+
options=graphiql_options,
134+
),
122135
charset=self.charset,
123136
content_type="text/html",
124137
)

tests/aiohttp/schema.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ def resolve_raises(*_):
2424
resolve=lambda obj, info, *args: info.context["request"].query.get("q"),
2525
),
2626
"context": GraphQLField(
27-
GraphQLNonNull(GraphQLString),
28-
resolve=lambda obj, info, *args: info.context["request"],
27+
GraphQLObjectType(
28+
name="context",
29+
fields={
30+
"session": GraphQLField(GraphQLString),
31+
"request": GraphQLField(
32+
GraphQLNonNull(GraphQLString),
33+
resolve=lambda obj, info: info.context["request"],
34+
),
35+
},
36+
),
37+
resolve=lambda obj, info: info.context,
2938
),
3039
"test": GraphQLField(
3140
type_=GraphQLString,

0 commit comments

Comments
 (0)