Module: GraphQL::InternalRepresentation::Selections

Defined in:
lib/graphql/internal_representation/selections.rb

Class Method Summary collapse

Class Method Details

.build(query, nodes) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphql/internal_representation/selections.rb', line 4

def self.build(query, nodes)
  # { type => { name => nodes } }
  selections = Hash.new { |h, k| h[k] = Hash.new { |h2, k2| h2[k2] = [] } }
  object_types = Set.new

  warden = query.warden
  ctx = query.context

  nodes.each do |node|
    node.typed_children.each_key do |type_cond|
      object_types.merge(warden.possible_types(type_cond))
    end
  end

  nodes.each do |node|
    node.typed_children.each do |type_cond, children|
      object_types.each do |obj_type|
        obj_selections = selections[obj_type]
        skipped = []
        if GraphQL::Execution::Typecast.compatible?(obj_type, type_cond, ctx)
          children.each do |name, irep_node|
            if irep_node.skipped?
              skipped << name
            else
              obj_selections[name] << irep_node
            end
          end
        end
        skipped.each { |k| obj_selections.delete(k) }
      end
    end
  end

  selections
end