Class: GraphQL::InternalRepresentation::Selection

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/internal_representation/selection.rb

Overview

A selection is a single field on an object. It's "backed" by one or more Nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query:, nodes:) ⇒ Selection

Returns a new instance of Selection.



7
8
9
10
11
12
13
14
15
16
# File 'lib/graphql/internal_representation/selection.rb', line 7

def initialize(query:, nodes:)
  @irep_node = nodes.first
  @definition_name = @irep_node.definition_name
  @name = @irep_node.name

  @skipped = nodes.any?(&:skipped?)
  @query = query
  @typed_child_nodes = build_typed_selections(query, nodes)
  @typed_subselections = Hash.new { |h, k| h[k] = {} }
end

Instance Attribute Details

#definition_nameString (readonly)

Returns name of the field in this selection.

Returns:

  • (String)

    name of the field in this selection



19
20
21
# File 'lib/graphql/internal_representation/selection.rb', line 19

def definition_name
  @definition_name
end

#irep_nodeNode (readonly)

Returns A "representative" node for this selection. Legacy. In the case that a selection appears in multiple nodes, is this valuable?.

Returns:

  • (Node)

    A "representative" node for this selection. Legacy. In the case that a selection appears in multiple nodes, is this valuable?



25
26
27
# File 'lib/graphql/internal_representation/selection.rb', line 25

def irep_node
  @irep_node
end

#nameString (readonly)

Returns the key of this selection in the result (may be the field's alias).

Returns:

  • (String)

    the key of this selection in the result (may be the field's alias)



22
23
24
# File 'lib/graphql/internal_representation/selection.rb', line 22

def name
  @name
end

Instance Method Details

#each_selection(type:) {|name, subselection| ... } ⇒ Object

Call the block with each name & subselection whose conditions match type

Parameters:

Yield Parameters:

  • name (String)

    the key in the child selection

  • subselection (Selection)

    the selection for that key



36
37
38
39
40
41
42
43
44
# File 'lib/graphql/internal_representation/selection.rb', line 36

def each_selection(type:)
  subselections = @typed_subselections[type]
  @typed_child_nodes[type].each do |name, child_nodes|
    subselection = subselections[name] ||= self.class.new(query: @query, nodes: child_nodes)
    if !subselection.skipped?
      yield(name, subselection)
    end
  end
end

#skipped?Boolean

Returns true if any of the backing nodes are skipped.

Returns:

  • (Boolean)

    true if any of the backing nodes are skipped



28
29
30
# File 'lib/graphql/internal_representation/selection.rb', line 28

def skipped?
  @skipped
end