Class: GraphQL::Analysis::QueryComplexity
- Inherits:
-
Object
- Object
- GraphQL::Analysis::QueryComplexity
- Defined in:
- lib/graphql/analysis/query_complexity.rb
Overview
Calculate the complexity of a query, using Field#complexity values.
Direct Known Subclasses
Defined Under Namespace
Classes: TypeComplexity
Instance Method Summary collapse
-
#call(memo, visit_type, irep_node) ⇒ Object
Implement the query analyzer API.
-
#final_value(reduced_value) ⇒ Object, GraphQL::AnalysisError
Send the query and complexity to the block.
-
#initial_value(query) ⇒ Object
State for the query complexity calcuation: -
query
is needed for variables, then passed to handler -complexities_on_type
holds complexity scores for each type in an IRep node -skip_depth
increments for each skipped node, then decrements on the way out. -
#initialize {|query, complexity| ... } ⇒ QueryComplexity
constructor
A new instance of QueryComplexity.
Constructor Details
#initialize {|query, complexity| ... } ⇒ QueryComplexity
Returns a new instance of QueryComplexity.
14 15 16 |
# File 'lib/graphql/analysis/query_complexity.rb', line 14 def initialize(&block) @complexity_handler = block end |
Instance Method Details
#call(memo, visit_type, irep_node) ⇒ Object
Implement the query analyzer API
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/graphql/analysis/query_complexity.rb', line 32 def call(memo, visit_type, irep_node) if irep_node.ast_node.is_a?(GraphQL::Language::Nodes::Field) if visit_type == :enter if irep_node.skipped? memo[:skip_depth] += 1 elsif memo[:skip_depth] == 0 memo[:complexities_on_type].push(TypeComplexity.new) end else if memo[:skip_depth] > 0 if irep_node.skipped? memo[:skip_depth] -= 1 end else type_complexities = memo[:complexities_on_type].pop child_complexity = type_complexities.max_possible_complexity own_complexity = get_complexity(irep_node, memo[:query], child_complexity) memo[:complexities_on_type].last.merge(irep_node.owner_type, own_complexity) end end end memo end |
#final_value(reduced_value) ⇒ Object, GraphQL::AnalysisError
Send the query and complexity to the block
58 59 60 61 |
# File 'lib/graphql/analysis/query_complexity.rb', line 58 def final_value(reduced_value) total_complexity = reduced_value[:complexities_on_type].pop.max_possible_complexity @complexity_handler.call(reduced_value[:query], total_complexity) end |
#initial_value(query) ⇒ Object
State for the query complexity calcuation:
query
is needed for variables, then passed to handlercomplexities_on_type
holds complexity scores for each type in an IRep nodeskip_depth
increments for each skipped node, then decrements on the way out. While it's greater than0
, we're visiting a skipped part of the query.
23 24 25 26 27 28 29 |
# File 'lib/graphql/analysis/query_complexity.rb', line 23 def initial_value(query) { query: query, complexities_on_type: [TypeComplexity.new], skip_depth: 0, } end |