19
19
20
20
class GraphQLView : # pylint: disable = too-many-instance-attributes
21
21
def __init__ (
22
- self ,
23
- schema = None ,
24
- executor = None ,
25
- root_value = None ,
26
- context = None ,
27
- pretty = False ,
28
- graphiql = False ,
29
- graphiql_version = None ,
30
- graphiql_template = None ,
31
- middleware = None ,
32
- batch = False ,
33
- jinja_env = None ,
34
- max_age = 86400 ,
35
- encoder = None ,
36
- error_formatter = None ,
37
- enable_async = True ):
22
+ self ,
23
+ schema = None ,
24
+ executor = None ,
25
+ root_value = None ,
26
+ context = None ,
27
+ pretty = False ,
28
+ graphiql = False ,
29
+ graphiql_version = None ,
30
+ graphiql_template = None ,
31
+ middleware = None ,
32
+ batch = False ,
33
+ jinja_env = None ,
34
+ max_age = 86400 ,
35
+ encoder = None ,
36
+ error_formatter = None ,
37
+ enable_async = True ,
38
+ ):
38
39
# pylint: disable=too-many-arguments
39
40
# pylint: disable=too-many-locals
40
41
@@ -52,33 +53,34 @@ def __init__(
52
53
self .max_age = max_age
53
54
self .encoder = encoder or json_encode
54
55
self .error_formatter = error_formatter or default_format_error
55
- self .enable_async = enable_async and isinstance (
56
- self . executor , AsyncioExecutor )
57
- assert isinstance ( self .schema , GraphQLSchema ), \
58
- ' A Schema is required to be provided to GraphQLView.'
56
+ self .enable_async = enable_async and isinstance (self . executor , AsyncioExecutor )
57
+ assert isinstance (
58
+ self .schema , GraphQLSchema
59
+ ), " A Schema is required to be provided to GraphQLView."
59
60
60
61
def get_context (self , request ):
61
62
if self .context and isinstance (self .context , Mapping ):
62
63
context = self .context .copy ()
63
64
else :
64
65
context = {}
65
66
66
- if isinstance (context , Mapping ) and ' request' not in context :
67
- context .update ({' request' : request })
67
+ if isinstance (context , Mapping ) and " request" not in context :
68
+ context .update ({" request" : request })
68
69
return context
69
70
70
71
async def parse_body (self , request ):
71
- if request .content_type == ' application/graphql' :
72
+ if request .content_type == " application/graphql" :
72
73
r_text = await request .text ()
73
- return {' query' : r_text }
74
+ return {" query" : r_text }
74
75
75
- if request .content_type == ' application/json' :
76
+ if request .content_type == " application/json" :
76
77
text = await request .text ()
77
78
return load_json_body (text )
78
79
79
80
if request .content_type in (
80
- 'application/x-www-form-urlencoded' ,
81
- 'multipart/form-data' ):
81
+ "application/x-www-form-urlencoded" ,
82
+ "multipart/form-data" ,
83
+ ):
82
84
# TODO: seems like a multidict would be more appropriate
83
85
# than casting it and de-duping variables. Alas, it's what
84
86
# graphql-python wants.
@@ -96,22 +98,24 @@ def render_graphiql(self, params, result):
96
98
)
97
99
98
100
def is_graphiql (self , request ):
99
- return all ([
100
- self .graphiql ,
101
- request .method .lower () == 'get' ,
102
- 'raw' not in request .query ,
103
- any ([
104
- 'text/html' in request .headers .get ('accept' , {}),
105
- '*/*' in request .headers .get ('accept' , {}),
106
- ]),
107
- ])
101
+ return all (
102
+ [
103
+ self .graphiql ,
104
+ request .method .lower () == "get" ,
105
+ "raw" not in request .query ,
106
+ any (
107
+ [
108
+ "text/html" in request .headers .get ("accept" , {}),
109
+ "*/*" in request .headers .get ("accept" , {}),
110
+ ]
111
+ ),
112
+ ]
113
+ )
108
114
109
115
def is_pretty (self , request ):
110
- return any ([
111
- self .pretty ,
112
- self .is_graphiql (request ),
113
- request .query .get ('pretty' ),
114
- ])
116
+ return any (
117
+ [self .pretty , self .is_graphiql (request ), request .query .get ("pretty" )]
118
+ )
115
119
116
120
async def __call__ (self , request ):
117
121
try :
@@ -120,7 +124,7 @@ async def __call__(self, request):
120
124
is_graphiql = self .is_graphiql (request )
121
125
is_pretty = self .is_pretty (request )
122
126
123
- if request_method == ' options' :
127
+ if request_method == " options" :
124
128
return self .process_preflight (request )
125
129
126
130
execution_results , all_params = run_http_query (
@@ -141,8 +145,8 @@ async def __call__(self, request):
141
145
if is_graphiql and self .enable_async :
142
146
# catch errors like run_http_query does when async
143
147
execution_results = [
144
- result .catch (lambda value : None )
145
- for result in execution_results ]
148
+ result .catch (lambda value : None ) for result in execution_results
149
+ ]
146
150
awaited_execution_results = await Promise .all (execution_results )
147
151
result , status_code = encode_execution_results (
148
152
awaited_execution_results ,
@@ -152,54 +156,46 @@ async def __call__(self, request):
152
156
)
153
157
154
158
if is_graphiql :
155
- return await self .render_graphiql (
156
- params = all_params [0 ],
157
- result = result ,
158
- )
159
+ return await self .render_graphiql (params = all_params [0 ], result = result ,)
159
160
160
161
return web .Response (
161
- text = result ,
162
- status = status_code ,
163
- content_type = 'application/json' ,
162
+ text = result , status = status_code , content_type = "application/json" ,
164
163
)
165
164
166
165
except HttpQueryError as err :
167
- if err .headers and ' Allow' in err .headers :
166
+ if err .headers and " Allow" in err .headers :
168
167
# bug in graphql_server.execute_graphql_request
169
168
# https://github.com/graphql-python/graphql-server-core/pull/4
170
- if isinstance (err .headers [' Allow' ], list ):
171
- err .headers [' Allow' ] = ', ' .join (err .headers [' Allow' ])
169
+ if isinstance (err .headers [" Allow" ], list ):
170
+ err .headers [" Allow" ] = ", " .join (err .headers [" Allow" ])
172
171
173
172
return web .Response (
174
- text = self .encoder ({
175
- 'errors' : [self .error_formatter (err )]
176
- }),
173
+ text = self .encoder ({"errors" : [self .error_formatter (err )]}),
177
174
status = err .status_code ,
178
175
headers = err .headers ,
179
- content_type = ' application/json' ,
176
+ content_type = " application/json" ,
180
177
)
181
178
182
179
def process_preflight (self , request ):
183
180
""" Preflight request support for apollo-client
184
181
https://www.w3.org/TR/cors/#resource-preflight-requests """
185
182
headers = request .headers
186
- origin = headers .get (' Origin' , '' )
187
- method = headers .get (' Access-Control-Request-Method' , '' ).upper ()
183
+ origin = headers .get (" Origin" , "" )
184
+ method = headers .get (" Access-Control-Request-Method" , "" ).upper ()
188
185
189
- accepted_methods = [' GET' , ' POST' , ' PUT' , ' DELETE' ]
186
+ accepted_methods = [" GET" , " POST" , " PUT" , " DELETE" ]
190
187
if method and method in accepted_methods :
191
188
return web .Response (
192
189
status = 200 ,
193
190
headers = {
194
- ' Access-Control-Allow-Origin' : origin ,
195
- ' Access-Control-Allow-Methods' : ', ' .join (accepted_methods ),
196
- ' Access-Control-Max-Age' : str (self .max_age ),
197
- }
191
+ " Access-Control-Allow-Origin" : origin ,
192
+ " Access-Control-Allow-Methods" : ", " .join (accepted_methods ),
193
+ " Access-Control-Max-Age" : str (self .max_age ),
194
+ },
198
195
)
199
196
return web .Response (status = 400 )
200
197
201
198
@classmethod
202
- def attach (cls , app , * , route_path = '/graphql' , route_name = 'graphql' ,
203
- ** kwargs ):
199
+ def attach (cls , app , * , route_path = "/graphql" , route_name = "graphql" , ** kwargs ):
204
200
view = cls (** kwargs )
205
- app .router .add_route ('*' , route_path , view , name = route_name )
201
+ app .router .add_route ("*" , route_path , view , name = route_name )
0 commit comments