Skip to content

Commit 5bd4d31

Browse files
committed
Port GraphQLView to Graphene 3
1 parent 8412fee commit 5bd4d31

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

graphene_django/rest_framework/serializer_converter.py

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
singledispatch = import_single_dispatch()
1111

12-
1312
@singledispatch
1413
def get_graphene_type_from_serializer_field(field):
1514
raise ImproperlyConfigured(

graphene_django/views.py

+25-39
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from django.utils.decorators import method_decorator
1010
from django.views.generic import View
1111
from django.views.decorators.csrf import ensure_csrf_cookie
12+
from graphene import Schema
13+
from graphql import parse, get_operation_ast, OperationType, execute, validate
1214

13-
from graphql import get_default_backend
1415
from graphql.error import format_error as format_graphql_error
1516
from graphql.error import GraphQLError
1617
from graphql.execution import ExecutionResult
@@ -55,10 +56,8 @@ class GraphQLView(View):
5556
graphiql_template = "graphene/graphiql.html"
5657
react_version = "16.8.6"
5758

58-
schema = None
59+
schema: Schema = None
5960
graphiql = False
60-
executor = None
61-
backend = None
6261
middleware = None
6362
root_value = None
6463
pretty = False
@@ -67,35 +66,28 @@ class GraphQLView(View):
6766
def __init__(
6867
self,
6968
schema=None,
70-
executor=None,
7169
middleware=None,
7270
root_value=None,
7371
graphiql=False,
7472
pretty=False,
7573
batch=False,
76-
backend=None,
7774
):
7875
if not schema:
7976
schema = graphene_settings.SCHEMA
8077

81-
if backend is None:
82-
backend = get_default_backend()
83-
8478
if middleware is None:
8579
middleware = graphene_settings.MIDDLEWARE
8680

8781
self.schema = self.schema or schema
8882
if middleware is not None:
8983
self.middleware = list(instantiate_middleware(middleware))
90-
self.executor = executor
9184
self.root_value = root_value
9285
self.pretty = self.pretty or pretty
9386
self.graphiql = self.graphiql or graphiql
9487
self.batch = self.batch or batch
95-
self.backend = backend
9688

9789
assert isinstance(
98-
self.schema, GraphQLSchema
90+
self.schema, Schema
9991
), "A Schema is required to be provided to GraphQLView."
10092
assert not all((graphiql, batch)), "Use either graphiql or batch processing"
10193

@@ -109,9 +101,6 @@ def get_middleware(self, request):
109101
def get_context(self, request):
110102
return request
111103

112-
def get_backend(self, request):
113-
return self.backend
114-
115104
@method_decorator(ensure_csrf_cookie)
116105
def dispatch(self, request, *args, **kwargs):
117106
try:
@@ -173,7 +162,9 @@ def get_response(self, request, data, show_graphiql=False):
173162
self.format_error(e) for e in execution_result.errors
174163
]
175164

176-
if execution_result.invalid:
165+
if execution_result.errors and any(
166+
not e.path for e in execution_result.errors
167+
):
177168
status_code = 400
178169
else:
179170
response["data"] = execution_result.data
@@ -246,43 +237,38 @@ def execute_graphql_request(
246237
raise HttpError(HttpResponseBadRequest("Must provide query string."))
247238

248239
try:
249-
backend = self.get_backend(request)
250-
document = backend.document_from_string(self.schema, query)
240+
document = parse(query)
251241
except Exception as e:
252-
return ExecutionResult(errors=[e], invalid=True)
242+
return ExecutionResult(errors=[e])
253243

254244
if request.method.lower() == "get":
255-
operation_type = document.get_operation_type(operation_name)
256-
if operation_type and operation_type != "query":
245+
operation_ast = get_operation_ast(document, operation_name)
246+
if operation_ast and operation_ast.operation != OperationType.QUERY:
257247
if show_graphiql:
258248
return None
259249

260250
raise HttpError(
261251
HttpResponseNotAllowed(
262252
["POST"],
263253
"Can only perform a {} operation from a POST request.".format(
264-
operation_type
254+
operation_ast.operation.value
265255
),
266256
)
267257
)
268258

269-
try:
270-
extra_options = {}
271-
if self.executor:
272-
# We only include it optionally since
273-
# executor is not a valid argument in all backends
274-
extra_options["executor"] = self.executor
275-
276-
return document.execute(
277-
root=self.get_root_value(request),
278-
variables=variables,
279-
operation_name=operation_name,
280-
context=self.get_context(request),
281-
middleware=self.get_middleware(request),
282-
**extra_options
283-
)
284-
except Exception as e:
285-
return ExecutionResult(errors=[e], invalid=True)
259+
validation_errors = validate(self.schema.graphql_schema, document)
260+
if validation_errors:
261+
return ExecutionResult(data=None, errors=validation_errors)
262+
263+
return execute(
264+
schema=self.schema.graphql_schema,
265+
document=document,
266+
root_value=self.get_root_value(request),
267+
variable_values=variables,
268+
operation_name=operation_name,
269+
context_value=self.get_context(request),
270+
middleware=self.get_middleware(request),
271+
)
286272

287273
@classmethod
288274
def can_display_graphiql(cls, request, data):

0 commit comments

Comments
 (0)