1
1
import json
2
2
3
+ import six
3
4
from flask import Response , request
4
5
from 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
11
6
from werkzeug .exceptions import BadRequest , MethodNotAllowed
12
7
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
+
13
15
14
16
class HttpError (Exception ):
15
17
def __init__ (self , response , message = None , * args , ** kwargs ):
@@ -34,11 +36,7 @@ def __init__(self, **kwargs):
34
36
setattr (self , key , value )
35
37
36
38
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 )
42
40
43
41
if inner_schema :
44
42
self .schema = inner_schema
@@ -49,7 +47,7 @@ def __init__(self, **kwargs):
49
47
def get_root_value (self , request ):
50
48
return self .root_value
51
49
52
- def get_request_context (self , request ):
50
+ def get_context (self , request ):
53
51
return request
54
52
55
53
def dispatch_request (self ):
@@ -114,10 +112,10 @@ def parse_body(self, request):
114
112
115
113
return {}
116
114
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 )
121
119
122
120
def execute_graphql_request (self , request ):
123
121
query , variables , operation_name = self .get_graphql_params (request , self .parse_body (request ))
@@ -129,6 +127,12 @@ def execute_graphql_request(self, request):
129
127
130
128
try :
131
129
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
+ )
132
136
except Exception as e :
133
137
return ExecutionResult (errors = [e ], invalid = True )
134
138
@@ -140,12 +144,12 @@ def execute_graphql_request(self, request):
140
144
))
141
145
142
146
try :
143
- return self ._execute (
147
+ return self .execute (
144
148
document_ast ,
145
- self .get_root_value (request ),
146
- variables ,
149
+ root_value = self .get_root_value (request ),
150
+ variable_values = variables ,
147
151
operation_name = operation_name ,
148
- request_context = self .get_request_context (request )
152
+ context_value = self .get_context (request )
149
153
)
150
154
except Exception as e :
151
155
return ExecutionResult (errors = [e ], invalid = True )
0 commit comments