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, name: nil, definition_name: nil, definitions: {}, children: {}, spreads: [], directives: Set.new, included: true) ⇒ Node

Returns a new instance of Node


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

def initialize(parent:, ast_node: nil, return_type: nil, name: nil, definition_name: nil, definitions: {}, children: {}, spreads: [], directives: Set.new, included: true)
  # Make sure these are kept in sync with #dup
  @ast_node = ast_node
  @return_type = return_type
  @name = name
  @definition_name = definition_name
  @parent = parent
  @definitions = definitions
  @children = children
  @spreads = spreads
  @directives = directives
  @included = included
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:


54
55
56
# File 'lib/graphql/internal_representation/node.rb', line 54

def ast_node
  @ast_node
end

#childrenArray<GraphQL::Query::Node> (readonly)

Returns:

  • (Array<GraphQL::Query::Node>)

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

def children
  @children
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)


29
30
31
# File 'lib/graphql/internal_representation/node.rb', line 29

def definition_name
  @definition_name
end

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

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

{ 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:


48
49
50
# File 'lib/graphql/internal_representation/node.rb', line 48

def definitions
  @definitions
end

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

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


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

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)


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

def included
  @included
end

#index=(value) ⇒ Object (writeonly)

Sets the attribute index

Parameters:

  • value

    the value to set the attribute index to.


95
96
97
# File 'lib/graphql/internal_representation/node.rb', line 95

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


51
52
53
# File 'lib/graphql/internal_representation/node.rb', line 51

def name
  @name
end

#parentGraphQL::InternalRepresentation::Node (readonly)

Returns The node which this node is a child of

Returns:


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

def parent
  @parent
end

#return_typeGraphQL::BaseType (readonly)

Returns:


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

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:


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

def spreads
  @spreads
end

Instance Method Details

#inspect(indent = 0) ⇒ Object


97
98
99
100
101
102
103
104
# File 'lib/graphql/internal_representation/node.rb', line 97

def inspect(indent = 0)
  own_indent = " " * indent
  self_inspect = "#{own_indent}<Node #{name} #{skipped? ? "(skipped)" : ""}(#{definition_name}: {#{definitions.keys.join("|")}} -> #{return_type})>"
  if children.any?
    self_inspect << " {\n#{children.values.map { |n| n.inspect(indent + 2)}.join("\n")}\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:


74
75
76
77
78
79
80
81
82
# File 'lib/graphql/internal_representation/node.rb', line 74

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

#pathObject


84
85
86
87
88
89
90
91
92
93
# File 'lib/graphql/internal_representation/node.rb', line 84

def path
  if parent
    path = parent.path
    path << name
    path << @index if @index
    path
  else
    []
  end
end

#skipped?Boolean

Returns:

  • (Boolean)

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

def skipped?
  !@included
end