Sha256: 0f3795020c76a48c0b36d47f08ae59d563972ae145d0d7f017034a3c85714945

Contents?: true

Size: 1.87 KB

Versions: 37

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module GraphQL
  module Tracing
    # Each platform provides:
    # - `.platform_keys`
    # - `#platform_trace`
    # - `#platform_field_key(type, field)`
    # @api private
    class PlatformTracing
      class << self
        attr_accessor :platform_keys
      end

      def initialize(options = {})
        @options = options
        @platform_keys = self.class.platform_keys
        @trace_scalars = options.fetch(:trace_scalars, false)
      end

      def trace(key, data)
        case key
        when "lex", "parse", "validate", "analyze_query", "analyze_multiplex", "execute_query", "execute_query_lazy", "execute_multiplex"
          platform_key = @platform_keys.fetch(key)
          platform_trace(platform_key, key, data) do
            yield
          end
        when "execute_field", "execute_field_lazy"
          if (platform_key = data[:context].field.metadata[:platform_key])
            platform_trace(platform_key, key, data) do
              yield
            end
          else
            yield
          end
        else
          # it's a custom key
          yield
        end
      end

      def instrument(type, field)
        return_type = field.type.unwrap
        case return_type
        when GraphQL::ScalarType, GraphQL::EnumType
          if field.trace || (field.trace.nil? && @trace_scalars)
            trace_field(type, field)
          else
            field
          end
        else
          trace_field(type, field)
        end
      end

      def trace_field(type, field)
        new_f = field.redefine
        new_f.metadata[:platform_key] = platform_field_key(type, field)
        new_f
      end

      def self.use(schema_defn, options = {})
        tracer = self.new(options)
        schema_defn.instrument(:field, tracer)
        schema_defn.tracer(tracer)
      end

      private
      attr_reader :options
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.17 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.16 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.15 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.14 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.13 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.12 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.11 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.10 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.9 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.8 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.7 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.6 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.5 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.4 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.3 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.2 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.1 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.0 lib/graphql/tracing/platform_tracing.rb
graphql-1.8.0.pre11 lib/graphql/tracing/platform_tracing.rb