mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-02-21 14:02:48 +03:00
Refactor execute_graphql_request function to reduce complexity
This commit is contained in:
parent
a94a89d88f
commit
59c01172bb
|
@ -284,6 +284,25 @@ class GraphQLView(View):
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
def validate_query_request_type(
|
||||||
|
self, request, document, operation_name, show_graphiql
|
||||||
|
):
|
||||||
|
if request.method.lower() == "get":
|
||||||
|
operation_ast = get_operation_ast(document, operation_name)
|
||||||
|
if (
|
||||||
|
operation_ast
|
||||||
|
and operation_ast.operation != OperationType.QUERY
|
||||||
|
and not show_graphiql
|
||||||
|
):
|
||||||
|
raise HttpError(
|
||||||
|
HttpResponseNotAllowed(
|
||||||
|
["POST"],
|
||||||
|
"Can only perform a {} operation from a POST request.".format(
|
||||||
|
operation_ast.operation.value
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def execute_graphql_request(
|
def execute_graphql_request(
|
||||||
self, request, data, query, variables, operation_name, show_graphiql=False
|
self, request, data, query, variables, operation_name, show_graphiql=False
|
||||||
):
|
):
|
||||||
|
@ -297,20 +316,12 @@ class GraphQLView(View):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return ExecutionResult(errors=[e])
|
return ExecutionResult(errors=[e])
|
||||||
|
|
||||||
if request.method.lower() == "get":
|
self.validate_query_request_type(
|
||||||
operation_ast = get_operation_ast(document, operation_name)
|
request, document, operation_name, show_graphiql
|
||||||
if operation_ast and operation_ast.operation != OperationType.QUERY:
|
)
|
||||||
if show_graphiql:
|
if show_graphiql:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
raise HttpError(
|
|
||||||
HttpResponseNotAllowed(
|
|
||||||
["POST"],
|
|
||||||
"Can only perform a {} operation from a POST request.".format(
|
|
||||||
operation_ast.operation.value
|
|
||||||
),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
extra_options = {}
|
extra_options = {}
|
||||||
if self.execution_context_class:
|
if self.execution_context_class:
|
||||||
|
@ -327,14 +338,7 @@ class GraphQLView(View):
|
||||||
options.update(extra_options)
|
options.update(extra_options)
|
||||||
|
|
||||||
operation_ast = get_operation_ast(document, operation_name)
|
operation_ast = get_operation_ast(document, operation_name)
|
||||||
if (
|
if self.is_atomic_mutation_enabled(operation_ast, connection):
|
||||||
operation_ast
|
|
||||||
and operation_ast.operation == OperationType.MUTATION
|
|
||||||
and (
|
|
||||||
graphene_settings.ATOMIC_MUTATIONS is True
|
|
||||||
or connection.settings_dict.get("ATOMIC_MUTATIONS", False) is True
|
|
||||||
)
|
|
||||||
):
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
result = self.schema.execute(**options)
|
result = self.schema.execute(**options)
|
||||||
if getattr(request, MUTATION_ERRORS_FLAG, False) is True:
|
if getattr(request, MUTATION_ERRORS_FLAG, False) is True:
|
||||||
|
@ -399,3 +403,14 @@ class GraphQLView(View):
|
||||||
meta = request.META
|
meta = request.META
|
||||||
content_type = meta.get("CONTENT_TYPE", meta.get("HTTP_CONTENT_TYPE", ""))
|
content_type = meta.get("CONTENT_TYPE", meta.get("HTTP_CONTENT_TYPE", ""))
|
||||||
return content_type.split(";", 1)[0].lower()
|
return content_type.split(";", 1)[0].lower()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_atomic_mutation_enabled(operation_ast, connection):
|
||||||
|
return (
|
||||||
|
operation_ast
|
||||||
|
and operation_ast.operation == OperationType.MUTATION
|
||||||
|
and (
|
||||||
|
graphene_settings.ATOMIC_MUTATIONS is True
|
||||||
|
or connection.settings_dict.get("ATOMIC_MUTATIONS", False) is True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user