1
+ import copy
2
+ from collections .abc import MutableMapping
1
3
from functools import partial
2
4
from typing import List
3
5
18
20
from graphql_server .render_graphiql import (
19
21
GraphiQLConfig ,
20
22
GraphiQLData ,
23
+ GraphiQLOptions ,
21
24
render_graphiql_sync ,
22
25
)
23
26
24
27
25
28
class GraphQLView (View ):
26
29
schema = None
27
30
root_value = None
31
+ context = None
28
32
pretty = False
29
33
graphiql = False
30
34
graphiql_version = None
@@ -34,6 +38,9 @@ class GraphQLView(View):
34
38
batch = False
35
39
subscriptions = None
36
40
headers = None
41
+ default_query = None
42
+ header_editor_enabled = None
43
+ should_persist_headers = None
37
44
38
45
methods = ["GET" , "POST" , "PUT" , "DELETE" ]
39
46
@@ -50,12 +57,18 @@ def __init__(self, **kwargs):
50
57
self .schema , GraphQLSchema
51
58
), "A Schema is required to be provided to GraphQLView."
52
59
53
- # noinspection PyUnusedLocal
54
60
def get_root_value (self ):
55
61
return self .root_value
56
62
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
59
72
60
73
def get_middleware (self ):
61
74
return self .middleware
@@ -80,7 +93,7 @@ def dispatch_request(self):
80
93
catch = catch ,
81
94
# Execute options
82
95
root_value = self .get_root_value (),
83
- context_value = self .get_context_value (),
96
+ context_value = self .get_context (),
84
97
middleware = self .get_middleware (),
85
98
)
86
99
result , status_code = encode_execution_results (
@@ -105,8 +118,13 @@ def dispatch_request(self):
105
118
graphiql_html_title = self .graphiql_html_title ,
106
119
jinja_env = None ,
107
120
)
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
+ )
108
126
source = render_graphiql_sync (
109
- data = graphiql_data , config = graphiql_config
127
+ data = graphiql_data , config = graphiql_config , options = graphiql_options
110
128
)
111
129
return render_template_string (source )
112
130
0 commit comments