Class: GraphQL::Analysis::QueryDepth
- Inherits:
-
Object
- Object
- GraphQL::Analysis::QueryDepth
- Defined in:
- lib/graphql/analysis/query_depth.rb
Overview
A query reducer for measuring the depth of a given query.
Direct Known Subclasses
Instance Method Summary collapse
- #call(memo, visit_type, irep_node) ⇒ Object
- #final_value(memo) ⇒ Object
- #initial_value(query) ⇒ Object
-
#initialize(&block) ⇒ QueryDepth
constructor
A new instance of QueryDepth.
Constructor Details
#initialize(&block) ⇒ QueryDepth
Returns a new instance of QueryDepth.
11 12 13 |
# File 'lib/graphql/analysis/query_depth.rb', line 11 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 37 38 39 40 41 42 43 44 45 46 47 |
# 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 if GraphQL::Schema::DYNAMIC_FIELDS.include?(irep_node.definition_name) # Don't validate introspection fields memo[:skip_current_scope] = true elsif memo[:skip_current_scope] # we're inside an introspection query elsif GraphQL::Execution::DirectiveChecks.include?(irep_node, memo[:query]) memo[:current_depth] += 1 end else if GraphQL::Schema::DYNAMIC_FIELDS.include?(irep_node.definition_name) memo[:skip_current_scope] = false elsif GraphQL::Execution::DirectiveChecks.include?(irep_node, memo[:query]) if memo[:max_depth] < memo[:current_depth] memo[:max_depth] = memo[:current_depth] end memo[:current_depth] -= 1 end end end memo end |
#final_value(memo) ⇒ Object
49 50 51 |
# File 'lib/graphql/analysis/query_depth.rb', line 49 def final_value(memo) @depth_handler.call(memo[:query], memo[:max_depth]) end |
#initial_value(query) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/graphql/analysis/query_depth.rb', line 15 def initial_value(query) { max_depth: 0, current_depth: 0, skip_current_scope: false, query: query, } end |