2016-09-20 08:33:46 +03:00
|
|
|
import inspect
|
2016-09-20 08:04:23 +03:00
|
|
|
import json
|
2016-09-20 08:15:10 +03:00
|
|
|
import re
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2020-12-31 02:37:57 +03:00
|
|
|
from django.db import connection, transaction
|
2016-09-20 08:04:23 +03:00
|
|
|
from django.http import HttpResponse, HttpResponseNotAllowed
|
|
|
|
from django.http.response import HttpResponseBadRequest
|
|
|
|
from django.shortcuts import render
|
2016-09-20 09:14:59 +03:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.decorators.csrf import ensure_csrf_cookie
|
2020-05-09 14:13:47 +03:00
|
|
|
from django.views.generic import View
|
2023-10-29 18:42:27 +03:00
|
|
|
from graphql import (
|
|
|
|
ExecutionResult,
|
|
|
|
OperationType,
|
|
|
|
execute,
|
|
|
|
get_operation_ast,
|
|
|
|
parse,
|
|
|
|
validate_schema,
|
|
|
|
)
|
2016-09-20 08:15:10 +03:00
|
|
|
from graphql.error import GraphQLError
|
2020-05-09 14:15:16 +03:00
|
|
|
from graphql.execution.middleware import MiddlewareManager
|
2023-10-29 18:42:27 +03:00
|
|
|
from graphql.validation import validate
|
2016-09-18 03:09:56 +03:00
|
|
|
|
2023-08-06 01:47:00 +03:00
|
|
|
from graphene import Schema
|
2020-12-31 02:37:57 +03:00
|
|
|
from graphene_django.constants import MUTATION_ERRORS_FLAG
|
|
|
|
from graphene_django.utils.utils import set_rollback
|
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
from .settings import graphene_settings
|
|
|
|
|
|
|
|
|
|
|
|
class HttpError(Exception):
|
|
|
|
def __init__(self, response, message=None, *args, **kwargs):
|
|
|
|
self.response = response
|
|
|
|
self.message = message = message or response.content.decode()
|
2022-10-19 17:10:30 +03:00
|
|
|
super().__init__(message, *args, **kwargs)
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_accepted_content_types(request):
|
|
|
|
def qualify(x):
|
2018-07-20 02:51:33 +03:00
|
|
|
parts = x.split(";", 1)
|
2016-09-20 08:04:23 +03:00
|
|
|
if len(parts) == 2:
|
2018-07-20 02:51:33 +03:00
|
|
|
match = re.match(r"(^|;)q=(0(\.\d{,3})?|1(\.0{,3})?)(;|$)", parts[1])
|
2016-09-20 08:04:23 +03:00
|
|
|
if match:
|
2017-12-12 05:08:42 +03:00
|
|
|
return parts[0].strip(), float(match.group(2))
|
|
|
|
return parts[0].strip(), 1
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
raw_content_types = request.META.get("HTTP_ACCEPT", "*/*").split(",")
|
2016-09-20 08:04:23 +03:00
|
|
|
qualified_content_types = map(qualify, raw_content_types)
|
2023-08-06 01:47:00 +03:00
|
|
|
return [
|
2018-07-20 02:51:33 +03:00
|
|
|
x[0] for x in sorted(qualified_content_types, key=lambda x: x[1], reverse=True)
|
2023-08-06 01:47:00 +03:00
|
|
|
]
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
|
2016-09-20 08:33:46 +03:00
|
|
|
def instantiate_middleware(middlewares):
|
|
|
|
for middleware in middlewares:
|
|
|
|
if inspect.isclass(middleware):
|
|
|
|
yield middleware()
|
|
|
|
continue
|
|
|
|
yield middleware
|
|
|
|
|
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
class GraphQLView(View):
|
2018-07-20 02:51:33 +03:00
|
|
|
graphiql_template = "graphene/graphiql.html"
|
2020-07-12 22:48:12 +03:00
|
|
|
|
|
|
|
# Polyfill for window.fetch.
|
2021-04-21 09:05:46 +03:00
|
|
|
whatwg_fetch_version = "3.6.2"
|
|
|
|
whatwg_fetch_sri = "sha256-+pQdxwAcHJdQ3e/9S4RK6g8ZkwdMgFQuHvLuN5uyk5c="
|
2020-07-12 22:48:12 +03:00
|
|
|
|
|
|
|
# React and ReactDOM.
|
2021-04-21 09:05:46 +03:00
|
|
|
react_version = "17.0.2"
|
|
|
|
react_sri = "sha256-Ipu/TQ50iCCVZBUsZyNJfxrDk0E2yhaEIz0vqI+kFG8="
|
|
|
|
react_dom_sri = "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0="
|
2020-07-12 22:48:12 +03:00
|
|
|
|
|
|
|
# The GraphiQL React app.
|
2023-06-02 11:48:53 +03:00
|
|
|
graphiql_version = "2.4.7"
|
|
|
|
graphiql_sri = "sha256-n/LKaELupC1H/PU6joz+ybeRJHT2xCdekEt6OYMOOZU="
|
|
|
|
graphiql_css_sri = "sha256-OsbM+LQHcnFHi0iH7AUKueZvDcEBoy/z4hJ7jx1cpsM="
|
2020-07-12 22:48:12 +03:00
|
|
|
|
|
|
|
# The websocket transport library for subscriptions.
|
2023-06-02 11:48:53 +03:00
|
|
|
subscriptions_transport_ws_version = "5.13.1"
|
2020-07-12 22:48:12 +03:00
|
|
|
subscriptions_transport_ws_sri = (
|
2023-04-14 17:34:17 +03:00
|
|
|
"sha256-EZhvg6ANJrBsgLvLAa0uuHNLepLJVCFYS+xlb5U/bqw="
|
2020-07-12 22:48:12 +03:00
|
|
|
)
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2023-05-04 22:06:10 +03:00
|
|
|
graphiql_plugin_explorer_version = "0.1.15"
|
|
|
|
graphiql_plugin_explorer_sri = "sha256-3hUuhBXdXlfCj6RTeEkJFtEh/kUG+TCDASFpFPLrzvE="
|
2023-06-02 11:48:53 +03:00
|
|
|
graphiql_plugin_explorer_css_sri = (
|
|
|
|
"sha256-fA0LPUlukMNR6L4SPSeFqDTYav8QdWjQ2nr559Zln1U="
|
|
|
|
)
|
2023-05-04 22:06:10 +03:00
|
|
|
|
2016-09-20 08:50:51 +03:00
|
|
|
schema = None
|
2016-09-20 08:04:23 +03:00
|
|
|
graphiql = False
|
|
|
|
middleware = None
|
|
|
|
root_value = None
|
|
|
|
pretty = False
|
2016-10-31 13:56:51 +03:00
|
|
|
batch = False
|
2020-07-12 16:42:31 +03:00
|
|
|
subscription_path = None
|
2021-03-31 20:31:20 +03:00
|
|
|
execution_context_class = None
|
2023-12-20 12:48:45 +03:00
|
|
|
validation_rules = None
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
schema=None,
|
|
|
|
middleware=None,
|
|
|
|
root_value=None,
|
|
|
|
graphiql=False,
|
|
|
|
pretty=False,
|
|
|
|
batch=False,
|
2020-07-12 16:42:31 +03:00
|
|
|
subscription_path=None,
|
2020-12-31 02:37:57 +03:00
|
|
|
execution_context_class=None,
|
2023-12-20 12:48:45 +03:00
|
|
|
validation_rules=None,
|
2018-07-20 02:51:33 +03:00
|
|
|
):
|
2016-09-20 08:04:23 +03:00
|
|
|
if not schema:
|
|
|
|
schema = graphene_settings.SCHEMA
|
|
|
|
|
|
|
|
if middleware is None:
|
|
|
|
middleware = graphene_settings.MIDDLEWARE
|
|
|
|
|
2023-05-24 15:54:44 +03:00
|
|
|
self.schema = schema or self.schema
|
2016-09-20 08:33:46 +03:00
|
|
|
if middleware is not None:
|
2020-05-09 14:15:16 +03:00
|
|
|
if isinstance(middleware, MiddlewareManager):
|
|
|
|
self.middleware = middleware
|
|
|
|
else:
|
|
|
|
self.middleware = list(instantiate_middleware(middleware))
|
2016-09-20 08:04:23 +03:00
|
|
|
self.root_value = root_value
|
2023-05-24 15:54:44 +03:00
|
|
|
self.pretty = pretty or self.pretty
|
|
|
|
self.graphiql = graphiql or self.graphiql
|
|
|
|
self.batch = batch or self.batch
|
|
|
|
self.execution_context_class = (
|
|
|
|
execution_context_class or self.execution_context_class
|
|
|
|
)
|
2020-07-12 16:42:31 +03:00
|
|
|
if subscription_path is None:
|
2020-07-12 22:48:12 +03:00
|
|
|
self.subscription_path = graphene_settings.SUBSCRIPTION_PATH
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2017-10-25 20:54:13 +03:00
|
|
|
assert isinstance(
|
2020-05-09 14:13:47 +03:00
|
|
|
self.schema, Schema
|
2018-07-20 02:51:33 +03:00
|
|
|
), "A Schema is required to be provided to GraphQLView."
|
|
|
|
assert not all((graphiql, batch)), "Use either graphiql or batch processing"
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2023-12-20 12:48:45 +03:00
|
|
|
self.validation_rules = validation_rules or self.validation_rules
|
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
def get_root_value(self, request):
|
|
|
|
return self.root_value
|
|
|
|
|
|
|
|
def get_middleware(self, request):
|
|
|
|
return self.middleware
|
|
|
|
|
|
|
|
def get_context(self, request):
|
|
|
|
return request
|
|
|
|
|
2016-09-20 09:14:59 +03:00
|
|
|
@method_decorator(ensure_csrf_cookie)
|
2016-09-20 08:04:23 +03:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
2018-07-20 02:51:33 +03:00
|
|
|
if request.method.lower() not in ("get", "post"):
|
|
|
|
raise HttpError(
|
|
|
|
HttpResponseNotAllowed(
|
|
|
|
["GET", "POST"], "GraphQL only supports GET and POST requests."
|
|
|
|
)
|
|
|
|
)
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
data = self.parse_body(request)
|
2018-07-20 02:51:33 +03:00
|
|
|
show_graphiql = self.graphiql and self.can_display_graphiql(request, data)
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2018-08-30 21:29:33 +03:00
|
|
|
if show_graphiql:
|
|
|
|
return self.render_graphiql(
|
2019-05-20 14:41:25 +03:00
|
|
|
request,
|
2020-07-12 22:48:12 +03:00
|
|
|
# Dependency parameters.
|
|
|
|
whatwg_fetch_version=self.whatwg_fetch_version,
|
|
|
|
whatwg_fetch_sri=self.whatwg_fetch_sri,
|
2019-05-20 14:41:25 +03:00
|
|
|
react_version=self.react_version,
|
2020-07-12 22:48:12 +03:00
|
|
|
react_sri=self.react_sri,
|
|
|
|
react_dom_sri=self.react_dom_sri,
|
|
|
|
graphiql_version=self.graphiql_version,
|
|
|
|
graphiql_sri=self.graphiql_sri,
|
|
|
|
graphiql_css_sri=self.graphiql_css_sri,
|
2020-07-12 16:42:31 +03:00
|
|
|
subscriptions_transport_ws_version=self.subscriptions_transport_ws_version,
|
2020-07-12 22:48:12 +03:00
|
|
|
subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri,
|
2023-05-04 22:06:10 +03:00
|
|
|
graphiql_plugin_explorer_version=self.graphiql_plugin_explorer_version,
|
|
|
|
graphiql_plugin_explorer_sri=self.graphiql_plugin_explorer_sri,
|
2023-09-13 19:26:18 +03:00
|
|
|
graphiql_plugin_explorer_css_sri=self.graphiql_plugin_explorer_css_sri,
|
2020-07-12 22:48:12 +03:00
|
|
|
# The SUBSCRIPTION_PATH setting.
|
2020-07-12 16:42:31 +03:00
|
|
|
subscription_path=self.subscription_path,
|
2020-08-07 12:13:26 +03:00
|
|
|
# GraphiQL headers tab,
|
|
|
|
graphiql_header_editor_enabled=graphene_settings.GRAPHIQL_HEADER_EDITOR_ENABLED,
|
2022-09-24 17:41:14 +03:00
|
|
|
graphiql_should_persist_headers=graphene_settings.GRAPHIQL_SHOULD_PERSIST_HEADERS,
|
2023-09-13 09:49:01 +03:00
|
|
|
graphiql_input_value_deprecation=graphene_settings.GRAPHIQL_INPUT_VALUE_DEPRECATION,
|
2018-08-30 21:29:33 +03:00
|
|
|
)
|
|
|
|
|
2016-10-31 13:56:51 +03:00
|
|
|
if self.batch:
|
|
|
|
responses = [self.get_response(request, entry) for entry in data]
|
2018-07-20 02:51:33 +03:00
|
|
|
result = "[{}]".format(
|
|
|
|
",".join([response[0] for response in responses])
|
|
|
|
)
|
|
|
|
status_code = (
|
|
|
|
responses
|
|
|
|
and max(responses, key=lambda response: response[1])[1]
|
|
|
|
or 200
|
|
|
|
)
|
2016-09-20 08:04:23 +03:00
|
|
|
else:
|
2018-07-20 02:51:33 +03:00
|
|
|
result, status_code = self.get_response(request, data, show_graphiql)
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
return HttpResponse(
|
2018-07-20 02:51:33 +03:00
|
|
|
status=status_code, content=result, content_type="application/json"
|
2016-09-20 08:04:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
except HttpError as e:
|
|
|
|
response = e.response
|
2018-07-20 02:51:33 +03:00
|
|
|
response["Content-Type"] = "application/json"
|
|
|
|
response.content = self.json_encode(
|
|
|
|
request, {"errors": [self.format_error(e)]}
|
|
|
|
)
|
2016-09-20 08:04:23 +03:00
|
|
|
return response
|
|
|
|
|
2016-10-31 13:56:51 +03:00
|
|
|
def get_response(self, request, data, show_graphiql=False):
|
2018-07-20 02:51:33 +03:00
|
|
|
query, variables, operation_name, id = self.get_graphql_params(request, data)
|
2016-10-31 13:56:51 +03:00
|
|
|
|
|
|
|
execution_result = self.execute_graphql_request(
|
2018-07-20 02:51:33 +03:00
|
|
|
request, data, query, variables, operation_name, show_graphiql
|
2016-10-31 13:56:51 +03:00
|
|
|
)
|
|
|
|
|
2020-12-31 02:37:57 +03:00
|
|
|
if getattr(request, MUTATION_ERRORS_FLAG, False) is True:
|
|
|
|
set_rollback()
|
|
|
|
|
2016-10-31 14:16:58 +03:00
|
|
|
status_code = 200
|
2016-10-31 13:56:51 +03:00
|
|
|
if execution_result:
|
|
|
|
response = {}
|
|
|
|
|
|
|
|
if execution_result.errors:
|
2020-12-31 02:37:57 +03:00
|
|
|
set_rollback()
|
2018-07-20 02:51:33 +03:00
|
|
|
response["errors"] = [
|
|
|
|
self.format_error(e) for e in execution_result.errors
|
|
|
|
]
|
2016-10-31 13:56:51 +03:00
|
|
|
|
2020-05-09 14:13:47 +03:00
|
|
|
if execution_result.errors and any(
|
2020-12-31 02:37:57 +03:00
|
|
|
not getattr(e, "path", None) for e in execution_result.errors
|
2020-05-09 14:13:47 +03:00
|
|
|
):
|
2016-10-31 13:56:51 +03:00
|
|
|
status_code = 400
|
|
|
|
else:
|
2018-07-20 02:51:33 +03:00
|
|
|
response["data"] = execution_result.data
|
2016-10-31 13:56:51 +03:00
|
|
|
|
|
|
|
if self.batch:
|
2018-07-20 02:51:33 +03:00
|
|
|
response["id"] = id
|
|
|
|
response["status"] = status_code
|
2016-10-31 13:56:51 +03:00
|
|
|
|
|
|
|
result = self.json_encode(request, response, pretty=show_graphiql)
|
|
|
|
else:
|
|
|
|
result = None
|
|
|
|
|
|
|
|
return result, status_code
|
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
def render_graphiql(self, request, **data):
|
|
|
|
return render(request, self.graphiql_template, data)
|
|
|
|
|
|
|
|
def json_encode(self, request, d, pretty=False):
|
2018-07-20 02:51:33 +03:00
|
|
|
if not (self.pretty or pretty) and not request.GET.get("pretty"):
|
|
|
|
return json.dumps(d, separators=(",", ":"))
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
return json.dumps(d, sort_keys=True, indent=2, separators=(",", ": "))
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
def parse_body(self, request):
|
|
|
|
content_type = self.get_content_type(request)
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
if content_type == "application/graphql":
|
|
|
|
return {"query": request.body.decode()}
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
elif content_type == "application/json":
|
2017-04-12 13:25:51 +03:00
|
|
|
# noinspection PyBroadException
|
2016-09-20 08:04:23 +03:00
|
|
|
try:
|
2018-07-20 02:51:33 +03:00
|
|
|
body = request.body.decode("utf-8")
|
2017-04-12 13:25:51 +03:00
|
|
|
except Exception as e:
|
|
|
|
raise HttpError(HttpResponseBadRequest(str(e)))
|
|
|
|
|
|
|
|
try:
|
|
|
|
request_json = json.loads(body)
|
2016-10-31 13:56:51 +03:00
|
|
|
if self.batch:
|
2017-02-20 12:08:42 +03:00
|
|
|
assert isinstance(request_json, list), (
|
2018-07-20 02:51:33 +03:00
|
|
|
"Batch requests should receive a list, but received {}."
|
2017-02-20 12:08:42 +03:00
|
|
|
).format(repr(request_json))
|
2018-07-20 02:51:33 +03:00
|
|
|
assert (
|
|
|
|
len(request_json) > 0
|
|
|
|
), "Received an empty list in the batch request."
|
2016-10-31 13:56:51 +03:00
|
|
|
else:
|
2018-07-20 02:51:33 +03:00
|
|
|
assert isinstance(
|
|
|
|
request_json, dict
|
|
|
|
), "The received data is not a valid JSON query."
|
2016-09-20 08:04:23 +03:00
|
|
|
return request_json
|
2017-02-20 12:08:42 +03:00
|
|
|
except AssertionError as e:
|
|
|
|
raise HttpError(HttpResponseBadRequest(str(e)))
|
2017-04-12 13:25:51 +03:00
|
|
|
except (TypeError, ValueError):
|
2018-07-20 02:51:33 +03:00
|
|
|
raise HttpError(HttpResponseBadRequest("POST body sent invalid JSON."))
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
elif content_type in [
|
|
|
|
"application/x-www-form-urlencoded",
|
|
|
|
"multipart/form-data",
|
|
|
|
]:
|
2016-09-20 08:04:23 +03:00
|
|
|
return request.POST
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
def execute_graphql_request(
|
|
|
|
self, request, data, query, variables, operation_name, show_graphiql=False
|
|
|
|
):
|
2016-09-20 08:04:23 +03:00
|
|
|
if not query:
|
|
|
|
if show_graphiql:
|
|
|
|
return None
|
2018-07-20 02:51:33 +03:00
|
|
|
raise HttpError(HttpResponseBadRequest("Must provide query string."))
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2023-10-29 18:42:27 +03:00
|
|
|
schema = self.schema.graphql_schema
|
|
|
|
|
|
|
|
schema_validation_errors = validate_schema(schema)
|
|
|
|
if schema_validation_errors:
|
|
|
|
return ExecutionResult(data=None, errors=schema_validation_errors)
|
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
try:
|
2020-05-09 14:13:47 +03:00
|
|
|
document = parse(query)
|
2016-09-20 08:04:23 +03:00
|
|
|
except Exception as e:
|
2020-05-09 14:13:47 +03:00
|
|
|
return ExecutionResult(errors=[e])
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2023-10-29 18:42:27 +03:00
|
|
|
operation_ast = get_operation_ast(document, operation_name)
|
2016-09-20 08:15:10 +03:00
|
|
|
|
2023-10-29 18:42:27 +03:00
|
|
|
if (
|
|
|
|
request.method.lower() == "get"
|
|
|
|
and operation_ast is not None
|
|
|
|
and operation_ast.operation != OperationType.QUERY
|
|
|
|
):
|
|
|
|
if show_graphiql:
|
|
|
|
return None
|
|
|
|
|
|
|
|
raise HttpError(
|
|
|
|
HttpResponseNotAllowed(
|
|
|
|
["POST"],
|
|
|
|
"Can only perform a {} operation from a POST request.".format(
|
|
|
|
operation_ast.operation.value
|
|
|
|
),
|
2018-07-20 02:51:33 +03:00
|
|
|
)
|
2023-10-29 18:42:27 +03:00
|
|
|
)
|
|
|
|
|
2023-12-20 12:48:45 +03:00
|
|
|
validation_errors = validate(
|
|
|
|
schema,
|
|
|
|
document,
|
|
|
|
self.validation_rules,
|
|
|
|
graphene_settings.MAX_VALIDATION_ERRORS,
|
|
|
|
)
|
2023-10-29 18:42:27 +03:00
|
|
|
|
|
|
|
if validation_errors:
|
|
|
|
return ExecutionResult(data=None, errors=validation_errors)
|
2020-12-31 02:37:57 +03:00
|
|
|
|
2023-10-29 18:42:27 +03:00
|
|
|
try:
|
|
|
|
execute_options = {
|
2020-12-31 02:37:57 +03:00
|
|
|
"root_value": self.get_root_value(request),
|
2023-10-29 18:42:27 +03:00
|
|
|
"context_value": self.get_context(request),
|
2020-12-31 02:37:57 +03:00
|
|
|
"variable_values": variables,
|
|
|
|
"operation_name": operation_name,
|
|
|
|
"middleware": self.get_middleware(request),
|
|
|
|
}
|
2023-10-29 18:42:27 +03:00
|
|
|
if self.execution_context_class:
|
|
|
|
execute_options[
|
|
|
|
"execution_context_class"
|
|
|
|
] = self.execution_context_class
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
if (
|
2023-10-29 18:42:27 +03:00
|
|
|
operation_ast is not None
|
2020-12-31 02:37:57 +03:00
|
|
|
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():
|
2023-10-29 18:42:27 +03:00
|
|
|
result = execute(schema, document, **execute_options)
|
2020-12-31 02:37:57 +03:00
|
|
|
if getattr(request, MUTATION_ERRORS_FLAG, False) is True:
|
|
|
|
transaction.set_rollback(True)
|
|
|
|
return result
|
|
|
|
|
2023-10-29 18:42:27 +03:00
|
|
|
return execute(schema, document, **execute_options)
|
2020-12-31 02:37:57 +03:00
|
|
|
except Exception as e:
|
|
|
|
return ExecutionResult(errors=[e])
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def can_display_graphiql(cls, request, data):
|
2018-07-20 02:51:33 +03:00
|
|
|
raw = "raw" in request.GET or "raw" in data
|
2016-09-20 08:04:23 +03:00
|
|
|
return not raw and cls.request_wants_html(request)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def request_wants_html(cls, request):
|
|
|
|
accepted = get_accepted_content_types(request)
|
2017-12-12 05:08:42 +03:00
|
|
|
accepted_length = len(accepted)
|
2017-12-18 19:40:19 +03:00
|
|
|
# the list will be ordered in preferred first - so we have to make
|
|
|
|
# sure the most preferred gets the highest number
|
2018-07-20 02:51:33 +03:00
|
|
|
html_priority = (
|
|
|
|
accepted_length - accepted.index("text/html")
|
|
|
|
if "text/html" in accepted
|
|
|
|
else 0
|
|
|
|
)
|
|
|
|
json_priority = (
|
|
|
|
accepted_length - accepted.index("application/json")
|
|
|
|
if "application/json" in accepted
|
|
|
|
else 0
|
|
|
|
)
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2017-12-17 03:32:01 +03:00
|
|
|
return html_priority > json_priority
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_graphql_params(request, data):
|
2018-07-20 02:51:33 +03:00
|
|
|
query = request.GET.get("query") or data.get("query")
|
|
|
|
variables = request.GET.get("variables") or data.get("variables")
|
|
|
|
id = request.GET.get("id") or data.get("id")
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2020-04-06 15:21:07 +03:00
|
|
|
if variables and isinstance(variables, str):
|
2016-09-20 08:04:23 +03:00
|
|
|
try:
|
|
|
|
variables = json.loads(variables)
|
2017-10-25 20:54:13 +03:00
|
|
|
except Exception:
|
2018-07-20 02:51:33 +03:00
|
|
|
raise HttpError(HttpResponseBadRequest("Variables are invalid JSON."))
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
operation_name = request.GET.get("operationName") or data.get("operationName")
|
2017-04-13 20:11:10 +03:00
|
|
|
if operation_name == "null":
|
|
|
|
operation_name = None
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2016-10-31 13:56:51 +03:00
|
|
|
return query, variables, operation_name, id
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def format_error(error):
|
|
|
|
if isinstance(error, GraphQLError):
|
2022-08-15 12:41:39 +03:00
|
|
|
return error.formatted
|
2016-09-20 08:04:23 +03:00
|
|
|
|
2020-04-06 15:21:07 +03:00
|
|
|
return {"message": str(error)}
|
2016-09-20 08:04:23 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_content_type(request):
|
|
|
|
meta = request.META
|
2018-07-20 02:51:33 +03:00
|
|
|
content_type = meta.get("CONTENT_TYPE", meta.get("HTTP_CONTENT_TYPE", ""))
|
|
|
|
return content_type.split(";", 1)[0].lower()
|