Module: GraphQL::Analysis

Defined in:
lib/graphql/analysis/field_usage.rb,
lib/graphql/analysis/query_depth.rb,
lib/graphql/analysis/analyze_query.rb,
lib/graphql/analysis/reducer_state.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: FieldUsage, MaxQueryComplexity, MaxQueryDepth, QueryComplexity, QueryDepth, ReducerState

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



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

def analyze_query(query, analyzers)
  reducer_states = analyzers.map { |r| ReducerState.new(r, query) }

  irep = query.internal_representation

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

  reducer_states.map(&:finalize_reducer)
end

.reduce_node(irep_node, reducer_states) ⇒ Object

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



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

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

  irep_node.typed_children.each do |type_defn, children|
    children.each do |name, child_irep_node|
      reduce_node(child_irep_node, reducer_states)
    end
  end

  visit_analyzers(:leave, irep_node, reducer_states)
end

.visit_analyzers(visit_type, irep_node, reducer_states) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/graphql/analysis/analyze_query.rb', line 45

def visit_analyzers(visit_type, irep_node, reducer_states)
  reducer_states.each do |reducer_state|
    next_memo = reducer_state.call(visit_type, irep_node)

    reducer_state.memo = next_memo
  end
end