mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-18 12:30:37 +03:00
Improved Django view exception handling
This commit is contained in:
parent
47bd3d3926
commit
5b415a1de6
|
@ -2,6 +2,7 @@ import json
|
||||||
|
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from graphql.core.error import GraphQLError, format_error
|
from graphql.core.error import GraphQLError, format_error
|
||||||
|
|
||||||
|
@ -14,6 +15,7 @@ def form_error(error):
|
||||||
|
|
||||||
class GraphQLView(View):
|
class GraphQLView(View):
|
||||||
schema = None
|
schema = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_result(result):
|
def format_result(result):
|
||||||
data = {'data': result.data}
|
data = {'data': result.data}
|
||||||
|
@ -34,6 +36,8 @@ class GraphQLView(View):
|
||||||
result = self.schema.execute(query, root=object())
|
result = self.schema.execute(query, root=object())
|
||||||
data = self.format_result(result)
|
data = self.format_result(result)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
if settings.DEBUG:
|
||||||
|
raise e
|
||||||
data = {
|
data = {
|
||||||
"errors": [{"message": str(e)}]
|
"errors": [{"message": str(e)}]
|
||||||
}
|
}
|
||||||
|
@ -41,13 +45,14 @@ class GraphQLView(View):
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
query = request.GET.get('query') or request.GET.get('q') or ''
|
query = request.GET.get('query')
|
||||||
return self.execute_query(request, query)
|
return self.execute_query(request, query or '')
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
if request.body:
|
if request.body:
|
||||||
received_json_data = json.loads(request.body)
|
received_json_data = json.loads(request.body)
|
||||||
query = received_json_data.get('query') or ''
|
query = received_json_data.get('query')
|
||||||
else:
|
else:
|
||||||
query = request.POST.get('query') or request.POST.get('q')
|
query = request.POST.get('query') or request.GET.get('query')
|
||||||
return self.execute_query(request, query)
|
raise Exception(query)
|
||||||
|
return self.execute_query(request, query or '')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user