class GraphQL::Language::Nodes::AbstractNode
AbstractNode is the base class for all nodes in a GraphQL AST.
It provides some APIs for working with ASTs: - children returns all AST nodes attached to this one. Used for tree traversal. - scalars returns all scalar (Ruby) values attached to this one. Used for comparing nodes. - to_query_string turns an AST node into a GraphQL string
Constants
- NO_CHILDREN
Attributes
Symbol?
Returns
-
Symbol— the method thatGraphQL::Language::Visitorcalls for this kind of node
Public Class Methods
# File lib/graphql/language/nodes.rb, line 224 def children_of_type @children_methods end
Returns
-
Hash{Symbol => Class, nil}, nil— child reader methods and the kind of node each one returns, if known
Source
# File lib/graphql/language/nodes.rb, line 207 def inherited(child_class) super name_underscored = child_class.name .split("::").last .gsub(/([a-z])([A-Z])/,'\1_\2') # insert underscores .downcase # remove caps child_class.instance_variable_set(:@visit_method, :"on_#{name_underscored}") child_class.children_method_name = :"#{name_underscored}s" end
Add a default .visit_method and .children_method_name using the class name
Public Instance Methods
Source
# File lib/graphql/language/nodes.rb, line 65 def ==(other) return true if equal?(other) other.kind_of?(self.class) && other.scalars == self.scalars && other.children == self.children end
Value equality
Returns
-
Boolean— True ifselfis equivalent toother
# File lib/graphql/language/nodes.rb, line 81 def children NO_CHILDREN end
Returns
-
Array<GraphQL::Language::Nodes::AbstractNode>— all nodes in the tree below this one
Source
# File lib/graphql/language/nodes.rb, line 103 def children_method_name self.class.children_method_name end
→ Integer?
Source
# File lib/graphql/language/nodes.rb, line 48 def col @col ||= @source&.column_at(@pos) end
→ Integer?
Source
# File lib/graphql/language/nodes.rb, line 53 def definition_line @definition_line ||= (@source && @definition_pos) ? @source.line_at(@definition_pos) : nil end
# File lib/graphql/language/nodes.rb, line 176 def delete_child(previous_child) # Figure out which list `previous_child` may be found in method_name = previous_child.children_method_name # Copy that list, and delete previous_child new_children = public_send(method_name).dup new_children.delete(previous_child) # Copy this node, but with the new list of children: copy_of_self = merge(method_name => new_children) # Return the copy: copy_of_self end
TODO DRY with replace_child
Source
# File lib/graphql/language/nodes.rb, line 97 def initialize_copy(other) @children = nil @scalars = nil @query_string = nil end
This might be unnecessary, but its easiest to add it here.
→ Integer?
Source
# File lib/graphql/language/nodes.rb, line 43 def line @line ||= @source&.line_at(@pos) end
# File lib/graphql/language/nodes.rb, line 149 def merge(new_options) dup.merge!(new_options) end
This creates a copy of self, with new_options applied.
Parameters
-
new_options(Hash)
Returns
-
AbstractNode— a shallow copy ofself
→ [Integer?, Integer?]
Source
# File lib/graphql/language/nodes.rb, line 120 def position [line, col] end
# File lib/graphql/language/nodes.rb, line 154 def replace_child(previous_child, new_child) # Figure out which list `previous_child` may be found in method_name = previous_child.children_method_name # Get the value from this (original) node prev_children = public_send(method_name) if prev_children.is_a?(Array) # Copy that list, and replace `previous_child` with `new_child` # in the list. new_children = prev_children.dup prev_idx = new_children.index(previous_child) #: as Integer new_children[prev_idx] = new_child else # Use the new value for the given attribute new_children = new_child end # Copy this node, but with the new child value copy_of_self = merge(method_name => new_children) # Return the copy: copy_of_self end
Copy self, but modify the copy so that previous_child is replaced by new_child
→ Array[untyped]
Source
# File lib/graphql/language/nodes.rb, line 92 def scalars NO_CHILDREN end
Returns
-
Array<Integer, Float, String, Boolean, Array>— Scalar values attached to this node
(?printer: untyped) → String
Source
# File lib/graphql/language/nodes.rb, line 125 def to_query_string(printer: GraphQL::Language::Printer.new) if printer.is_a?(GraphQL::Language::Printer) if frozen? @query_string || printer.print(self) else @query_string ||= printer.print(self) end else printer.print(self) end end
Source
# File lib/graphql/language/nodes.rb, line 115 def visit_method self.class.visit_method end
Built-in node classes override this with a literal Symbol.
Returns
-
Symbol— the method thatGraphQL::Language::Visitorcalls for this node