4
4
import six
5
5
from flask import Response , request
6
6
from flask .views import View
7
- from werkzeug .exceptions import BadRequest , MethodNotAllowed
8
7
9
8
from graphql import Source , execute , parse , validate
10
9
from graphql .error import format_error as format_graphql_error
16
15
from .render_graphiql import render_graphiql
17
16
18
17
19
- class HttpError (Exception ):
18
+ class HttpQueryError (Exception ):
20
19
def __init__ (self , status_code , message = None , is_graphql_error = False , headers = None ):
21
20
self .status_code = status_code
22
21
self .message = message
23
22
self .is_graphql_error = is_graphql_error
24
23
self .headers = headers
25
- super (HttpError , self ).__init__ (message )
24
+ super (HttpQueryError , self ).__init__ (message )
26
25
27
26
28
27
class GraphQLView (View ):
@@ -70,10 +69,11 @@ def render_graphiql(self, **kwargs):
70
69
)
71
70
72
71
def dispatch_request (self ):
72
+
73
73
try :
74
74
request_method = request .method .lower ()
75
75
if request_method not in ('get' , 'post' ):
76
- raise HttpError (
76
+ raise HttpQueryError (
77
77
405 ,
78
78
'GraphQL only supports GET and POST requests.' ,
79
79
headers = {
@@ -91,7 +91,7 @@ def dispatch_request(self):
91
91
data = dict (data , ** request .args .to_dict ())
92
92
data = [data ]
93
93
elif not self .batch :
94
- raise HttpError (
94
+ raise HttpQueryError (
95
95
400 ,
96
96
'Batch requests are not allowed.'
97
97
)
@@ -129,7 +129,7 @@ def dispatch_request(self):
129
129
content_type = 'application/json'
130
130
)
131
131
132
- except HttpError as e :
132
+ except HttpQueryError as e :
133
133
return Response (
134
134
self .json_encode ({
135
135
'errors' : [self .format_error<
10000
/span>(e )]
@@ -151,7 +151,7 @@ def get_response(self, execute, data, show_graphiql=False, only_allow_query=Fals
151
151
operation_name ,
152
152
only_allow_query ,
153
153
)
154
- except HttpError :
154
+ except HttpQueryError :
155
155
if show_graphiql :
156
156
execution_result = None
157
157
else :
@@ -193,7 +193,7 @@ def parse_body(self, request):
193
193
try :
194
194
return json .loads (request .data .decode ('utf8' ))
195
195
except :
196
- raise HttpError (
196
+ raise HttpQueryError (
197
197
400 ,
198
198
'POST body sent invalid JSON.'
199
199
)
@@ -225,7 +225,7 @@ def execute(self, schema, *args, **kwargs):
225
225
@staticmethod
226
226
def execute_graphql_request (schema , execute , data , query , variables , operation_name , only_allow_query = False ):
227
227
if not query :
228
- raise HttpError (400 , 'Must provide query string.' )
228
+ raise HttpQueryError (400 , 'Must provide query string.' )
229
229
230
230
try :
231
231
source = Source (query , name = 'GraphQL request' )
@@ -242,7 +242,7 @@ def execute_graphql_request(schema, execute, data, query, variables, operation_n
242
242
if only_allow_query :
243
243
operation_ast = get_operation_ast (ast , operation_name )
244
244
if operation_ast and operation_ast .operation != 'query' :
245
- raise HttpError (
245
+ raise HttpQueryError (
246
246
405 ,
247
247
'Can only perform a {} operation from a POST request.' .format (operation_ast .operation ),
248
248
headers = {
@@ -294,7 +294,7 @@ def get_graphql_params(data):
294
294
try :
295
295
variables = json .loads (variables )
296
296
except :
297
- raise HttpError (400 , 'Variables are invalid JSON.' )
297
+ raise HttpQueryError (400 , 'Variables are invalid JSON.' )
298
298
299
299
operation_name =<
3CA3
/span> data .get ('operationName' )
300
300
0 commit comments