Module: GraphQL::Language::Nodes::Selections

Included in:
Field, FragmentDefinition, InlineFragment, OperationDefinition
Defined in:
lib/graphql/language/nodes/selection_ext.rb,
lib/graphql/language/nodes/inject_selection_ext.rb,
lib/graphql/language/nodes/query_result_class_ext.rb,
lib/graphql/language/nodes/replace_fragment_spread_ext.rb

Overview

Public: Define shared trait for Nodes that have a “selections” collection.

Instance Method Summary collapse

Instance Method Details

#inject_selection(*selections) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/graphql/language/nodes/inject_selection_ext.rb', line 8

def inject_selection(*selections)
  other = self.dup
  other.selections = selections + self.selections.map do |selection|
    case selection
    when Selections
      selection.inject_selection(*selections)
    else
      selection
    end
  end
  other
end

#query_result_class(**kargs) ⇒ Object

Public: Get GraphQL::QueryResult class for result of query.

Returns subclass of QueryResult or nil.



13
14
15
# File 'lib/graphql/language/nodes/query_result_class_ext.rb', line 13

def query_result_class(**kargs)
  GraphQL::Client::QueryResult.define(fields: selections_query_result_classes(**kargs))
end

#replace_fragment_spread(fragments) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/graphql/language/nodes/replace_fragment_spread_ext.rb', line 8

def replace_fragment_spread(fragments)
  other = self.dup
  other.selections = self.selections.map do |selection|
    case selection
    when FragmentSpread
      if fragment = fragments[selection.name.to_sym]
        InlineFragment.new(type: fragment.type, directives: fragment.directives, selections: fragment.selections)
      else
        selection
      end
    when Selections
      selection.replace_fragment_spread(fragments)
    else
      selection
    end
  end
  other
end

#selection_query_result_classes(**kargs) ⇒ Object



17
18
19
# File 'lib/graphql/language/nodes/query_result_class_ext.rb', line 17

def selection_query_result_classes(**kargs)
  selections_query_result_classes(**kargs)
end

#selections_query_result_classes(shadow: Set.new, **kargs) ⇒ Object

Internal: Gather QueryResult classes for each selection.

Returns a Hash[String => (QueryResult|nil)].



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/language/nodes/query_result_class_ext.rb', line 24

def selections_query_result_classes(shadow: Set.new, **kargs)
  self.selections.inject({}) do |h, selection|
    case selection
    when Selection
      if shadow.include?(selection)
        {}
      else
        h.merge!(selection.selection_query_result_classes(shadow: shadow, **kargs))
      end
    else
      raise TypeError, "expected selection to be of type Selection, but was #{selection.class}"
    end
  end
end