Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Simplify the request processing in the graphql view handler
NyanKiyoshi83806f3309ff
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.pyindex f2ded089dd..5c72c1bc33 100644--- a/saleor/graphql/views.py+++ b/saleor/graphql/views.py@@ -7,7 +7,7 @@ from django.shortcuts import render_to_responsefrom django.views.generic import Viewfrom graphene_django.settings import graphene_settingsfrom graphene_django.views import instantiate_middleware-from graphql import get_default_backend+from graphql import get_default_backend, GraphQLDocumentfrom graphql.error import (GraphQLError, GraphQLSyntaxError, format_error as format_graphql_error)from graphql.execution import ExecutionResult@@ -86,11 +86,7 @@ class GraphQLView(View):return JsonResponse(data=result, status=status_code, safe=False)def get_response(self, request: HttpRequest, data: dict):- query, variables, operation_name = self.get_graphql_params(- request, data)-- execution_result = self.execute_graphql_request(- request, query, variables, operation_name)+ execution_result = self.execute_graphql_request(request, data)status_code = 200if execution_result:response = {}@@ -109,17 +105,32 @@ class GraphQLView(View):def get_root_value(self, request: HttpRequest):return self.root_value- def execute_graphql_request(- self, request: HttpRequest, query: str, variables: dict,- operation_name: str):+ def parse_query(self, query: str) -> (GraphQLDocument, ExecutionResult):+ """Attempt to parse a query (mandatory) to a gql document object.++ If no query was given, it returns an error.+ If the query is invalid, it returns an error as well.+ Otherwise, it returns the parsed gql document.+ """if not query:- return ExecutionResult(+ return None, ExecutionResult(errors=[ValueError('Must provide a query string.')],invalid=True)++ # Attempt to parse the query, if it fails, return the errortry:- document = self.backend.document_from_string(self.schema, query)+ return self.backend.document_from_string(self.schema, query), Noneexcept (ValueError, GraphQLSyntaxError) as e:- return ExecutionResult(errors=[e], invalid=True)+ return None, ExecutionResult(errors=[e], invalid=True)++ def execute_graphql_request(self, request: HttpRequest, data: dict):+ query, variables, operation_name = self.get_graphql_params(+ request, data)++ document, error = self.parse_query(query)+ if error:+ return error+extra_options = {}if self.executor:# We only include it optionally since