Sha256: 4a01b081e17f82fc5b0ea466800539e2775fdc63bad17673c59be314cb9d2c92

Contents?: true

Size: 1.39 KB

Versions: 90

Compression:

Stored size: 1.39 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
    #     use GraphQL::Analysis::AST
    #     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
          super
        end

        def on_enter_field(node, parent, visitor)
          return if visitor.skipping? || visitor.visiting_fragment_definition?

          @current_depth += 1
        end

        def on_leave_field(node, parent, visitor)
          return if visitor.skipping? || visitor.visiting_fragment_definition?

          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

90 entries across 90 versions & 2 rubygems

Version Path
graphql-1.13.23 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.22 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.21 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.20 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.19 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.18 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.17 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.16 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.15 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.14 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.13 lib/graphql/analysis/ast/query_depth.rb
graphql_cody-1.13.0 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.12 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.11 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.10 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.9 lib/graphql/analysis/ast/query_depth.rb
graphql-1.12.24 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.8 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.7 lib/graphql/analysis/ast/query_depth.rb
graphql-1.13.6 lib/graphql/analysis/ast/query_depth.rb