Class: GraphQL::Analysis::Visitor
- Inherits:
-
Language::StaticVisitor
- Object
- Language::StaticVisitor
- GraphQL::Analysis::Visitor
- Defined in:
- lib/graphql/analysis/visitor.rb
Overview
Depth first traversal through a query AST, calling AST analyzers along the way.
The visitor is a special case of GraphQL::Language::StaticVisitor, visiting only the selected operation, providing helpers for common use cases such as skipped fields and visiting fragment spreads.
See GraphQL::Analysis::Analyzer AST Analyzers for queries
Instance Attribute Summary collapse
-
#object_types ⇒ Object
readonly
Returns.
-
#query ⇒ Object
readonly
Returns.
-
#rescued_errors ⇒ Object
readonly
Returns.
Instance Method Summary collapse
-
#argument_definition ⇒ Object
Returns.
- #arguments_for(ast_node, field_definition) ⇒ Object
-
#directive_definition ⇒ Object
Returns.
-
#field_definition ⇒ Object
Returns.
-
#initialize(query:, analyzers:, timeout:) ⇒ Visitor
constructor
A new instance of Visitor.
- #on_argument(node, parent) ⇒ Object
- #on_directive(node, parent) ⇒ Object
- #on_field(node, parent) ⇒ Object
- #on_fragment_spread(node, parent) ⇒ Object
- #on_inline_fragment(node, parent) ⇒ Object
-
#on_operation_definition(node, parent) ⇒ Object
rubocop:enable Development/NoEvalCop.
-
#parent_type_definition ⇒ Object
Returns.
-
#previous_argument_definition ⇒ Object
Returns.
-
#previous_field_definition ⇒ Object
Returns.
-
#response_path ⇒ Object
Returns.
-
#skipping? ⇒ Boolean
Returns.
-
#type_definition ⇒ Object
Returns.
- #visit ⇒ Object
-
#visiting_fragment_definition? ⇒ Boolean
Returns.
Methods inherited from Language::StaticVisitor
make_visit_methods, #on_argument_children, #on_document_children, #on_field_children, #on_fragment_definition_children, #on_operation_definition_children, #visit_directives, #visit_selections
Constructor Details
#initialize(query:, analyzers:, timeout:) ⇒ Visitor
Returns a new instance of Visitor.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/graphql/analysis/visitor.rb', line 15 def initialize(query:, analyzers:, timeout:) @analyzers = analyzers @path = [] @object_types = [] @directives = [] @field_definitions = [] @argument_definitions = [] @directive_definitions = [] @rescued_errors = [] @query = query @schema = query.schema @types = query.types @response_path = [] @skip_stack = [false] # For each selection set being visited, the fragments already inlined into it. @inlined_fragments_stack = [{}] @timeout_time = if timeout Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second) + timeout else Float::INFINITY end super(query.selected_operation) end |
Instance Attribute Details
#object_types ⇒ Object (readonly)
Returns
Array<GraphQL::ObjectType>— Types whose scope we've entered
47 48 49 |
# File 'lib/graphql/analysis/visitor.rb', line 47 def object_types @object_types end |
#query ⇒ Object (readonly)
Returns
GraphQL::Query— the query being visited
42 43 44 |
# File 'lib/graphql/analysis/visitor.rb', line 42 def query @query end |
#rescued_errors ⇒ Object (readonly)
Returns
Array<GraphQL::AnalysisError>— Array of errors rescued during analysis
52 53 54 |
# File 'lib/graphql/analysis/visitor.rb', line 52 def rescued_errors @rescued_errors end |
Instance Method Details
#argument_definition ⇒ Object
Returns
GraphQL::Argument, nil— The most-recently-entered GraphQL::Argument, if currently inside one
:call-seq:
argument_definition() -> GraphQL::Argument | nil
306 307 308 |
# File 'lib/graphql/analysis/visitor.rb', line 306 def argument_definition @argument_definitions.last end |
#arguments_for(ast_node, field_definition) ⇒ Object
See GraphQL::Query#arguments_for
Returns
GraphQL::Execution::Interpreter::Arguments— Arguments for this node, merging default values, literal values and query variables
:call-seq:
arguments_for(ast_node, field_definition) -> GraphQL::Execution::Interpreter::Arguments
69 70 71 |
# File 'lib/graphql/analysis/visitor.rb', line 69 def arguments_for(ast_node, field_definition) @query.arguments_for(ast_node, field_definition) end |
#directive_definition ⇒ Object
Returns
GraphQL::Directive, nil— The most-recently-entered GraphQL::Directive, if currently inside one
:call-seq:
directive_definition() -> GraphQL::Directive | nil
296 297 298 |
# File 'lib/graphql/analysis/visitor.rb', line 296 def directive_definition @directive_definitions.last end |
#field_definition ⇒ Object
Returns
GraphQL::Field, nil— The most-recently-entered GraphQL::Field, if currently inside one
:call-seq:
field_definition() -> GraphQL::Field | nil
276 277 278 |
# File 'lib/graphql/analysis/visitor.rb', line 276 def field_definition @field_definitions.last end |
#on_argument(node, parent) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/graphql/analysis/visitor.rb', line 203 def on_argument(node, parent) check_timeout argument_defn = if (arg = @argument_definitions.last) arg_type = arg.type.unwrap if arg_type.kind.input_object? @types.argument(arg_type, node.name) else nil end elsif (directive_defn = @directive_definitions.last) @types.argument(directive_defn, node.name) elsif (field_defn = @field_definitions.last) @types.argument(field_defn, node.name) else nil end @argument_definitions.push(argument_defn) @path.push(node.name) call_on_enter_argument(node, parent) super call_on_leave_argument(node, parent) @argument_definitions.pop @path.pop end |
#on_directive(node, parent) ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/graphql/analysis/visitor.rb', line 193 def on_directive(node, parent) check_timeout directive_defn = @schema.directives[node.name] @directive_definitions.push(directive_defn) call_on_enter_directive(node, parent) super call_on_leave_directive(node, parent) @directive_definitions.pop end |
#on_field(node, parent) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/graphql/analysis/visitor.rb', line 163 def on_field(node, parent) check_timeout @response_path.push(node.alias || node.name) parent_type = @object_types.last # This could be nil if the previous field wasn't found: field_definition = parent_type && @types.field(parent_type, node.name) @field_definitions.push(field_definition) if !field_definition.nil? next_object_type = field_definition.type.unwrap @object_types.push(next_object_type) else @object_types.push(nil) end @path.push(node.alias || node.name) @skipping = @skip_stack.last || skip?(node) @skip_stack << @skipping @inlined_fragments_stack.push({}) call_on_enter_field(node, parent) super @skipping = @skip_stack.pop call_on_leave_field(node, parent) @inlined_fragments_stack.pop @response_path.pop @field_definitions.pop @object_types.pop @path.pop end |
#on_fragment_spread(node, parent) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/graphql/analysis/visitor.rb', line 229 def on_fragment_spread(node, parent) check_timeout @path.push("... #{node.name}") @skipping = @skip_stack.last || skip?(node) @skip_stack << @skipping call_on_enter_fragment_spread(node, parent) # Spreading the same fragment again into this selection set adds no new selections, so don't visit it again. inlined_fragments = @inlined_fragments_stack.last inlined_key = [node.name, @object_types.last, @skipping] if (inline_fragment = !inlined_fragments.key?(inlined_key)) inlined_fragments[inlined_key] = true end enter_fragment_spread_inline(node) if inline_fragment super @skipping = @skip_stack.pop leave_fragment_spread_inline(node) if inline_fragment call_on_leave_fragment_spread(node, parent) @path.pop end |
#on_inline_fragment(node, parent) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/graphql/analysis/visitor.rb', line 144 def on_inline_fragment(node, parent) check_timeout object_type = if node.type @types.type(node.type.name) else @object_types.last end @object_types.push(object_type) @path.push("...#{node.type ? " on #{node.type.name}" : ""}") @skipping = @skip_stack.last || skip?(node) @skip_stack << @skipping call_on_enter_inline_fragment(node, parent) super @skipping = @skip_stack.pop call_on_leave_inline_fragment(node, parent) @object_types.pop @path.pop end |
#on_operation_definition(node, parent) ⇒ Object
rubocop:enable Development/NoEvalCop
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/graphql/analysis/visitor.rb', line 130 def on_operation_definition(node, parent) check_timeout object_type = @schema.root_type_for_operation(node.operation_type) @object_types.push(object_type) @path.push("#{node.operation_type}#{node.name ? " #{node.name}" : ""}") @inlined_fragments_stack.push({}) call_on_enter_operation_definition(node, parent) super call_on_leave_operation_definition(node, parent) @inlined_fragments_stack.pop @object_types.pop @path.pop end |
#parent_type_definition ⇒ Object
Returns
GraphQL::BaseType— The type which the current type came from
:call-seq:
parent_type_definition() -> GraphQL::BaseType
266 267 268 |
# File 'lib/graphql/analysis/visitor.rb', line 266 def parent_type_definition @object_types[-2] end |
#previous_argument_definition ⇒ Object
Returns
GraphQL::Argument, nil— The previous GraphQL argument
:call-seq:
previous_argument_definition() -> GraphQL::Argument | nil
316 317 318 |
# File 'lib/graphql/analysis/visitor.rb', line 316 def previous_argument_definition @argument_definitions[-2] end |
#previous_field_definition ⇒ Object
Returns
GraphQL::Field, nil— The GraphQL field which returned the object that the current field belongs to
:call-seq:
previous_field_definition() -> GraphQL::Field | nil
286 287 288 |
# File 'lib/graphql/analysis/visitor.rb', line 286 def previous_field_definition @field_definitions[-2] end |
#response_path ⇒ Object
Returns
Array<String>— The path to the response key for the current field
:call-seq:
response_path() -> Array[String]
99 100 101 |
# File 'lib/graphql/analysis/visitor.rb', line 99 def response_path @response_path.dup end |
#skipping? ⇒ Boolean
Returns
Boolean— If the current node should be skipped because of a skip or include directive
:call-seq:
skipping?() -> bool
89 90 91 |
# File 'lib/graphql/analysis/visitor.rb', line 89 def skipping? @skipping end |
#type_definition ⇒ Object
Returns
GraphQL::BaseType— The current object type
:call-seq:
type_definition() -> GraphQL::BaseType
256 257 258 |
# File 'lib/graphql/analysis/visitor.rb', line 256 def type_definition @object_types.last end |
#visit ⇒ Object
54 55 56 57 |
# File 'lib/graphql/analysis/visitor.rb', line 54 def visit return unless @document super end |
#visiting_fragment_definition? ⇒ Boolean
Returns
Boolean— If the visitor is currently inside a fragment definition
:call-seq:
visiting_fragment_definition?() -> bool
79 80 81 |
# File 'lib/graphql/analysis/visitor.rb', line 79 def visiting_fragment_definition? @in_fragment_def end |