Sha256: 98c09663b6f05586fd85adfd004153fa925ca6fadc6f0046097ee88700f250ec

Contents?: true

Size: 1.11 KB

Versions: 95

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Analysis
    # A query reducer for measuring the depth of a given query.
    #
    # @example Logging the depth of a query
    #   Schema.query_analyzers << GraphQL::Analysis::QueryDepth.new { |query, depth|  puts "GraphQL query depth: #{depth}" }
    #   Schema.execute(query_str)
    #   # GraphQL query depth: 8
    #
    class QueryDepth
      def initialize(&block)
        @depth_handler = block
      end

      def initial_value(query)
        {
          max_depth: 0,
          current_depth: 0,
          query: query,
        }
      end

      def call(memo, visit_type, irep_node)
        if irep_node.ast_node.is_a?(GraphQL::Language::Nodes::Field)
          if visit_type == :enter
            memo[:current_depth] += 1
          else
            if memo[:max_depth] < memo[:current_depth]
              memo[:max_depth] = memo[:current_depth]
            end
            memo[:current_depth] -= 1
          end
        end
        memo
      end

      def final_value(memo)
        @depth_handler.call(memo[:query], memo[:max_depth])
      end
    end
  end
end

Version data entries

95 entries across 95 versions & 2 rubygems

Version Path
graphql-1.11.12 lib/graphql/analysis/query_depth.rb
graphql-1.11.11 lib/graphql/analysis/query_depth.rb
graphql-1.12.25 lib/graphql/analysis/query_depth.rb
graphql-1.13.24 lib/graphql/analysis/query_depth.rb
graphql-1.13.23 lib/graphql/analysis/query_depth.rb
graphql-1.13.22 lib/graphql/analysis/query_depth.rb
graphql-1.13.21 lib/graphql/analysis/query_depth.rb
graphql-1.13.20 lib/graphql/analysis/query_depth.rb
graphql-1.13.19 lib/graphql/analysis/query_depth.rb
graphql-1.13.18 lib/graphql/analysis/query_depth.rb
graphql-1.13.17 lib/graphql/analysis/query_depth.rb
graphql-1.13.16 lib/graphql/analysis/query_depth.rb
graphql-1.13.15 lib/graphql/analysis/query_depth.rb
graphql-1.13.14 lib/graphql/analysis/query_depth.rb
graphql-1.13.13 lib/graphql/analysis/query_depth.rb
graphql_cody-1.13.0 lib/graphql/analysis/query_depth.rb
graphql-1.13.12 lib/graphql/analysis/query_depth.rb
graphql-1.13.11 lib/graphql/analysis/query_depth.rb
graphql-1.13.10 lib/graphql/analysis/query_depth.rb
graphql-1.13.9 lib/graphql/analysis/query_depth.rb