44import six
55from flask import Response , request
66from flask .views import View
7- from werkzeug .exceptions import BadRequest , MethodNotAllowed
87
98from graphql import Source , execute , parse , validate
109from graphql .error import format_error as format_graphql_error
1615from .render_graphiql import render_graphiql
1716
1817
19- class HttpError (Exception ):
18+ class HttpQueryError (Exception ):
2019 def __init__ (self , status_code , message = None , is_graphql_error = False , headers = None ):
2120 self .status_code = status_code
2221 self .message = message
2322 self .is_graphql_error = is_graphql_error
2423 self .headers = headers
25- super (HttpError , self ).__init__ (message )
24+ super (HttpQueryError , self ).__init__ (message )
2625
2726
2827class GraphQLView (View ):
@@ -70,10 +69,11 @@ def render_graphiql(self, **kwargs):
7069 )
7170
7271 def dispatch_request (self ):
72+
7373 try :
7474 request_method = request .method .lower ()
7575 if request_method not in ('get' , 'post' ):
76- raise HttpError (
76+ raise HttpQueryError (
7777 405 ,
7878 'GraphQL only supports GET and POST requests.' ,
7979 headers = {
@@ -91,7 +91,7 @@ def dispatch_request(self):
9191 data = dict (data , ** request .args .to_dict ())
9292 data = [data ]
9393 elif not self .batch :
94- raise HttpError (
94+ raise HttpQueryError (
9595 400 ,
9696 'Batch requests are not allowed.'
9797 )
@@ -129,7 +129,7 @@ def dispatch_request(self):
129129 content_type = 'application/json'
130130 )
131131
132- except HttpError as e :
132+ except HttpQueryError as e :
133133 return Response (
134134 self .json_encode ({
135135 'errors' : [self .format_error (e )]
@@ -151,7 +151,7 @@ def get_response(self, execute, data, show_graphiql=False, only_allow_query=Fals
151151 operation_name ,
152152 only_allow_query ,
153153 )
154- except HttpError :
154+ except HttpQueryError :
155155 if show_graphiql :
156156 execution_result = None
157157 else :
@@ -193,7 +193,7 @@ def parse_body(self, request):
193193 try :
194194 return json .loads (request .data .decode ('utf8' ))
195195 except :
196- raise HttpError (
196+ raise HttpQueryError (
197197 400 ,
198198 'POST body sent invalid JSON.'
199199 )
@@ -225,7 +225,7 @@ def execute(self, schema, *args, **kwargs):
225225 @staticmethod
226226 def execute_graphql_request (schema , execute , data , query , variables , operation_name , only_allow_query = False ):
227227 if not query :
228- raise HttpError (400 , 'Must provide query string.' )
228+ raise HttpQueryError (400 , 'Must provide query string.' )
229229
230230 try :
231231 source = Source (query , name = 'GraphQL request' )
@@ -242,7 +242,7 @@ def execute_graphql_request(schema, execute, data, query, variables, operation_n
242242 if only_allow_query :
243243 operation_ast = get_operation_ast (ast , operation_name )
244244 if operation_ast and operation_ast .operation != 'query' :
245- raise HttpError (
245+ raise HttpQueryError (
246246 405 ,
247247 'Can only perform a {} operation from a POST request.' .format (operation_ast .operation ),
248248 headers = {
@@ -294,7 +294,7 @@ def get_graphql_params(data):
294294 try :
295295 variables = json .loads (variables )
296296 except :
297- raise HttpError (400 , 'Variables are invalid JSON.' )
297+ raise HttpQueryError (400 , 'Variables are invalid JSON.' )
298298
299299 operation_name = data .get ('operationName' )
300300
0 commit comments