async def __call__(self, request): try: data = await self.parse_body(request) request_method = request.method.lower() is_graphiql = self.is_graphiql(request) is_pretty = self.is_pretty(request) # TODO: way better than if-else so better # implement this too on flask and sanic if request_method == "options": return self.process_preflight(request) all_params: List[GraphQLParams] execution_results, all_params = run_http_query( self.schema, request_method, data, query_data=request.query, batch_enabled=self.batch, catch=is_graphiql, # Execute options run_sync=not self.enable_async, root_value=self.get_root_value(), context_value=self.get_context(request), middleware=self.get_middleware(), validation_rules=self.get_validation_rules(), ) exec_res = ( [ ex if ex is None or isinstance(ex, ExecutionResult) else await ex for ex in execution_results ] if self.enable_async else execution_results ) result, status_code = encode_execution_results( exec_res, is_batch=isinstance(data, list), format_error=self.format_error, encode=partial(self.encode, pretty=is_pretty), # noqa: ignore ) if is_graphiql: graphiql_data = GraphiQLData( result=result, query=getattr(all_params[0], "query"), variables=getattr(all_params[0], "variables"), operation_name=getattr(all_params[0], "operation_name"), subscription_url=self.subscriptions, headers=self.headers, ) graphiql_config = GraphiQLConfig( graphiql_version=self.graphiql_version, graphiql_template=self.graphiql_template, graphiql_html_title=self.graphiql_html_title, jinja_env=self.jinja_env, ) graphiql_options = GraphiQLOptions( default_query=self.default_query, header_editor_enabled=self.header_editor_enabled, should_persist_headers=self.should_persist_headers, ) source = await render_graphiql_async( data=graphiql_data, config=graphiql_config, options=graphiql_options ) return web.Response(text=source, content_type="text/html") return web.Response( text=result, status=status_code, content_type="application/json", ) except HttpQueryError as err: parsed_error = GraphQLError(err.message) return web.Response( body=self.encode(dict(errors=[self.format_error(parsed_error)])), status=err.status_code, headers=err.headers, content_type="application/json", )
async def dispatch_request(self): try: request_method = request.method.lower() data = await self.parse_body() show_graphiql = request_method == "get" and self.should_display_graphiql( ) catch = show_graphiql pretty = self.pretty or show_graphiql or request.args.get("pretty") all_params: List[GraphQLParams] execution_results, all_params = run_http_query( self.schema, request_method, data, query_data=request.args, batch_enabled=self.batch, catch=catch, # Execute options run_sync=not self.enable_async, root_value=self.get_root_value(), context_value=self.get_context(), middleware=self.get_middleware(), ) exec_res = ([ ex if ex is None or isinstance(ex, ExecutionResult) else await ex for ex in execution_results ] if self.enable_async else execution_results) result, status_code = encode_execution_results( exec_res, is_batch=isinstance(data, list), format_error=self.format_error, encode=partial(self.encode, pretty=pretty), # noqa ) if show_graphiql: graphiql_data = GraphiQLData( result=result, query=getattr(all_params[0], "query"), variables=getattr(all_params[0], "variables"), operation_name=getattr(all_params[0], "operation_name"), subscription_url=self.subscriptions, headers=self.headers, ) graphiql_config = GraphiQLConfig( graphiql_version=self.graphiql_version, graphiql_template=self.graphiql_template, graphiql_html_title=self.graphiql_html_title, jinja_env=None, ) graphiql_options = GraphiQLOptions( default_query=self.default_query, header_editor_enabled=self.header_editor_enabled, should_persist_headers=self.should_persist_headers, ) source = render_graphiql_sync(data=graphiql_data, config=graphiql_config, options=graphiql_options) return await render_template_string(source) return Response(result, status=status_code, content_type="application/json") except HttpQueryError as e: parsed_error = GraphQLError(e.message) return Response( self.encode(dict(errors=[self.format_error(parsed_error)])), status=e.status_code, headers=e.headers, content_type="application/json", )
def dispatch_request(self, request): try: request_method = request.method.lower() data = self.parse_body(request) is_graphiql = self.is_graphiql(request) is_pretty = self.is_pretty(request) all_params: List[GraphQLParams] execution_results, all_params = run_http_query( self.schema, request_method, data, query_data=request.query_params, batch_enabled=self.batch, catch=is_graphiql, # Execute options root_value=self.get_root_value(), context_value=self.get_context(request), middleware=self.get_middleware(), ) result, status_code = encode_execution_results( execution_results, is_batch=isinstance(data, list), format_error=self.format_error, encode=partial(self.encode, pretty=is_pretty), # noqa ) if is_graphiql: graphiql_data = GraphiQLData( result=result, query=getattr(all_params[0], "query"), variables=getattr(all_params[0], "variables"), operation_name=getattr(all_params[0], "operation_name"), subscription_url=self.subscriptions, headers=self.headers, ) graphiql_config = GraphiQLConfig( graphiql_version=self.graphiql_version, graphiql_template=self.graphiql_template, graphiql_html_title=self.graphiql_html_title, jinja_env=None, ) graphiql_options = GraphiQLOptions( default_query=self.default_query, header_editor_enabled=self.header_editor_enabled, should_persist_headers=self.should_persist_headers, ) source = render_graphiql_sync(data=graphiql_data, config=graphiql_config, options=graphiql_options) return Response(source, headers={'Content-Type': 'text/html'}) return Response(result, status_code=status_code, headers={'Content-Type': 'application/json'}) except HttpQueryError as e: parsed_error = GraphQLError(e.message) headers = {} if e.headers is not None: headers.update(e.headers) return Response( self.encode(dict(errors=[self.format_error(parsed_error)])), status_code=e.status_code, headers=headers.update({'Content-Type': 'application/json'}), )