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]


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
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/graphql/language/selection_set.rb', line 50

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/graphql/language/selection_set.rb', line 105

def complete_value(context, field_type, resolved_object, selection_set)
  return nil if resolved_object.nil?

  resolved_object = resolved_object.value if resolved_object.is_a?(Celluloid::Future)

  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

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]


16
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
# File 'lib/graphql/language/selection_set.rb', line 16

def evaluate(context, object_type, object)
  Execution::Pool.future do

    collect_fields(context, object_type).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


      memo[key.to_sym]      = begin
        resolution_context  = context.merge({ parent_type: object_type })
        resolved_object     = field.resolve(resolution_context, object_type, object)
        complete_value(context, field_definition.type, resolved_object, merge_selection_sets(fields))
      rescue Celluloid::TaskTerminated => e
        context[:errors] << "Field '#{field.name}' of '#{object_type}' error: #{e}"
        nil
      end unless field_definition.nil?

      memo
    end
  end
end

#merge_selection_sets(fields) ⇒ Object

GraphQL Specification

6.4.1 Field entries
  MergeSelectionSets implementations
    fields


95
96
97
98
99
100
101
102
# File 'lib/graphql/language/selection_set.rb', line 95

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