Class: GraphQL::FragmentCache::Schema::LazyCacheResolver

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

Instance Method Summary collapse

Constructor Details

#initialize(fragment, query_ctx, object_to_cache, &block) ⇒ LazyCacheResolver

Returns a new instance of LazyCacheResolver.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/graphql/fragment_cache/schema/lazy_cache_resolver.rb', line 8

def initialize(fragment, query_ctx, object_to_cache, &block)
  @fragment = fragment
  @query_ctx = query_ctx
  @object_to_cache = object_to_cache
  @lazy_state = query_ctx[:lazy_cache_resolver_statez] ||= {
    pending_fragments: Set.new,
    resolved_fragments: {}
  }
  @block = block

  @lazy_state[:pending_fragments] << @fragment
end

Instance Method Details

#resolveObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/fragment_cache/schema/lazy_cache_resolver.rb', line 21

def resolve
  unless @lazy_state[:resolved_fragments].key?(@fragment)
    resolved_fragments = Fragment.read_multi(@lazy_state[:pending_fragments].to_a)
    @lazy_state[:pending_fragments].clear
    resolved_fragments.each { |key, value| @lazy_state[:resolved_fragments][key] = value }
  end

  cached = @lazy_state[:resolved_fragments][@fragment]

  if cached
    return (cached == Fragment::NIL_IN_CACHE) ? nil : GraphQL::Execution::Interpreter::RawValue.new(cached)
  end

  (@block ? @block.call : @object_to_cache).tap do |resolved_value|
    @query_ctx.fragments << @fragment
  end
end