Class: GraphQL::Language::SelectionSet
- Inherits:
-
Struct
- Object
- Struct
- GraphQL::Language::SelectionSet
- Defined in:
- lib/graphql/language/selection_set.rb
Instance Attribute Summary collapse
-
#selections ⇒ Object
Returns the value of attribute selections.
Instance Method Summary collapse
-
#collect_fields(context, object_type, visited_fragments = []) ⇒ Object
GraphQL Specification 6.3 Evaluate selection sets CollectFields implementation objectType, selectionSet = self, visitedFragments = [] + context[schema, document].
- #complete_value(context, field_type, resolved_object, selection_set) ⇒ Object
- #empty? ⇒ Boolean
- #evaluate(context, object_type, object, serially: false) ⇒ Object
-
#merge_selection_sets(fields) ⇒ Object
GraphQL Specification 6.4.1 Field entries MergeSelectionSets implementations fields.
Instance Attribute Details
#selections ⇒ Object
Returns the value of attribute selections
3 4 5 |
# File 'lib/graphql/language/selection_set.rb', line 3 def selections @selections end |
Instance Method Details
#collect_fields(context, object_type, visited_fragments = []) ⇒ Object
GraphQL Specification
6.3 Evaluate selection sets
CollectFields implementation
objectType, selectionSet = self, visitedFragments = []
+ context[schema, document]
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/graphql/language/selection_set.rb', line 84 def collect_fields(context, object_type, visited_fragments = []) memo = {} selections.each do |selection| case selection when Field # TODO: Directives (memo[selection.key] ||= []) << selection when FragmentSpread next if visited_fragments.include?(selection.name) visited_fragments << selection.name fragment = context[:document].fragment(selection.name) next if fragment.nil? next unless fragment.apply?(context, object_type) fragment.selection_set.collect_fields(context, object_type).each do |key, fields| memo[key] = (memo[key] ||= []).concat(fields) end when InlineFragment next unless selection.apply?(context, object_type) selection.selection_set.collect_fields(context, object_type).each do |key, fields| memo[key] = (memo[key] ||= []).concat(fields) end end end memo end |
#complete_value(context, field_type, resolved_object, selection_set) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/graphql/language/selection_set.rb', line 139 def complete_value(context, field_type, resolved_object, selection_set) return nil if resolved_object.nil? if resolved_object.is_a?(Exception) context[:errors] << "Field '#{field_type}' of '#{context[:field_name]}' error: '#{resolved_object}'." return nil end case field_type when GraphQLNonNull completed_object = complete_value(context, field_type.of_type, resolved_object, selection_set) raise "Field error: expecting non null value" if completed_object.nil? completed_object when GraphQLList resolved_object.map do |item| complete_value(context, field_type.of_type, item, selection_set) end when GraphQLScalarType, GraphQLEnumType field_type.serialize(resolved_object) when GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType field_type = field_type.resolve_type(resolved_object) if field_type.is_a?(GraphQLAbstractType) selection_set.evaluate(context, field_type, resolved_object) end end |
#empty? ⇒ Boolean
5 6 7 |
# File 'lib/graphql/language/selection_set.rb', line 5 def empty? selections.empty? end |
#evaluate(context, object_type, object, serially: false) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/graphql/language/selection_set.rb', line 49 def evaluate(context, object_type, object, serially: false) grouped_fields = collect_fields(context, object_type) grouped_fields.reduce({}) do |memo, (key, fields)| field = fields.first field_definition = case when GraphQL::Introspection.(field.name) GraphQL::Introspection.(field.name) else object_type.field(field.name) end unless field_definition.nil? context[:field_name] = field.name value = Execution::Pool.future do resolved_value = field.resolve(context, object_type, object) resolved_value = resolved_value.value if resolved_value.is_a?(Celluloid::Future) complete_value(context, field_definition.type, resolved_value, merge_selection_sets(fields)) end memo[key.to_sym] = serially ? value.value : value end memo end end |
#merge_selection_sets(fields) ⇒ Object
GraphQL Specification
6.4.1 Field entries
MergeSelectionSets implementations
fields
129 130 131 132 133 134 135 136 |
# File 'lib/graphql/language/selection_set.rb', line 129 def merge_selection_sets(fields) selections = fields.reduce([]) do |memo, field| memo.concat field.selection_set.selections unless field.selection_set.nil? || field.selection_set.empty? memo end SelectionSet.new(selections) end |