Class: GraphQL::Language::Visitor
- Inherits:
-
Object
- Object
- GraphQL::Language::Visitor
- Defined in:
- lib/graphql/language/visitor.rb
Overview
Depth-first traversal through the tree, calling hooks at each stop.
Direct Known Subclasses
Analysis::AST::Visitor, DefinitionSlice::DependencyVisitor, StaticValidation::BaseVisitor
Defined Under Namespace
Classes: DeleteNode, NodeVisitor
Constant Summary collapse
- SKIP =
Deprecated.
Use
super
to continue the visit; or don't call it to halt.If any hook returns this value, the GraphQL::Language::Visitor stops visiting this node right away
:_skip
- DELETE_NODE =
When this is returned from a visitor method, Then the
node
passed into the method is removed fromparent
's children. DeleteNode.new
Instance Attribute Summary collapse
-
#result ⇒ GraphQL::Language::Nodes::Document
readonly
The document with any modifications applied.
Class Method Summary collapse
-
.make_visit_method(node_method) ⇒ Object
We don't use
alias
here because it breakssuper
.
Instance Method Summary collapse
-
#[](node_class) ⇒ NodeVisitor
deprecated
Deprecated.
see
on_
methods, like #on_field -
#initialize(document) ⇒ Visitor
constructor
A new instance of Visitor.
-
#on_abstract_node(node, parent) ⇒ Array?
The default implementation for visiting an AST node.
-
#visit ⇒ void
Visit
document
and all children, applying hooks as you go. -
#visit_node(node, parent) ⇒ Object
Call the user-defined handler for
node
.
Constructor Details
#initialize(document) ⇒ Visitor
Returns a new instance of Visitor.
45 46 47 48 49 |
# File 'lib/graphql/language/visitor.rb', line 45 def initialize(document) @document = document @visitors = {} @result = nil end |
Instance Attribute Details
#result ⇒ GraphQL::Language::Nodes::Document (readonly)
Returns The document with any modifications applied.
52 53 54 |
# File 'lib/graphql/language/visitor.rb', line 52 def result @result end |
Class Method Details
.make_visit_method(node_method) ⇒ Object
We don't use alias
here because it breaks super
120 121 122 123 124 125 126 127 128 |
# File 'lib/graphql/language/visitor.rb', line 120 def self.make_visit_method(node_method) class_eval(<<-RUBY, __FILE__, __LINE__ + 1) def #{node_method}(node, parent) child_mod = on_abstract_node(node, parent) # If visiting the children returned changes, continue passing those. child_mod || [node, parent] end RUBY end |
Instance Method Details
#[](node_class) ⇒ NodeVisitor
see on_
methods, like #on_field
Get a NodeVisitor for node_class
61 62 63 |
# File 'lib/graphql/language/visitor.rb', line 61 def [](node_class) @visitors[node_class] ||= NodeVisitor.new end |
#on_abstract_node(node, parent) ⇒ Array?
The default implementation for visiting an AST node. It doesn't do anything, but it continues to visiting the node's children. To customize this hook, override one of its make_visit_methodes (or the base method?) in your subclasses.
For compatibility, it calls hook procs, too.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/graphql/language/visitor.rb', line 91 def on_abstract_node(node, parent) if node.equal?(DELETE_NODE) # This might be passed to `super(DELETE_NODE, ...)` # by a user hook, don't want to keep visiting in that case. nil else # Run hooks if there are any new_node = node no_hooks = !@visitors.key?(node.class) if no_hooks || begin_visit(new_node, parent) node.children.each do |child_node| new_child_and_node = on_node_with_modifications(child_node, new_node) # Reassign `node` in case the child hook makes a modification if new_child_and_node.is_a?(Array) new_node = new_child_and_node[1] end end end end_visit(new_node, parent) unless no_hooks if new_node.equal?(node) nil else [new_node, parent] end end end |
#visit ⇒ void
This method returns an undefined value.
Visit document
and all children, applying hooks as you go
67 68 69 70 71 72 73 74 75 |
# File 'lib/graphql/language/visitor.rb', line 67 def visit result = on_node_with_modifications(@document, nil) @result = if result.is_a?(Array) result.first else # The node wasn't modified @document end end |
#visit_node(node, parent) ⇒ Object
Call the user-defined handler for node
.
78 79 80 |
# File 'lib/graphql/language/visitor.rb', line 78 def visit_node(node, parent) public_send(node.visit_method, node, parent) end |