Class: GraphQL::Analysis::QueryDepth
- Defined in:
- lib/graphql/analysis/query_depth.rb
Overview
A query reducer for measuring the depth of a given query.
See https://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
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(query) ⇒ QueryDepth
constructor
A new instance of QueryDepth.
- #on_enter_field(node, parent, visitor) ⇒ Object
- #on_leave_field(node, parent, visitor) ⇒ Object
- #result ⇒ Object
Methods inherited from Analyzer
Constructor Details
#initialize(query) ⇒ QueryDepth
Returns a new instance of QueryDepth.
33 34 35 36 37 38 |
# File 'lib/graphql/analysis/query_depth.rb', line 33 def initialize(query) @max_depth = 0 #: Integer @current_depth = 0 @count_introspection_fields = query.schema.count_introspection_fields super end |
Instance Method Details
#on_enter_field(node, parent, visitor) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/graphql/analysis/query_depth.rb', line 40 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 |
#on_leave_field(node, parent, visitor) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/graphql/analysis/query_depth.rb', line 48 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 |
#result ⇒ Object
59 60 61 |
# File 'lib/graphql/analysis/query_depth.rb', line 59 def result @max_depth end |