class GraphQL::Analysis::QueryDepth
A query reducer for measuring the depth of a given query.
See graphql-ruby.org/queries/ast_analysis.html for more examples.
Examples
Example: Logging the depth of a query
class LogQueryDepth < GraphQL::Analysis::QueryDepth def result log("GraphQL query depth: #{@max_depth}") end end # In your Schema file: class MySchema < GraphQL::Schema query_analyzer LogQueryDepth end # When you run the query, the depth will get logged: Schema.execute(query_str) # GraphQL query depth: 8
Public Class Methods
Source
# File lib/graphql/analysis/query_depth.rb, line 32 def initialize(query) @max_depth = 0 @current_depth = 0 @count_introspection_fields = query.schema.count_introspection_fields super end
Calls superclass method
GraphQL::Analysis::Analyzer::new
Public Instance Methods
# File lib/graphql/analysis/query_depth.rb, line 39 def on_enter_field(node, parent, visitor) return if visitor.skipping? || visitor.visiting_fragment_definition? || (@count_introspection_fields == false && visitor.field_definition.introspection?) @current_depth += 1 end
# File lib/graphql/analysis/query_depth.rb, line 47 def on_leave_field(node, parent, visitor) return if visitor.skipping? || visitor.visiting_fragment_definition? || (@count_introspection_fields == false && visitor.field_definition.introspection?) if @max_depth < @current_depth @max_depth = @current_depth end @current_depth -= 1 end