Class: GraphQL::InternalRepresentation::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent:, ast_node: nil, return_type: nil, owner_type: nil, name: nil, definition_name: nil, definition: nil, spreads: [], directives: Set.new, included: true, typed_children: Hash.new {|h, k| h[k] = {} }, definitions: {}, children: {}) ⇒ Node

Returns a new instance of Node.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/graphql/internal_representation/node.rb', line 6

def initialize(parent:, ast_node: nil, return_type: nil, owner_type: nil, name: nil, definition_name: nil, definition: nil, spreads: [], directives: Set.new, included: true, typed_children: Hash.new {|h, k| h[k] = {} }, definitions: {}, children: {})
  @ast_node = ast_node
  @return_type = return_type
  @owner_type = owner_type
  @name = name
  @definition_name = definition_name
  @definition = definition
  @parent = parent
  @spreads = spreads
  @directives = directives
  @included = included
  @typed_children = typed_children
  @children = children
  @definitions = definitions
end

Instance Attribute Details

#ast_nodeGraphQL::Language::Nodes::AbstractNode (readonly)

Returns The AST node (or one of the nodes) where this was derived from.

Returns:



65
66
67
# File 'lib/graphql/internal_representation/node.rb', line 65

def ast_node
  @ast_node
end

#childrenArray<Node> (readonly)

Deprecated.

use #typed_children instead

Returns leaf selections on this node. Known to be buggy: deeply nested selections are not handled properly

Returns:



77
78
79
# File 'lib/graphql/internal_representation/node.rb', line 77

def children
  @children
end

#definitionGraphQL::Field, GraphQL::Directive (readonly)

Returns the static definition for this field (it might be an interface field definition even though an object field definition will be used at runtime).

Returns:

  • (GraphQL::Field, GraphQL::Directive)

    the static definition for this field (it might be an interface field definition even though an object field definition will be used at runtime)



59
60
61
# File 'lib/graphql/internal_representation/node.rb', line 59

def definition
  @definition
end

#definition_nameString (readonly)

Returns the name for this node's definition (#name may be a field's alias, this is always the name).

Returns:

  • (String)

    the name for this node's definition (#name may be a field's alias, this is always the name)



34
35
36
# File 'lib/graphql/internal_representation/node.rb', line 34

def definition_name
  @definition_name
end

#definitionsHash<GraphQL::BaseType => GraphQL::Field> (readonly)

Deprecated.

use #typed_children to find matching children, the use the node's #definition

A shallow cache of type-field pairs for executing & analyzing this node.

Known to be buggy: some fields are deeply merged when they shouldn't be.

{ person(id: 1) { firstName # => defined type is person } } { node(id: $nodeId) { ... on Nameable { firstName # => defined type is Nameable } } }

Examples:

On-type from previous return value

On-type from explicit type condition

Returns:



56
57
58
# File 'lib/graphql/internal_representation/node.rb', line 56

def definitions
  @definitions
end

#directivesSet<GraphQL::Language::Nodes::Directive> (readonly)

These are the compiled directives from fragment spreads, inline fragments, and the field itself



31
32
33
# File 'lib/graphql/internal_representation/node.rb', line 31

def directives
  @directives
end

#includedBoolean Also known as: included?

Returns false if every field for this node included @skip(if: true).

Returns:

  • (Boolean)

    false if every field for this node included @skip(if: true)



80
81
82
# File 'lib/graphql/internal_representation/node.rb', line 80

def included
  @included
end

#index=(value) ⇒ Object (writeonly)

Sets the attribute index

Parameters:

  • value

    the value to set the attribute index to.



113
114
115
# File 'lib/graphql/internal_representation/node.rb', line 113

def index=(value)
  @index = value
end

#nameString (readonly)

Returns the name to use for the result in the response hash.

Returns:

  • (String)

    the name to use for the result in the response hash



62
63
64
# File 'lib/graphql/internal_representation/node.rb', line 62

def name
  @name
end

#owner_typeGraphQL::BaseType (readonly)

Returns:



71
72
73
# File 'lib/graphql/internal_representation/node.rb', line 71

def owner_type
  @owner_type
end

#parentGraphQL::InternalRepresentation::Node (readonly)

Returns The node which this node is a child of.

Returns:



88
89
90
# File 'lib/graphql/internal_representation/node.rb', line 88

def parent
  @parent
end

#return_typeGraphQL::BaseType (readonly)

Returns:



68
69
70
# File 'lib/graphql/internal_representation/node.rb', line 68

def return_type
  @return_type
end

#spreadsArray<GraphQL::InternalRepresentation::Node> (readonly)

Note: by the time this gets out of the Rewrite phase, this will be empty -- it's emptied out when fragments are merged back in

Returns:



27
28
29
# File 'lib/graphql/internal_representation/node.rb', line 27

def spreads
  @spreads
end

#typed_childrenHash{GraphQL::BaseType => Hash{String => Node}] Children for each type condition (readonly)

Returns Hash=> Hash{String => Node] Children for each type condition.

Returns:



23
24
25
# File 'lib/graphql/internal_representation/node.rb', line 23

def typed_children
  @typed_children
end

Instance Method Details

#inspect(indent = 0) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/graphql/internal_representation/node.rb', line 115

def inspect(indent = 0)
  own_indent = " " * indent
  self_inspect = "#{own_indent}<Node #{name} #{skipped? ? "(skipped)" : ""}(#{definition_name} -> #{return_type})>"
  if typed_children.any?
    self_inspect << " {"
    typed_children.each do |type_defn, children|
      self_inspect << "\n#{own_indent}  #{type_defn} => (#{children.keys.join(",")})"
    end
    self_inspect << "\n#{own_indent}}"
  end
  self_inspect
end

#ownerGraphQL::InternalRepresentation::Node

Returns The root node which this node is a (perhaps-distant) child of, or self if this is a root node.

Returns:



91
92
93
94
95
96
97
98
99
# File 'lib/graphql/internal_representation/node.rb', line 91

def owner
  @owner ||= begin
    if parent.nil?
      self
    else
      parent.owner
    end
  end
end

#pathObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/graphql/internal_representation/node.rb', line 101

def path
  warn("InternalRepresentation::Node#path is deprecated, use Query::Context#path instead")
  if parent
    path = parent.path
    path << name
    path << @index if @index
    path
  else
    []
  end
end

#skipped?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/graphql/internal_representation/node.rb', line 83

def skipped?
  !@included
end