11import json
22
3+ import six
34from flask import Response , request
45from flask .views import View
5- from graphql .core import Source , parse
6- from graphql .core .error import GraphQLError , format_error as format_graphql_error
7- from graphql .core .execution import ExecutionResult , get_default_executor
8- from graphql .core .type .schema import GraphQLSchema
9- from graphql .core .utils .get_operation_ast import get_operation_ast
10- import six
116from werkzeug .exceptions import BadRequest , MethodNotAllowed
127
8+ from graphql import Source , execute , parse , validate
9+ from graphql .error import format_error as format_graphql_error
10+ from graphql .error import GraphQLError
11+ from graphql .execution import ExecutionResult
12+ from graphql .type .schema import GraphQLSchema
13+ from graphql .utils .get_operation_ast import get_operation_ast
14+
1315
1416class HttpError (Exception ):
1517 def __init__ (self , response , message = None , * args , ** kwargs ):
@@ -34,11 +36,7 @@ def __init__(self, **kwargs):
3436 setattr (self , key , value )
3537
3638 inner_schema = getattr (self .schema , 'schema' , None )
37- execute = getattr (self .schema , 'execute' , None )
38- if execute :
39- self .execute = execute
40- elif not self .executor :
41- self .executor = get_default_executor ()
39+ self ._execute = getattr (self .schema , 'execute' , None )
4240
4341 if inner_schema :
4442 self .schema = inner_schema
@@ -49,7 +47,7 @@ def __init__(self, **kwargs):
4947 def get_root_value (self , request ):
5048 return self .root_value
5149
52- def get_request_context (self , request ):
50+ def get_context (self , request ):
5351 return request
5452
5553 def dispatch_request (self ):
@@ -114,10 +112,10 @@ def parse_body(self, request):
114112
115113 return {}
116114
117- def _execute (self , * args , ** kwargs ):
118- if self .execute :
119- return self .execute (* args , ** kwargs )
120- return self . executor . execute (self .schema , * args , ** kwargs )
115+ def execute (self , * args , ** kwargs ):
116+ if self ._execute :
117+ return self ._execute (* args , ** kwargs )
118+ return execute (self .schema , * args , ** kwargs )
121119
122120 def execute_graphql_request (self , request ):
123121 query , variables , operation_name = self .get_graphql_params (request , self .parse_body (request ))
@@ -129,6 +127,12 @@ def execute_graphql_request(self, request):
129127
130128 try :
131129 document_ast = parse (source )
130+ validation_errors = validate (self .schema , document_ast )
131+ if validation_errors :
132+ return ExecutionResult (
133+ errors = validation_errors ,
134+ invalid = True ,
135+ )
132136 except Exception as e :
133137 return ExecutionResult (errors = [e ], invalid = True )
134138
@@ -140,12 +144,12 @@ def execute_graphql_request(self, request):
140144 ))
141145
142146 try :
143- return self ._execute (
147+ return self .execute (
144148 document_ast ,
145- self .get_root_value (request ),
146- variables ,
149+ root_value = self .get_root_value (request ),
150+ variable_values = variables ,
147151 operation_name = operation_name ,
148- request_context = self .get_request_context (request )
152+ context_value = self .get_context (request )
149153 )
150154 except Exception as e :
151155 return ExecutionResult (errors = [e ], invalid = True )
0 commit comments