class GraphQL::FragmentCache::Fragment

Represents a single fragment to cache

Constants

NIL_IN_CACHE

Attributes

context[R]
options[R]
path[R]

Public Class Methods

new(context, **options) click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 13
def initialize(context, **options)
  @context = context
  @options = options
  @path = interpreter_context[:current_path]
end

Public Instance Methods

cache_key() click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 28
def cache_key
  @cache_key ||= CacheKeyBuilder.call(path: path, query: context.query, **options)
end
read(keep_in_context = false) click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 21
def read(keep_in_context = false)
  return nil if context[:renew_cache] == true
  return read_from_context { value_from_cache } if keep_in_context

  value_from_cache
end
value() click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 36
def value
  final_value.dig(*path)
end
with_final_value?() click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 32
def with_final_value?
  !final_value.nil?
end

Private Instance Methods

final_value() click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 60
def final_value
  @final_value ||= context.query.result["data"]
end
interpreter_context() click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 56
def interpreter_context
  context.namespace(:interpreter)
end
read_from_context() { || ... } click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 42
def read_from_context
  if (loaded_value = context.loaded_fragments[cache_key])
    return loaded_value
  end

  yield.tap { |value| context.loaded_fragments[cache_key] = value }
end
value_from_cache() click to toggle source
# File lib/graphql/fragment_cache/fragment.rb, line 50
def value_from_cache
  FragmentCache.cache_store.read(cache_key).tap do |cached|
    return NIL_IN_CACHE if cached.nil? && FragmentCache.cache_store.exist?(cache_key)
  end
end