Sha256: 90a9a670bd255df6cda4662a683b2d362dced0199c8bf36faeaf35e94bf46bf0

Contents?: true

Size: 1.36 KB

Versions: 34

Compression:

Stored size: 1.36 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
          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

34 entries across 34 versions & 1 rubygems

Version Path
graphql-2.0.31 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.29 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.28 lib/graphql/analysis/ast/query_depth.rb
graphql-2.1.1 lib/graphql/analysis/ast/query_depth.rb
graphql-2.1.0 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.27 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.26 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.25 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.24 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.23 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.22 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.21 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.20 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.17.2 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.17.1 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.19 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.18 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.17 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.16 lib/graphql/analysis/ast/query_depth.rb
graphql-2.0.15 lib/graphql/analysis/ast/query_depth.rb