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



18
19
20
21
22
23
# File 'lib/graphql/analysis/ast/query_depth.rb', line 18

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

Instance Method Details

#on_enter_field(node, parent, visitor) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/graphql/analysis/ast/query_depth.rb', line 25

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

  # Don't validate introspection fields or skipped nodes
  if GraphQL::Schema::DYNAMIC_FIELDS.include?(visitor.field_definition.name)
    @skip_depth += 1
  elsif @skip_depth > 0
    # we're inside an introspection query or skipped node
  else
    @current_depth += 1
  end
end

#on_enter_fragment_spread(node, _, visitor) ⇒ Object



52
53
54
# File 'lib/graphql/analysis/ast/query_depth.rb', line 52

def on_enter_fragment_spread(node, _, visitor)
  visitor.enter_fragment_spread_inline(node)
end

#on_leave_field(node, parent, visitor) ⇒ Object



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

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

  # Don't validate introspection fields or skipped nodes
  if GraphQL::Schema::DYNAMIC_FIELDS.include?(visitor.field_definition.name)
    @skip_depth -= 1
  else
    if @max_depth < @current_depth
      @max_depth = @current_depth
    end
    @current_depth -= 1
  end
end

#on_leave_fragment_spread(node, _, visitor) ⇒ Object



56
57
58
# File 'lib/graphql/analysis/ast/query_depth.rb', line 56

def on_leave_fragment_spread(node, _, visitor)
  visitor.leave_fragment_spread_inline(node)
end

#resultObject



60
61
62
# File 'lib/graphql/analysis/ast/query_depth.rb', line 60

def result
  @max_depth
end