Class: GraphQL::Visitor
- Inherits:
-
Object
- Object
- GraphQL::Visitor
- Defined in:
- lib/graph_ql/parser/visitor.rb
Overview
Depth-first traversal through the tree, calling hooks at each stop.
@example: Create a visitor, add hooks, then search a document
total_field_count = 0
visitor = GraphQL::Visitor.new
visitor[GraphQL::Nodes::Field] << -> (node) { total_field_count += 1 }
visitor[GraphQL::Nodes::Document].leave << -> (node) { p total_field_count }
visitor.visit(document)
# => 6
Defined Under Namespace
Classes: NodeVisitor
Constant Summary collapse
- SKIP =
:_skip
Instance Attribute Summary collapse
-
#enter ⇒ Object
readonly
Returns the value of attribute enter.
-
#leave ⇒ Object
readonly
Returns the value of attribute leave.
Instance Method Summary collapse
- #[](node_class) ⇒ Object
-
#initialize ⇒ Visitor
constructor
A new instance of Visitor.
-
#visit(root, parent = nil) ⇒ Object
Apply built-up vistors to ‘document`.
Constructor Details
#initialize ⇒ Visitor
Returns a new instance of Visitor.
15 16 17 18 19 |
# File 'lib/graph_ql/parser/visitor.rb', line 15 def initialize @visitors = {} @enter = [] @leave = [] end |
Instance Attribute Details
#enter ⇒ Object (readonly)
Returns the value of attribute enter.
14 15 16 |
# File 'lib/graph_ql/parser/visitor.rb', line 14 def enter @enter end |
#leave ⇒ Object (readonly)
Returns the value of attribute leave.
14 15 16 |
# File 'lib/graph_ql/parser/visitor.rb', line 14 def leave @leave end |
Instance Method Details
#[](node_class) ⇒ Object
21 22 23 |
# File 'lib/graph_ql/parser/visitor.rb', line 21 def [](node_class) @visitors[node_class] ||= NodeVisitor.new end |