Class: GraphQL::Language::SelectionSet

Inherits:
Struct
  • Object
show all
Defined in:
lib/graphql/language/selection_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#selectionsObject

Returns the value of attribute selections

Returns:

  • (Object)

    the current value of 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

Returns:

  • (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.meta_field?(field.name)
      GraphQL::Introspection.meta_field(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