Commit 41c73d84 authored by Alex Kalderimis's avatar Alex Kalderimis

[GQL] BaseResolver: add synchronized_object

This allows us to DRY up code that uses the common pattern of synching
the resolver's object
parent 0569f469
......@@ -65,6 +65,17 @@ module Resolvers
end
end
def synchronized_object
strong_memoize(:synchronized_object) do
case object
when BatchLoader::GraphQL
object.sync
else
object
end
end
end
def single?
false
end
......
......@@ -121,4 +121,28 @@ describe Resolvers::BaseResolver do
end
end
end
describe '#synchronized_object' do
let(:object) { double(foo: :the_foo) }
let(:resolver) do
Class.new(described_class) do
def resolve(**args)
[synchronized_object.foo]
end
end
end
it 'handles raw objects' do
expect(resolve(resolver, obj: object)).to contain_exactly(:the_foo)
end
it 'handles lazy objects' do
delayed = BatchLoader::GraphQL.for(1).batch do |_, loader|
loader.call(1, object)
end
expect(resolve(resolver, obj: delayed)).to contain_exactly(:the_foo)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment