Sha256: fd8d4bf6d38e0903118663a02f8a0f3c544b268addf58ca23231e0bf15071efb

Contents?: true

Size: 1.45 KB

Versions: 11

Compression:

Stored size: 1.45 KB

Contents

module GraphQL
  module Analysis
    # A query reducer for tracking both field usage and deprecated field usage.
    #
    # @example Logging field usage and deprecated field usage
    #   Schema.query_analyzers << GraphQL::Analysis::FieldUsage.new { |query, used_fields, used_deprecated_fields|
    #     puts "Used GraphQL fields: #{used_fields.join(', ')}"
    #     puts "Used deprecated GraphQL fields: #{used_deprecated_fields.join(', ')}"
    #   }
    #   Schema.execute(query_str)
    #   # Used GraphQL fields: Cheese.id, Cheese.fatContent, Query.cheese
    #   # Used deprecated GraphQL fields: Cheese.fatContent
    #
    class FieldUsage
      def initialize(&block)
        @field_usage_handler = block
      end

      def initial_value(query)
        {
          query: query,
          used_fields: Set.new,
          used_deprecated_fields: Set.new
        }
      end

      def call(memo, visit_type, irep_node)
        if irep_node.ast_node.is_a?(GraphQL::Language::Nodes::Field) && visit_type == :leave
          irep_node.definitions.each do |type_defn, field_defn|
            field = "#{type_defn.name}.#{field_defn.name}"
            memo[:used_fields] << field
            memo[:used_deprecated_fields] << field if field_defn.deprecation_reason
          end
        end

        memo
      end

      def final_value(memo)
        @field_usage_handler.call(memo[:query], memo[:used_fields].to_a, memo[:used_deprecated_fields].to_a)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
graphql-1.1.0 lib/graphql/analysis/field_usage.rb
graphql-1.0.0 lib/graphql/analysis/field_usage.rb
graphql-0.19.4 lib/graphql/analysis/field_usage.rb
graphql-0.19.3 lib/graphql/analysis/field_usage.rb
graphql-0.19.2 lib/graphql/analysis/field_usage.rb
graphql-0.19.1 lib/graphql/analysis/field_usage.rb
graphql-0.19.0 lib/graphql/analysis/field_usage.rb
graphql-0.18.15 lib/graphql/analysis/field_usage.rb
graphql-0.18.14 lib/graphql/analysis/field_usage.rb
graphql-0.18.13 lib/graphql/analysis/field_usage.rb
graphql-0.18.12 lib/graphql/analysis/field_usage.rb