Sha256: 7b27ef39797331a0e42971e5725e0c9c1b1feedee2370a51e3ee46a00b868f42

Contents?: true

Size: 1.65 KB

Versions: 32

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Analysis
    # A query reducer for measuring the depth of a given query.
    #
    # See https://graphql-ruby.org/queries/ast_analysis.html for more 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
    #
    module AST
      class QueryDepth < Analyzer
        def initialize(query)
          @max_depth = 0
          @current_depth = 0
          @count_introspection_fields = query.schema.count_introspection_fields
          super
        end

        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

        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

        def result
          @max_depth
        end
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
graphql-2.2.16 lib/graphql/analysis/ast/query_depth.rb
graphql-2.1.13 lib/graphql/analysis/ast/query_depth.rb
graphql-2.3.5 lib/graphql/analysis/ast/query_depth.rb
graphql-2.3.2 lib/graphql/analysis/ast/query_depth.rb
graphql-2.3.1 lib/graphql/analysis/ast/query_depth.rb
graphql-2.3.0 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.14 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.13 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.12 lib/graphql/analysis/ast/query_depth.rb
graphql-2.1.12 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.11 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.10 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.9 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.8 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.7 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.6 lib/graphql/analysis/ast/query_depth.rb
graphql-2.1.11 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.5 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.4 lib/graphql/analysis/ast/query_depth.rb
graphql-2.2.3 lib/graphql/analysis/ast/query_depth.rb