1414 collect_fields , default_resolve_fn , get_field_def ,
1515 get_operation_root_type )
1616from .executors .sync import SyncExecutor
17+ from .middleware import MiddlewareManager
1718
1819logger = logging .getLogger (__name__ )
1920
2021
2122def execute (schema , document_ast , root_value = None , context_value = None ,
2223 variable_values = None , operation_name = None , executor = None ,
23- return_promise = False ):
24+ return_promise = False , middlewares = None ):
2425 assert schema , 'Must provide schema'
2526 assert isinstance (schema , GraphQLSchema ), (
2627 'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
2728 'not multiple versions of GraphQL installed in your node_modules directory.'
2829 )
30+ if middlewares :
31+ assert isinstance (middlewares , MiddlewareManager ), (
32+ 'middlewares have to be an instance'
33+ ' of MiddlewareManager. Received "{}".' .format (middlewares )
34+ )
2935
3036 if executor is None :
3137 executor = SyncExecutor ()
@@ -37,7 +43,8 @@ def execute(schema, document_ast, root_value=None, context_value=None,
3743 context_value ,
3844 variable_values ,
3945 operation_name ,
40- executor
46+ executor ,
47+ middlewares
4148 )
4249
4350 def executor (resolve , reject ):
@@ -132,6 +139,9 @@ def resolve_field(exe_context, parent_type, source, field_asts):
132139 return_type = field_def .type
133140 resolve_fn = field_def .resolver or default_resolve_fn
134141
142+ # We wrap the resolve_fn from the middleware
143+ resolve_fn_middleware = exe_context .get_field_resolver (resolve_fn )
144+
135145 # Build a dict of arguments from the field.arguments AST, using the variables scope to
136146 # fulfill any variable references.
137147 args = exe_context .get_argument_values (field_def , field_ast )
@@ -156,7 +166,7 @@ def resolve_field(exe_context, parent_type, source, field_asts):
156166 )
157167
158168 executor = exe_context .executor
159- result = resolve_or_error (resolve_fn , source , args , context , info , executor )
169+ result = resolve_or_error (resolve_fn_middleware , source , args , context , info , executor )
160170
161171 return complete_value_catching_error (
162172 exe_context ,
0 commit comments