def dispatch_request(self, request, *args, **kwargs):
try:
request_method = request.method.lower()
data = self.parse_body(request)
show_graphiql = request_method == 'get' and self.should_display_graphiql(request)
catch = show_graphiql
pretty = self.pretty or show_graphiql or request.args.get('pretty')
if request_method != 'options':
execution_results, all_params = run_http_query(
self.schema,
request_method,
data,
query_data=request.args,
batch_enabled=self.batch,
catch=catch,
# Execute options
return_promise=self._enable_async,
root_value=self.get_root_value(request),
context_value=self.get_context(request),
middleware=self.get_middleware(request),
executor=self.get_executor(request),
)
awaited_execution_results = await Promise.all(execution_results)
result, status_code = encode_execution_results(
awaited_execution_results,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=pretty)
)
if show_graphiql:
return await self.render_graphiql(
params=all_params[0],
result=result
)
return HTTPResponse(
result,
status=status_code,
content_type='application/json'
)
else:
return self.process_preflight(request)
except HttpQueryError as e:
return HTTPResponse(
self.encode({
'errors': [default_format_error(e)]
}),
status=e.status_code,
headers=e.headers,
content_type='application/json'
)
# noinspection PyBroadException
评论列表
文章目录