Module: GraphQL::Analysis

Defined in:
lib/graphql/analysis/query_depth.rb,
lib/graphql/analysis/analyze_query.rb,
lib/graphql/analysis/max_query_depth.rb,
lib/graphql/analysis/query_complexity.rb,
lib/graphql/analysis/max_query_complexity.rb

Defined Under Namespace

Classes: MaxQueryComplexity, MaxQueryDepth, QueryComplexity, QueryDepth

Class Method Summary collapse

Class Method Details

.analyze_query(query, analyzers) ⇒ Array<Any>

Visit query‘s internal representation, calling analyzers along the way.

  • First, query analyzers are initialized by calling ‘.initial_value(query)`, if they respond to that method.

  • Then, they receive ‘.call(memo, visit_type, irep_node)`, where visit type is :enter or :leave.

  • Last, they receive ‘.final_value(memo)`, if they respond to that method.

It returns an array of final memo values in the order that analyzers were passed in.

Parameters:

  • query (GraphQL::Query)
  • analyzers (Array<#call>)

    Objects that respond to ‘#call(memo, visit_type, irep_node)`

Returns:

  • (Array<Any>)

    Results from those analyzers



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/graphql/analysis/analyze_query.rb', line 15

def analyze_query(query, analyzers)
  analyzers_and_values = analyzers.map { |r| initialize_reducer(r, query) }

  irep = query.internal_representation

  irep.each do |name, op_node|
    reduce_node(op_node, analyzers_and_values)
  end

  analyzers_and_values.map { |(r, value)| finalize_reducer(r, value) }
end

.finalize_reducer(reducer, reduced_value) ⇒ Any

If the reducer accepts final_value, send it the last memo value. Otherwise, use the last value from the traversal.

Returns:

  • (Any)

    final memo value



65
66
67
68
69
70
71
# File 'lib/graphql/analysis/analyze_query.rb', line 65

def finalize_reducer(reducer, reduced_value)
  if reducer.respond_to?(:final_value)
    reducer.final_value(reduced_value)
  else
    reduced_value
  end
end

.initialize_reducer(reducer, query) ⇒ Array<(#call, Any)>

If the reducer has an initial_value method, call it and store the result as memo. Otherwise, use nil as memo.

Returns:

  • (Array<(#call, Any)>)

    reducer-memo pairs



54
55
56
57
58
59
60
# File 'lib/graphql/analysis/analyze_query.rb', line 54

def initialize_reducer(reducer, query)
  if reducer.respond_to?(:initial_value)
    [reducer, reducer.initial_value(query)]
  else
    [reducer, nil]
  end
end

.reduce_node(irep_node, analyzers_and_values) ⇒ Object

Enter the node, visit its children, then leave the node.



32
33
34
35
36
37
38
39
40
# File 'lib/graphql/analysis/analyze_query.rb', line 32

def reduce_node(irep_node, analyzers_and_values)
  visit_analyzers(:enter, irep_node, analyzers_and_values)

  irep_node.children.each do |name, child_irep_node|
    reduce_node(child_irep_node, analyzers_and_values)
  end

  visit_analyzers(:leave, irep_node, analyzers_and_values)
end

.visit_analyzers(visit_type, irep_node, analyzers_and_values) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/graphql/analysis/analyze_query.rb', line 42

def visit_analyzers(visit_type, irep_node, analyzers_and_values)
  analyzers_and_values.each do |reducer_and_value|
    reducer = reducer_and_value[0]
    memo = reducer_and_value[1]
    next_memo = reducer.call(memo, visit_type, irep_node)
    reducer_and_value[1] = next_memo
  end
end