class GraphQL::Analysis::AST::Visitor

@see {GraphQL::Analysis::AST::Analyzer} AST Analyzers for queries
as skipped fields and visiting fragment spreads.
only the selected operation, providing helpers for common use cases such
The visitor is a special case of GraphQL::Language::Visitor, visiting
along the way.
Depth first traversal through a query AST, calling AST analyzers

def argument_definition

Returns:
  • (GraphQL::Argument, nil) - The most-recently-entered GraphQL::Argument, if currently inside one
def argument_definition
  @argument_definitions.last
end

def arguments_for(ast_node, field_definition)

Other tags:
    See: {GraphQL::Query#arguments_for} -

Returns:
  • (GraphQL::Query::Arguments) - Arguments for this node, merging default values, literal values and query variables
def arguments_for(ast_node, field_definition)
  @query.arguments_for(ast_node, field_definition)
end

def call_analyzers(method, node, parent)

def call_analyzers(method, node, parent)
  @analyzers.each do |analyzer|
    analyzer.public_send(method, node, parent, self)
  end
end

def directive_definition

Returns:
  • (GraphQL::Directive, nil) - The most-recently-entered GraphQL::Directive, if currently inside one
def directive_definition
  @directive_definitions.last
end

def enter_fragment_spread_inline(fragment_spread)

by itself.
Visit a fragment spread inline instead of visiting the definition
def enter_fragment_spread_inline(fragment_spread)
  fragment_def = query.fragments[fragment_spread.name]
  object_type = if fragment_def.type
    @query.warden.get_type(fragment_def.type.name)
  else
    object_types.last
  end
  object_types << object_type
  fragment_def.selections.each do |selection|
    visit_node(selection, fragment_def)
  end
end

def field_definition

Returns:
  • (GraphQL::Field, nil) - The most-recently-entered GraphQL::Field, if currently inside one
def field_definition
  @field_definitions.last
end

def initialize(query:, analyzers:)

def initialize(query:, analyzers:)
  @analyzers = analyzers
  @path = []
  @object_types = []
  @directives = []
  @field_definitions = []
  @argument_definitions = []
  @directive_definitions = []
  @query = query
  @schema = query.schema
  @response_path = []
  @skip_stack = [false]
  super(query.selected_operation)
end

def leave_fragment_spread_inline(_fragment_spread)

by itself.
Visit a fragment spread inline instead of visiting the definition
def leave_fragment_spread_inline(_fragment_spread)
  object_types.pop
end

def on_abstract_node(node, parent)

def on_abstract_node(node, parent)
  call_analyzers(:on_enter_abstract_node, node, parent)
  super
  call_analyzers(:on_leave_abstract_node, node, parent)
end

def on_argument(node, parent)

def on_argument(node, parent)
  argument_defn = if (arg = @argument_definitions.last)
    arg_type = arg.type.unwrap
    if arg_type.kind.input_object?
      arg_type.arguments[node.name]
    else
      nil
    end
  elsif (directive_defn = @directive_definitions.last)
    directive_defn.arguments[node.name]
  elsif (field_defn = @field_definitions.last)
    field_defn.arguments[node.name]
  else
    nil
  end
  @argument_definitions.push(argument_defn)
  @path.push(node.name)
  call_analyzers(:on_enter_argument, node, parent)
  super
  call_analyzers(:on_leave_argument, node, parent)
  @argument_definitions.pop
  @path.pop
end

def on_directive(node, parent)

def on_directive(node, parent)
  directive_defn = @schema.directives[node.name]
  @directive_definitions.push(directive_defn)
  call_analyzers(:on_enter_directive, node, parent)
  super
  call_analyzers(:on_leave_directive, node, parent)
  @directive_definitions.pop
end

def on_field(node, parent)

def on_field(node, parent)
  @response_path.push(node.alias || node.name)
  parent_type = @object_types.last
  field_definition = @schema.get_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
  call_analyzers(:on_enter_field, node, parent)
  super
  @skipping = @skip_stack.pop
  call_analyzers(:on_leave_field, node, parent)
  @response_path.pop
  @field_definitions.pop
  @object_types.pop
  @path.pop
end

def on_fragment_definition(node, parent)

def on_fragment_definition(node, parent)
  on_fragment_with_type(node) do
    @path.push("fragment #{node.name}")
    @in_fragment_def = false
    call_analyzers(:on_enter_fragment_definition, node, parent)
    super
    @in_fragment_def = false
    call_analyzers(:on_leave_fragment_definition, node, parent)
  end
end

def on_fragment_spread(node, parent)

def on_fragment_spread(node, parent)
  @path.push("... #{node.name}")
  call_analyzers(:on_enter_fragment_spread, node, parent)
  enter_fragment_spread_inline(node)
  super
  leave_fragment_spread_inline(node)
  call_analyzers(:on_leave_fragment_spread, node, parent)
  @path.pop
end

def on_fragment_with_type(node)

def on_fragment_with_type(node)
  object_type = if node.type
    @query.warden.get_type(node.type.name)
  else
    @object_types.last
  end
  @object_types.push(object_type)
  yield(node)
  @object_types.pop
  @path.pop
end

def on_inline_fragment(node, parent)

def on_inline_fragment(node, parent)
  on_fragment_with_type(node) do
    @path.push("...#{node.type ? " on #{node.type.name}" : ""}")
    call_analyzers(:on_enter_inline_fragment, node, parent)
    super
    call_analyzers(:on_leave_inline_fragment, node, parent)
  end
end

def on_operation_definition(node, parent)

def on_operation_definition(node, parent)
  object_type = @schema.root_type_for_operation(node.operation_type)
  @object_types.push(object_type)
  @path.push("#{node.operation_type}#{node.name ? " #{node.name}" : ""}")
  call_analyzers(:on_enter_operation_definition, node, parent)
  super
  call_analyzers(:on_leave_operation_definition, node, parent)
  @object_types.pop
  @path.pop
end

def parent_type_definition

Returns:
  • (GraphQL::BaseType) - The type which the current type came from
def parent_type_definition
  @object_types[-2]
end

def previous_argument_definition

Returns:
  • (GraphQL::Argument, nil) - The previous GraphQL argument
def previous_argument_definition
  @argument_definitions[-2]
end

def previous_field_definition

Returns:
  • (GraphQL::Field, nil) - The GraphQL field which returned the object that the current field belongs to
def previous_field_definition
  @field_definitions[-2]
end

def response_path

Returns:
  • (Array) - The path to the response key for the current field
def response_path
  @response_path.dup
end

def skip?(ast_node)

def skip?(ast_node)
  dir = ast_node.directives
  dir.any? && !GraphQL::Execution::DirectiveChecks.include?(dir, query)
end

def skipping?

Returns:
  • (Boolean) - If the current node should be skipped because of a skip or include directive
def skipping?
  @skipping
end

def type_definition

Returns:
  • (GraphQL::BaseType) - The current object type
def type_definition
  @object_types.last
end

def visit

def visit
  return unless @document
  super
end

def visiting_fragment_definition?

Returns:
  • (Boolean) - If the visitor is currently inside a fragment definition
def visiting_fragment_definition?
  @in_fragment_def
end