11# -*- coding: utf-8 -*-
22from ..error import GraphQLError
33from ..language import ast
4+ from ..pyutils .default_ordered_dict import DefaultOrderedDict
45from ..type .definition import GraphQLInterfaceType , GraphQLUnionType
56from ..type .directives import GraphQLIncludeDirective , GraphQLSkipDirective
67from ..type .introspection import (SchemaMetaFieldDef , TypeMetaFieldDef ,
@@ -18,9 +19,9 @@ class ExecutionContext(object):
1819 and the fragments defined in the query document"""
1920
2021 __slots__ = 'schema' , 'fragments' , 'root_value' , 'operation' , 'variable_values' , 'errors' , 'context_value' , \
21- 'argument_values_cache' , 'executor'
22+ 'argument_values_cache' , 'executor' , 'middleware' , '_subfields_cache'
2223
23- def __init__ (self , schema , document_ast , root_value , context_value , variable_values , operation_name , executor ):
24+ def __init__ (self , schema , document_ast , root_value , context_value , variable_values , operation_name , executor , middleware ):
2425 """Constructs a ExecutionContext object from the arguments passed
2526 to execute, which we will pass throughout the other execution
2627 methods."""
@@ -63,6 +64,13 @@ def __init__(self, schema, document_ast, root_value, context_value, variable_val
6364 self .context_value = context_value
6465 self .argument_values_cache = {}
6566 self .executor = executor
67+ self .middleware = middleware
68+ self ._subfields_cache = {}
69+
70+ def get_field_resolver (self , field_resolver ):
71+ if not self .middleware :
72+ return field_resolver
73+ return self .middleware .get_field_resolver (field_resolver )
6674
6775 def get_argument_values (self , field_def , field_ast ):
6876 k = field_def , field_ast
@@ -74,6 +82,21 @@ def get_argument_values(self, field_def, field_ast):
7482
7583 return result
7684
85+ def get_sub_fields (self , return_type , field_asts ):
86+ k = return_type , tuple (field_asts )
87+ if k not in self ._subfields_cache :
88+ subfield_asts = DefaultOrderedDict (list )
89+ visited_fragment_names = set ()
90+ for field_ast in field_asts :
91+ selection_set = field_ast .selection_set
92+ if selection_set :
93+ subfield_asts = collect_fields (
94+ self , return_type , selection_set ,
95+ subfield_asts , visited_fragment_names
96+ )
97+ self ._subfields_cache [k ] = subfield_asts
98+ return self ._subfields_cache [k ]
99+
77100
78101class ExecutionResult (object ):
79102 """The result of execution. `data` is the result of executing the
@@ -245,6 +268,8 @@ def get_field_entry_key(node):
245268
246269
247270class ResolveInfo (object ):
271+ __slots__ = ('field_name' , 'field_asts' , 'return_type' , 'parent_type' ,
272+ 'schema' , 'fragments' , 'root_value' , 'operation' , 'variable_values' )
248273
249274 def __init__ (self , field_name , field_asts , return_type , parent_type ,
250275 schema , fragments , root_value , operation , variable_values ):
@@ -277,10 +302,10 @@ def get_field_def(schema, parent_type, field_name):
277302 are allowed, like on a Union. __schema could get automatically
278303 added to the query type, but that would require mutating type
279304 definitions, which would cause issues."""
280- if field_name == SchemaMetaFieldDef . name and schema .get_query_type () == parent_type :
305+ if field_name == '__schema' and schema .get_query_type () == parent_type :
281306 return SchemaMetaFieldDef
282- elif field_name == TypeMetaFieldDef . name and schema .get_query_type () == parent_type :
307+ elif field_name == '__type' and schema .get_query_type () == parent_type :
283308 return TypeMetaFieldDef
284- elif field_name == TypeNameMetaFieldDef . name :
309+ elif field_name == '__typename' :
285310 return TypeNameMetaFieldDef
286- return parent_type .get_fields () .get (field_name )
311+ return parent_type .fields .get (field_name )
0 commit comments