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 @@ ...@@ -3,27 +3,33 @@
module Resolvers module Resolvers
class BaseResolver < GraphQL::Schema::Resolver class BaseResolver < GraphQL::Schema::Resolver
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
include ::Gitlab::Utils::StrongMemoize
def self.single def self.single
@single ||= Class.new(self) do @single ||= Class.new(self) do
def ready?(**args)
ready, early_return = super
[ready, select_result(early_return)]
end
def resolve(**args) def resolve(**args)
super.first select_result(super)
end end
def single? def single?
true true
end end
def select_result(results)
results&.first
end
end end
end end
def self.last def self.last
@last ||= Class.new(self) do @last ||= Class.new(self.single) do
def resolve(**args) def select_result(results)
super.last results&.last
end
def single?
true
end end
end end
end end
......
...@@ -41,9 +41,35 @@ describe Resolvers::BaseResolver do ...@@ -41,9 +41,35 @@ describe Resolvers::BaseResolver do
end end
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 describe '.last' do
it 'returns a subclass from the resolver' 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 end
it 'returns the same subclass every time' do 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