Class: GraphQL::Language::Nodes::AbstractNode
- Inherits:
-
Object
- Object
- GraphQL::Language::Nodes::AbstractNode
- Defined in:
- lib/graphql/language/nodes.rb
Overview
AbstractNode is the base class for all nodes in a GraphQL AST.
It provides some APIs for working with ASTs:
childrenreturns all AST nodes attached to this one. Used for tree traversal.scalarsreturns all scalar (Ruby) values attached to this one. Used for comparing nodes.to_query_stringturns an AST node into a GraphQL string
Direct Known Subclasses
Argument, Directive, DirectiveDefinition, Document, EnumTypeDefinition, EnumTypeExtension, EnumValueDefinition, Field, FieldDefinition, FragmentDefinition, FragmentSpread, InlineFragment, InputObject, InputObjectTypeDefinition, InputObjectTypeExtension, InputValueDefinition, InterfaceTypeDefinition, InterfaceTypeExtension, NameOnlyNode, ObjectTypeDefinition, ObjectTypeExtension, OperationDefinition, ScalarTypeDefinition, ScalarTypeExtension, SchemaDefinition, SchemaExtension, UnionTypeDefinition, UnionTypeExtension, VariableDefinition, WrapperType
Defined Under Namespace
Modules: DefinitionNode
Constant Summary collapse
- NO_CHILDREN =
GraphQL::EmptyObjects::EMPTY_ARRAY
Class Attribute Summary collapse
-
.children_method_name ⇒ Object
Returns the value of attribute children_method_name.
-
.visit_method ⇒ Object
readonly
Returns.
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
: String?.
Class Method Summary collapse
-
.children_of_type ⇒ Object
Returns.
-
.inherited(child_class) ⇒ Object
Add a default
.visit_methodand.children_method_nameusing the class name.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Value equality.
-
#children ⇒ Object
Returns.
- #children_method_name ⇒ Object
-
#col ⇒ Object
: -> Integer?.
-
#definition_line ⇒ Object
: -> Integer?.
-
#delete_child(previous_child) ⇒ Object
TODO DRY with
replace_child. -
#initialize_copy(other) ⇒ Object
This might be unnecessary, but its easiest to add it here.
-
#line ⇒ Object
: -> Integer?.
-
#merge(new_options) ⇒ Object
This creates a copy of
self, withnew_optionsapplied. -
#position ⇒ Object
: -> [Integer?, Integer?].
-
#replace_child(previous_child, new_child) ⇒ Object
Copy
self, but modify the copy so thatprevious_childis replaced bynew_child. -
#scalars ⇒ Object
Returns.
-
#to_query_string(printer: GraphQL::Language::Printer.new) ⇒ Object
: (?printer: untyped) -> String.
-
#visit_method ⇒ Object
Built-in node classes override this with a literal Symbol.
Class Attribute Details
.children_method_name ⇒ Object
Returns the value of attribute children_method_name.
205 206 207 |
# File 'lib/graphql/language/nodes.rb', line 205 def children_method_name @children_method_name end |
.visit_method ⇒ Object (readonly)
Returns
Symbol— the method that GraphQL::Language::Visitor calls for this kind of node : Symbol?
203 204 205 |
# File 'lib/graphql/language/nodes.rb', line 203 def visit_method @visit_method end |
Instance Attribute Details
#filename ⇒ Object (readonly)
: String?
41 42 43 |
# File 'lib/graphql/language/nodes.rb', line 41 def filename @filename end |
Class Method Details
.children_of_type ⇒ Object
Returns
Hash{Symbol => Class, nil}, nil— child reader methods and the kind of node each one returns, if known
:call-seq:
children_of_type() -> Hash[Symbol, Class | nil] | nil
225 226 227 |
# File 'lib/graphql/language/nodes.rb', line 225 def children_of_type @children_methods end |
.inherited(child_class) ⇒ Object
Add a default .visit_method and .children_method_name using the class name
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/graphql/language/nodes.rb', line 208 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 |
Instance Method Details
#==(other) ⇒ Object
Value equality
Returns
Boolean— True ifselfis equivalent toother
:call-seq:
==(other) -> bool
66 67 68 69 70 71 |
# File 'lib/graphql/language/nodes.rb', line 66 def ==(other) return true if equal?(other) other.kind_of?(self.class) && other.scalars == self.scalars && other.children == self.children end |
#children ⇒ Object
Returns
Array<GraphQL::Language::Nodes::AbstractNode>— all nodes in the tree below this one
:call-seq:
children() -> Array[GraphQL::Language::Nodes::AbstractNode]
: -> Array
82 83 84 |
# File 'lib/graphql/language/nodes.rb', line 82 def children NO_CHILDREN end |
#children_method_name ⇒ Object
104 105 106 |
# File 'lib/graphql/language/nodes.rb', line 104 def children_method_name self.class.children_method_name end |
#col ⇒ Object
: -> Integer?
49 50 51 |
# File 'lib/graphql/language/nodes.rb', line 49 def col @col ||= @source&.column_at(@pos) end |
#definition_line ⇒ Object
: -> Integer?
54 55 56 |
# File 'lib/graphql/language/nodes.rb', line 54 def definition_line @definition_line ||= (@source && @definition_pos) ? @source.line_at(@definition_pos) : nil end |
#delete_child(previous_child) ⇒ Object
TODO DRY with replace_child
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/graphql/language/nodes.rb', line 177 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 |
#initialize_copy(other) ⇒ Object
This might be unnecessary, but its easiest to add it here.
98 99 100 101 102 |
# File 'lib/graphql/language/nodes.rb', line 98 def initialize_copy(other) @children = nil @scalars = nil @query_string = nil end |
#line ⇒ Object
: -> Integer?
44 45 46 |
# File 'lib/graphql/language/nodes.rb', line 44 def line @line ||= @source&.line_at(@pos) end |
#merge(new_options) ⇒ Object
This creates a copy of self, with new_options applied.
Parameters
new_options(Hash)
Returns
AbstractNode— a shallow copy ofself
:call-seq:
merge(Hash new_options) -> AbstractNode
150 151 152 |
# File 'lib/graphql/language/nodes.rb', line 150 def merge() dup.merge!() end |
#position ⇒ Object
: -> [Integer?, Integer?]
121 122 123 |
# File 'lib/graphql/language/nodes.rb', line 121 def position [line, col] end |
#replace_child(previous_child, new_child) ⇒ Object
Copy self, but modify the copy so that previous_child is replaced by new_child
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/graphql/language/nodes.rb', line 155 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 |
#scalars ⇒ Object
Returns
Array<Integer, Float, String, Boolean, Array>— Scalar values attached to this node
:call-seq:
scalars() -> Array[Integer | Float | String | bool | Array]
: -> Array
93 94 95 |
# File 'lib/graphql/language/nodes.rb', line 93 def scalars NO_CHILDREN end |
#to_query_string(printer: GraphQL::Language::Printer.new) ⇒ Object
: (?printer: untyped) -> String
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/graphql/language/nodes.rb', line 126 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 |
#visit_method ⇒ Object
Built-in node classes override this with a literal Symbol.
Returns
Symbol— the method that GraphQL::Language::Visitor calls for this node
:call-seq:
visit_method() -> Symbol
116 117 118 |
# File 'lib/graphql/language/nodes.rb', line 116 def visit_method self.class.visit_method end |