class GraphQL::FragmentCache::FieldExtension

Wraps resolver with cache method

Constants

NOT_RESOLVED

Public Class Methods

new(options:, **_rest) click to toggle source
# File lib/graphql/fragment_cache/field_extension.rb, line 30
def initialize(options:, **_rest)
  @cache_options = GraphQL::FragmentCache.default_options.merge(options || {})
  @cache_options[:default_options_merged] = true

  @context_key = @cache_options.delete(:context_key)
  @cache_key = @cache_options.delete(:cache_key)
end

Public Instance Methods

resolve(object:, arguments:, **_options) { |object, arguments| ... } click to toggle source
# File lib/graphql/fragment_cache/field_extension.rb, line 40
def resolve(object:, arguments:, **_options)
  resolved_value = NOT_RESOLVED

  if @cache_options[:if].is_a?(Proc)
    @cache_options[:if] = object.instance_exec(&@cache_options[:if])
  end
  if @cache_options[:unless].is_a?(Proc)
    @cache_options[:unless] = object.instance_exec(&@cache_options[:unless])
  end

  object_for_key = if @context_key
    Array(@context_key).map { |key| object.context[key] }
  elsif @cache_key == :object
    object.object
  elsif @cache_key == :value
    resolved_value = yield(object, arguments)
  end
  cache_fragment_options = @cache_options.merge(object: object_for_key)

  object.cache_fragment(**cache_fragment_options) do
    resolved_value == NOT_RESOLVED ? yield(object, arguments) : resolved_value
  end
end