Class: GraphQL::Analysis::AST::QueryDepth

Inherits:
Analyzer
  • Object
show all
Defined in:
lib/graphql/analysis/ast/query_depth.rb

Direct Known Subclasses

MaxQueryDepth

Instance Method Summary collapse

Methods inherited from Analyzer

#analyze?, build_visitor_hooks

Constructor Details

#initialize(query) ⇒ QueryDepth

Returns a new instance of QueryDepth



29
30
31
32
33
# File 'lib/graphql/analysis/ast/query_depth.rb', line 29

def initialize(query)
  @max_depth = 0
  @current_depth = 0
  super
end

Instance Method Details

#on_enter_field(node, parent, visitor) ⇒ Object



35
36
37
38
39
# File 'lib/graphql/analysis/ast/query_depth.rb', line 35

def on_enter_field(node, parent, visitor)
  return if visitor.skipping? || visitor.visiting_fragment_definition?

  @current_depth += 1
end

#on_leave_field(node, parent, visitor) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/graphql/analysis/ast/query_depth.rb', line 41

def on_leave_field(node, parent, visitor)
  return if visitor.skipping? || visitor.visiting_fragment_definition?

  if @max_depth < @current_depth
    @max_depth = @current_depth
  end
  @current_depth -= 1
end

#resultObject



50
51
52
# File 'lib/graphql/analysis/ast/query_depth.rb', line 50

def result
  @max_depth
end