1919
2020class GraphQLView : # pylint: disable = too-many-instance-attributes
2121 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+ ):
3839 # pylint: disable=too-many-arguments
3940 # pylint: disable=too-many-locals
4041
@@ -52,33 +53,34 @@ def __init__(
5253 self .max_age = max_age
5354 self .encoder = encoder or json_encode
5455 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."
5960
6061 def get_context (self , request ):
6162 if self .context and isinstance (self .context , Mapping ):
6263 context = self .context .copy ()
6364 else :
6465 context = {}
6566
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 })
6869 return context
6970
7071 async def parse_body (self , request ):
71- if request .content_type == ' application/graphql' :
72+ if request .content_type == " application/graphql" :
7273 r_text = await request .text ()
73- return {' query' : r_text }
74+ return {" query" : r_text }
7475
75- if request .content_type == ' application/json' :
76+ if request .content_type == " application/json" :
7677 text = await request .text ()
7778 return load_json_body (text )
7879
7980 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+ ):
8284 # TODO: seems like a multidict would be more appropriate
8385 # than casting it and de-duping variables. Alas, it's what
8486 # graphql-python wants.
@@ -96,22 +98,24 @@ def render_graphiql(self, params, result):
9698 )
9799
98100 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+ )
108114
109115 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+ )
115119
116120 async def __call__ (self , request ):
117121 try :
@@ -120,7 +124,7 @@ async def __call__(self, request):
120124 is_graphiql = self .is_graphiql (request )
121125 is_pretty = self .is_pretty (request )
122126
123- if request_method == ' options' :
127+ if request_method == " options" :
124128 return self .process_preflight (request )
125129
126130 execution_results , all_params = run_http_query (
@@ -141,8 +145,8 @@ async def __call__(self, request):
141145 if is_graphiql and self .enable_async :
142146 # catch errors like run_http_query does when async
143147 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+ ]
146150 awaited_execution_results = await Promise .all (execution_results )
147151 result , status_code = encode_execution_results (
148152 awaited_execution_results ,
@@ -152,54 +156,46 @@ async def __call__(self, request):
152156 )
153157
154158 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 ,)
159160
160161 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" ,
164163 )
165164
166165 except HttpQueryError as err :
167- if err .headers and ' Allow' in err .headers :
166+ if err .headers and " Allow" in err .headers :
168167 # bug in graphql_server.execute_graphql_request
169168 # 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" ])
172171
173172 return web .Response (
174- text = self .encoder ({
175- 'errors' : [self .error_formatter (err )]
176- }),
173+ text = self .encoder ({"errors" : [self .error_formatter (err )]}),
177174 status = err .status_code ,
178175 headers = err .headers ,
179- content_type = ' application/json' ,
176+ content_type = " application/json" ,
180177 )
181178
182179 def process_preflight (self , request ):
183180 """ Preflight request support for apollo-client
184181 https://www.w3.org/TR/cors/#resource-preflight-requests """
185182 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 ()
188185
189- accepted_methods = [' GET' , ' POST' , ' PUT' , ' DELETE' ]
186+ accepted_methods = [" GET" , " POST" , " PUT" , " DELETE" ]
190187 if method and method in accepted_methods :
191188 return web .Response (
192189 status = 200 ,
193190 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+ },
198195 )
199196 return web .Response (status = 400 )
200197
201198 @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 ):
204200 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