Commit 0569f469 authored by Alex Kalderimis's avatar Alex Kalderimis

[GQL] BaseResolver: support #ready? in single and last

This adds support for the framework method `#ready?`, allowing resolvers
to use it and have derived single resolvers behave correctly.
parent 7a533749
......@@ -3,27 +3,33 @@
module Resolvers
class BaseResolver < GraphQL::Schema::Resolver
extend ::Gitlab::Utils::Override
include ::Gitlab::Utils::StrongMemoize
def self.single
@single ||= Class.new(self) do
def ready?(**args)
ready, early_return = super
[ready, select_result(early_return)]
end
def resolve(**args)
super.first
select_result(super)
end
def single?
true
end
def select_result(results)
results&.first
end
end
end
def self.last
@last ||= Class.new(self) do
def resolve(**args)
super.last
end
def single?
true
@last ||= Class.new(self.single) do
def select_result(results)
results&.last
end
end
end
......
......@@ -41,9 +41,35 @@ describe Resolvers::BaseResolver do
end
end
context 'when the resolver returns early' do
let(:resolver) do
Class.new(described_class) do
def ready?(**args)
[false, %w(early return)]
end
def resolve(**args)
raise 'Should not get here'
end
end
end
it 'runs correctly in our test framework' do
expect(resolve(resolver)).to contain_exactly('early', 'return')
end
it 'single selects the first early return value' do
expect(resolve(resolver.single)).to eq('early')
end
it 'last selects the last early return value' do
expect(resolve(resolver.last)).to eq('return')
end
end
describe '.last' do
it 'returns a subclass from the resolver' do
expect(last_resolver.last.superclass).to eq(last_resolver)
expect(last_resolver.last.ancestors).to include(last_resolver)
end
it 'returns the same subclass every time' do
......
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