Improved Django GraphQL view error handling

This commit is contained in:
Syrus Akbary 2015-10-03 02:10:03 -07:00
parent dcd8edb59a
commit b0e3b3a3af

View File

@ -23,8 +23,21 @@ class GraphQLView(View):
return data return data
def execute_query(self, request, query): def execute_query(self, request, query):
if not query:
data = {
"errors": [{
"message": "Must provide query string."
}]
}
else:
try:
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:
data = {
"errors": [{"message": str(e)}]
}
return JsonResponse(data) return JsonResponse(data)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):