| 1 |
from typing import Any, Dict, List, Set, Union, cast |
| 2 |
|
| 3 |
from ..language import ( |
| 4 |
FieldNode, |
| 5 |
FragmentDefinitionNode, |
| 6 |
FragmentSpreadNode, |
| 7 |
InlineFragmentNode, |
| 8 |
SelectionSetNode, |
| 9 |
) |
| 10 |
from ..type import ( |
| 11 |
GraphQLAbstractType, |
| 12 |
GraphQLIncludeDirective, |
| 13 |
GraphQLObjectType, |
| 14 |
GraphQLSchema, |
| 15 |
GraphQLSkipDirective, |
| 16 |
is_abstract_type, |
| 17 |
) |
| 18 |
from ..utilities.type_from_ast import type_from_ast |
| 19 |
from .values import get_directive_values |
| 20 |
|
| 21 |
__all__ = ["collect_fields", "collect_sub_fields"] |
| 22 |
|
| 23 |
|
| 24 |
def collect_fields( |
| 25 |
schema: GraphQLSchema, |
| 26 |
fragments: Dict[str, FragmentDefinitionNode], |
| 27 |
variable_values: Dict[str, Any], |
| 28 |
runtime_type: GraphQLObjectType, |
| 29 |
selection_set: SelectionSetNode, |
| 30 |
) -> Dict[str, List[FieldNode]]: |
| 31 |
"""Collect fields. |
| 32 |
|
| 33 |
Given a selection_set, collects all the fields and returns them. |
| 34 |
|
| 35 |
collect_fields requires the "runtime type" of an object. For a field that |
| 36 |
returns an Interface or Union type, the "runtime type" will be the actual |
| 37 |
object type returned by that field. |
| 38 |
|
| 39 |
For internal use only. |
| 40 |
""" |
| 41 |
fields: Dict[str, List[FieldNode]] = {} |
| 42 |
collect_fields_impl( |
| 43 |
schema, fragments, variable_values, runtime_type, selection_set, fields, set() |
| 44 |
) |
| 45 |
return fields |
| 46 |
|
| 47 |
|
| 48 |
def collect_sub_fields( |
| 49 |
schema: GraphQLSchema, |
| 50 |
fragments: Dict[str, FragmentDefinitionNode], |
| 51 |
variable_values: Dict[str, Any], |
| 52 |
return_type: GraphQLObjectType, |
| 53 |
field_nodes: List[FieldNode], |
| 54 |
) -> Dict[str, List[FieldNode]]: |
| 55 |
"""Collect sub fields. |
| 56 |
|
| 57 |
Given a list of field nodes, collects all the subfields of the passed in fields, |
| 58 |
and returns them at the end. |
| 59 |
|
| 60 |
collect_sub_fields requires the "return type" of an object. For a field that |
| 61 |
returns an Interface or Union type, the "return type" will be the actual |
| 62 |
object type returned by that field. |
| 63 |
|
| 64 |
For internal use only. |
| 65 |
""" |
| 66 |
sub_field_nodes: Dict[str, List[FieldNode]] = {} |
| 67 |
visited_fragment_names: Set[str] = set() |
| 68 |
for node in field_nodes: |
| 69 |
if node.selection_set: |
| 70 |
collect_fields_impl( |
| 71 |
schema, |
| 72 |
fragments, |
| 73 |
variable_values, |
| 74 |
return_type, |
| 75 |
node.selection_set, |
| 76 |
sub_field_nodes, |
| 77 |
visited_fragment_names, |
| 78 |
) |
| 79 |
return sub_field_nodes |
| 80 |
|
| 81 |
|
| 82 |
def collect_fields_impl( |
| 83 |
schema: GraphQLSchema, |
| 84 |
fragments: Dict[str, FragmentDefinitionNode], |
| 85 |
variable_values: Dict[str, Any], |
| 86 |
runtime_type: GraphQLObjectType, |
| 87 |
selection_set: SelectionSetNode, |
| 88 |
fields: Dict[str, List[FieldNode]], |
| 89 |
visited_fragment_names: Set[str], |
| 90 |
) -> None: |
| 91 |
"""Collect fields (internal implementation).""" |
| 92 |
for selection in selection_set.selections: |
| 93 |
if isinstance(selection, FieldNode): |
| 94 |
if not should_include_node(variable_values, selection): |
| 95 |
continue |
| 96 |
name = get_field_entry_key(selection) |
| 97 |
fields.setdefault(name, []).append(selection) |
| 98 |
elif isinstance(selection, InlineFragmentNode): |
| 99 |
if not should_include_node( |
| 100 |
variable_values, selection |
| 101 |
) or not does_fragment_condition_match(schema, selection, runtime_type): |
| 102 |
continue |
| 103 |
collect_fields_impl( |
| 104 |
schema, |
| 105 |
fragments, |
| 106 |
variable_values, |
| 107 |
runtime_type, |
| 108 |
selection.selection_set, |
| 109 |
fields, |
| 110 |
visited_fragment_names, |
| 111 |
) |
| 112 |
elif isinstance(selection, FragmentSpreadNode): # pragma: no cover else |
| 113 |
frag_name = selection.name.value |
| 114 |
if frag_name in visited_fragment_names or not should_include_node( |
| 115 |
variable_values, selection |
| 116 |
): |
| 117 |
continue |
| 118 |
visited_fragment_names.add(frag_name) |
| 119 |
fragment = fragments.get(frag_name) |
| 120 |
if not fragment or not does_fragment_condition_match( |
| 121 |
schema, fragment, runtime_type |
| 122 |
): |
| 123 |
continue |
| 124 |
collect_fields_impl( |
| 125 |
schema, |
| 126 |
fragments, |
| 127 |
variable_values, |
| 128 |
runtime_type, |
| 129 |
fragment.selection_set, |
| 130 |
fields, |
| 131 |
visited_fragment_names, |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
def should_include_node( |
| 136 |
variable_values: Dict[str, Any], |
| 137 |
node: Union[FragmentSpreadNode, FieldNode, InlineFragmentNode], |
| 138 |
) -> bool: |
| 139 |
"""Check if node should be included |
| 140 |
|
| 141 |
Determines if a field should be included based on the @include and @skip |
| 142 |
directives, where @skip has higher precedence than @include. |
| 143 |
""" |
| 144 |
skip = get_directive_values(GraphQLSkipDirective, node, variable_values) |
| 145 |
if skip and skip["if"]: |
| 146 |
return False |
| 147 |
|
| 148 |
include = get_directive_values(GraphQLIncludeDirective, node, variable_values) |
| 149 |
if include and not include["if"]: |
| 150 |
return False |
| 151 |
|
| 152 |
return True |
| 153 |
|
| 154 |
|
| 155 |
def does_fragment_condition_match( |
| 156 |
schema: GraphQLSchema, |
| 157 |
fragment: Union[FragmentDefinitionNode, InlineFragmentNode], |
| 158 |
type_: GraphQLObjectType, |
| 159 |
) -> bool: |
| 160 |
"""Determine if a fragment is applicable to the given type.""" |
| 161 |
type_condition_node = fragment.type_condition |
| 162 |
if not type_condition_node: |
| 163 |
return True |
| 164 |
conditional_type = type_from_ast(schema, type_condition_node) |
| 165 |
if conditional_type is type_: |
| 166 |
return True |
| 167 |
if is_abstract_type(conditional_type): |
| 168 |
return schema.is_sub_type(cast(GraphQLAbstractType, conditional_type), type_) |
| 169 |
return False |
| 170 |
|
| 171 |
|
| 172 |
def get_field_entry_key(node: FieldNode) -> str: |
| 173 |
"""Implements the logic to compute the key of a given field's entry""" |
| 174 |
return node.alias.value if node.alias else node.name.value |