Class: GraphQL::Cache::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/cache/fetcher.rb

Instance Method Summary collapse

Instance Method Details

#instrument(type, field) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/graphql/cache/fetcher.rb', line 4

def instrument(type, field)
  old_resolve_proc = field.resolve_proc

  new_resolve_proc = lambda do |obj, args, ctx|
    unless field.[:cache]
      return old_resolve_proc.call(obj, args, ctx)
    end

    key = cache_key(obj, args, type, field)

    Marshal[key].read(field.[:cache]) do
      old_resolve_proc.call(obj, args, ctx)
    end
  end

  # Return a copy of `field`, with the new resolve proc
  field.redefine { resolve(new_resolve_proc) }
end