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].
- #empty? ⇒ Boolean
-
#evaluate(context, object_type, object) ⇒ Object
GraphQL Specification 6.4.1 Field entries GetFieldEntry implementation objectType, object, - fields + context[schema, document].
-
#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]
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 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/graphql/language/selection_set.rb', line 51 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 |
#empty? ⇒ Boolean
5 6 7 |
# File 'lib/graphql/language/selection_set.rb', line 5 def empty? selections.empty? end |
#evaluate(context, object_type, object) ⇒ Object
GraphQL Specification
6.4.1 Field entries
GetFieldEntry implementation
objectType, object, - fields
+ context[schema, document]
TODO: think of way to have error accessor at this point. Executor?
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/graphql/language/selection_set.rb', line 17 def evaluate(context, object_type, object) grouped_fields = collect_fields(context, object_type) # # TODO: Merge equal fields # 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? resolve_context = context.merge({ parent_type: object_type }) resolved_object = field.resolve(context, object_type, object) selection_set = merge_selection_sets(fields) memo[key.to_sym] = Executor::FutureCompleter.complete_value(context, field_definition.type, resolved_object, selection_set) end memo end end |
#merge_selection_sets(fields) ⇒ Object
GraphQL Specification
6.4.1 Field entries
MergeSelectionSets implementations
fields
96 97 98 99 100 101 102 103 |
# File 'lib/graphql/language/selection_set.rb', line 96 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 |