@@ -148,7 +148,15 @@ def get_operation_root_type(schema, operation):
148148 )
149149
150150
151- def collect_fields (ctx , type , selection_set , fields , prev_fragment_names ):
151+ def collect_fields (ctx , runtime_type , selection_set , fields , prev_fragment_names ):
152+ """
153+ Given a selectionSet, adds all of the fields in that selection to
154+ the passed in map of fields, and returns it at the end.
155+
156+ collect_fields requires the "runtime type" of an object. For a field which
157+ returns and Interface or Union type, the "runtime type" will be the actual
158+ Object type returned by that field.
159+ """
152160 for selection in selection_set .selections :
153161 directives = selection .directives
154162
@@ -160,10 +168,10 @@ def collect_fields(ctx, type, selection_set, fields, prev_fragment_names):
160168 fields [name ].append (selection )
161169
162170 elif isinstance (selection , ast .InlineFragment ):
163- if not should_include_node (ctx , directives ) or not does_fragment_condition_match (ctx , selection , type ):
171+ if not should_include_node (ctx , directives ) or not does_fragment_condition_match (ctx , selection , runtime_type ):
164172 continue
165173
166- collect_fields (ctx , type , selection .selection_set , fields , prev_fragment_names )
174+ collect_fields (ctx , runtime_type , selection .selection_set , fields , prev_fragment_names )
167175
168176 elif isinstance (selection , ast .FragmentSpread ):
169177 frag_name = selection .name .value
@@ -176,10 +184,10 @@ def collect_fields(ctx, type, selection_set, fields, prev_fragment_names):
176184 frag_directives = fragment .directives
177185 if not fragment or not \
178186 should_include_node (ctx , frag_directives ) or not \
179- does_fragment_condition_match (ctx , fragment , type ):
187+ does_fragment_condition_match (ctx , fragment , runtime_type ):
180188 continue
181189
182- collect_fields (ctx , type , fragment .selection_set , fields , prev_fragment_names )
190+ collect_fields (ctx , runtime_type , fragment .selection_set , fields , prev_fragment_names )
183191
184192 return fields
185193
0 commit comments