Class: GraphQL::Analysis::QueryDepth

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

Overview

A query reducer for measuring the depth of a given query.

Examples:

Logging the depth of a query

Schema.query_analyzers << GraphQL::Analysis::QueryDepth.new { |query, depth|  puts "GraphQL query depth: #{depth}" }
Schema.execute(query_str)
# GraphQL query depth: 8

Direct Known Subclasses

MaxQueryDepth

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ QueryDepth

Returns a new instance of QueryDepth



12
13
14
# File 'lib/graphql/analysis/query_depth.rb', line 12

def initialize(&block)
  @depth_handler = block
end

Instance Method Details

#call(memo, visit_type, irep_node) ⇒ Object



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

def call(memo, visit_type, irep_node)
  if irep_node.ast_node.is_a?(GraphQL::Language::Nodes::Field)
    if visit_type == :enter
      memo[:current_depth] += 1
    else
      if memo[:max_depth] < memo[:current_depth]
        memo[:max_depth] = memo[:current_depth]
      end
      memo[:current_depth] -= 1
    end
  end
  memo
end

#final_value(memo) ⇒ Object



38
39
40
# File 'lib/graphql/analysis/query_depth.rb', line 38

def final_value(memo)
  @depth_handler.call(memo[:query], memo[:max_depth])
end

#initial_value(query) ⇒ Object



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

def initial_value(query)
  {
    max_depth: 0,
    current_depth: 0,
    query: query,
  }
end