Class: GraphQL::FragmentCache::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/fragment_cache/fragment.rb

Overview

Represents a single fragment to cache

Constant Summary collapse

NIL_IN_CACHE =
Object.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, **options) ⇒ Fragment

Returns a new instance of Fragment.



53
54
55
56
57
58
# File 'lib/graphql/fragment_cache/fragment.rb', line 53

def initialize(context, **options)
  @context = context
  @keep_in_context = options.delete(:keep_in_context)
  @options = options
  @path = interpreter_context[:current_path]
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



51
52
53
# File 'lib/graphql/fragment_cache/fragment.rb', line 51

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



51
52
53
# File 'lib/graphql/fragment_cache/fragment.rb', line 51

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



51
52
53
# File 'lib/graphql/fragment_cache/fragment.rb', line 51

def path
  @path
end

Class Method Details

.read_multi(fragments) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/graphql/fragment_cache/fragment.rb', line 14

def read_multi(fragments)
  unless FragmentCache.cache_store.respond_to?(:read_multi)
    return fragments.map { |f| [f, f.read] }.to_h
  end

  fragments_to_cache_keys = fragments.map { |f| [f, f.cache_key] }.to_h

  # Filter out all the cache_keys for fragments with renew_cache: true in their context
  cache_keys = fragments_to_cache_keys.reject { |k, _v| k.context[:renew_cache] == true }.values

  # If there are cache_keys look up values with read_multi otherwise return an empty hash
  cache_keys_to_values = if cache_keys.empty?
    {}
  else
    FragmentCache.cache_store.read_multi(*cache_keys)
  end

  if GraphQL::FragmentCache.monitoring_enabled
    begin
      fragments.map do |fragment|
        cache_lookup_event(
          cache_key: fragment.cache_key,
          operation_name: fragment.context.query.operation_name,
          path: fragment.path,
          cache_hit: cache_keys_to_values.key?(fragment.cache_key)
        )
      end
    rescue
      # Allow cache_lookup_event to fail when we do not have all of the requested attributes
    end
  end

  # Fragmenst without values or with renew_cache: true in their context will have nil values like the read method
  fragments_to_cache_keys.map { |fragment, cache_key| [fragment, cache_keys_to_values[cache_key]] }.to_h
end

Instance Method Details

#cache_keyObject



67
68
69
# File 'lib/graphql/fragment_cache/fragment.rb', line 67

def cache_key
  @cache_key ||= CacheKeyBuilder.call(path: path, query: context.query, **options)
end

#readObject



60
61
62
63
64
65
# File 'lib/graphql/fragment_cache/fragment.rb', line 60

def read
  return nil if context[:renew_cache] == true
  return read_from_context { value_from_cache } if @keep_in_context

  value_from_cache
end

#valueObject



75
76
77
# File 'lib/graphql/fragment_cache/fragment.rb', line 75

def value
  final_value.dig(*path)
end

#with_final_value?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/graphql/fragment_cache/fragment.rb', line 71

def with_final_value?
  !final_value.nil?
end