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:
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
Direct Known Subclasses
Argument, Directive, DirectiveDefinition, Document, EnumTypeDefinition, EnumValueDefinition, Field, FieldDefinition, FragmentDefinition, FragmentSpread, InlineFragment, InputObject, InputObjectTypeDefinition, InputValueDefinition, InterfaceTypeDefinition, NameOnlyNode, ObjectTypeDefinition, OperationDefinition, ScalarTypeDefinition, SchemaDefinition, UnionTypeDefinition, VariableDefinition, WrapperType
Instance Attribute Summary collapse
-
#col ⇒ Object
Returns the value of attribute col.
-
#line ⇒ Object
Returns the value of attribute line.
Class Method Summary collapse
-
.child_attributes(*attr_names) ⇒ Object
define
attr_names
as places where child nodes may be attached to this node. -
.inherited(subclass) ⇒ Object
A node subclass inherits
scalar_attributes
andchild_attributes
from its parent. -
.scalar_attributes(*attr_names) ⇒ Object
define
attr_names
as places where scalars may be attached to this node.
Instance Method Summary collapse
-
#children ⇒ Array<GraphQL::Language::Nodes::AbstractNode>
All nodes in the tree below this one.
-
#eql?(other) ⇒ Boolean
Value equality.
-
#initialize(options = {}) ⇒ AbstractNode
constructor
Initialize a node by extracting its position, then calling the class's
initialize_node
method. -
#initialize_node(options = {}) ⇒ Object
This is called with node-specific options.
- #position ⇒ Object
-
#scalars ⇒ Array<Integer, Float, String, Boolean, Array>
Scalar values attached to this node.
- #to_query_string ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ AbstractNode
Initialize a node by extracting its position,
then calling the class's initialize_node
method.
17 18 19 20 21 22 23 24 |
# File 'lib/graphql/language/nodes.rb', line 17 def initialize(={}) if .key?(:position_source) position_source = .delete(:position_source) @line, @col = position_source.line_and_column end initialize_node() end |
Instance Attribute Details
#col ⇒ Object
Returns the value of attribute col.
12 13 14 |
# File 'lib/graphql/language/nodes.rb', line 12 def col @col end |
#line ⇒ Object
Returns the value of attribute line.
12 13 14 |
# File 'lib/graphql/language/nodes.rb', line 12 def line @line end |
Class Method Details
.child_attributes(*attr_names) ⇒ Object
define attr_names
as places where child nodes may be attached to this node
68 69 70 71 |
# File 'lib/graphql/language/nodes.rb', line 68 def child_attributes(*attr_names) @child_attributes ||= [] @child_attributes += attr_names end |
.inherited(subclass) ⇒ Object
A node subclass inherits scalar_attributes
and child_attributes
from its parent
56 57 58 59 |
# File 'lib/graphql/language/nodes.rb', line 56 def inherited(subclass) subclass.scalar_attributes(*@scalar_attributes) subclass.child_attributes(*@child_attributes) end |
.scalar_attributes(*attr_names) ⇒ Object
define attr_names
as places where scalars may be attached to this node
62 63 64 65 |
# File 'lib/graphql/language/nodes.rb', line 62 def scalar_attributes(*attr_names) @scalar_attributes ||= [] @scalar_attributes += attr_names end |
Instance Method Details
#children ⇒ Array<GraphQL::Language::Nodes::AbstractNode>
Returns all nodes in the tree below this one.
41 42 43 44 45 |
# File 'lib/graphql/language/nodes.rb', line 41 def children self.class.child_attributes .map { |attr_name| public_send(attr_name) } .flatten end |
#eql?(other) ⇒ Boolean
Value equality
33 34 35 36 37 38 |
# File 'lib/graphql/language/nodes.rb', line 33 def eql?(other) return true if equal?(other) other.is_a?(self.class) && other.scalars.eql?(self.scalars) && other.children.eql?(self.children) end |
#initialize_node(options = {}) ⇒ Object
This is called with node-specific options
27 28 29 |
# File 'lib/graphql/language/nodes.rb', line 27 def initialize_node(={}) raise NotImplementedError end |
#position ⇒ Object
74 75 76 |
# File 'lib/graphql/language/nodes.rb', line 74 def position [line, col] end |
#scalars ⇒ Array<Integer, Float, String, Boolean, Array>
Returns Scalar values attached to this node.
48 49 50 51 |
# File 'lib/graphql/language/nodes.rb', line 48 def scalars self.class.scalar_attributes .map { |attr_name| public_send(attr_name) } end |
#to_query_string ⇒ Object
78 79 80 |
# File 'lib/graphql/language/nodes.rb', line 78 def to_query_string Generation.generate(self) end |