Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Support GET method for graphql

jxltom08fe4c136e01
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index d8edafaadf..1d884ff54e 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -1,5 +1,6 @@
import json
+import six
from django.conf import settings
from django.http import HttpRequest, HttpResponseNotAllowed, JsonResponse
from django.shortcuts import render_to_response
@@ -46,8 +47,10 @@ class GraphQLView(View):
def dispatch(self, request, *args, **kwargs):
# Handle options method the GraphQlView restricts it.
if request.method == 'GET':
- return render_to_response('graphql/playground.html')
- if request.method == 'OPTIONS':
+ if settings.DEBUG:
+ return render_to_response('graphql/playground.html')
+ response = self.handle_query(request, *args, **kwargs)
+ elif request.method == 'OPTIONS':
response = self.options(request, *args, **kwargs)
elif request.method == 'POST':
response = self.handle_query(request, *args, **kwargs)
@@ -80,8 +83,12 @@ 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)
+ try:
+ query, variables, operation_name = self.get_graphql_params(
+ request, data)
+ except ValueError as e:
+ return str(e), 400
+
execution_result = self.execute_graphql_request(
request, query, variables, operation_name)
status_code = 200
@@ -144,11 +151,20 @@ class GraphQLView(View):
@staticmethod
def get_graphql_params(request: HttpRequest, data: dict):
- query = data.get('query')
- variables = data.get('variables')
- operation_name = data.get('operationName')
- if operation_name == 'null':
+ query = request.GET.get("query") or data.get("query")
+ variables = request.GET.get("variables") or data.get("variables")
+
+ if variables and isinstance(variables, six.text_type):
+ try:
+ variables = json.loads(variables)
+ except Exception:
+ raise ValueError("Variables are invalid JSON.")
+
+ operation_name = request.GET.get(
+ "operationName") or data.get("operationName")
+ if operation_name == "null":
operation_name = None
+
if request.content_type == 'multipart/form-data':
operations = json.loads(data.get('operations', '{}'))
files_map = json.loads(data.get('map', '{}'))