2019-02-12 08:51:10 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Resolvers::BaseResolver do
|
2019-02-12 08:51:10 -05:00
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
let(:resolver) do
|
|
|
|
Class.new(described_class) do
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :test, ::GraphQL::Types::Int, required: false
|
|
|
|
type [::GraphQL::Types::Int], null: true
|
2020-11-09 19:08:52 -05:00
|
|
|
|
|
|
|
def resolve(test: 100)
|
2019-12-13 04:08:01 -05:00
|
|
|
process(object)
|
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
[test, test]
|
2019-02-12 08:51:10 -05:00
|
|
|
end
|
2019-12-13 04:08:01 -05:00
|
|
|
|
|
|
|
def process(obj); end
|
2019-02-12 08:51:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-14 07:06:30 -05:00
|
|
|
let(:last_resolver) do
|
|
|
|
Class.new(described_class) do
|
2021-07-22 08:10:04 -04:00
|
|
|
type [::GraphQL::Types::Int], null: true
|
2020-11-09 19:08:52 -05:00
|
|
|
|
2019-11-14 07:06:30 -05:00
|
|
|
def resolve(**args)
|
|
|
|
[1, 2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
describe '.singular_type' do
|
|
|
|
subject { resolver.singular_type }
|
|
|
|
|
|
|
|
context 'for a connection of scalars' do
|
|
|
|
let(:resolver) do
|
|
|
|
Class.new(described_class) do
|
2021-07-22 08:10:04 -04:00
|
|
|
type ::GraphQL::Types::Int.connection_type, null: true
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
it { is_expected.to eq(::GraphQL::Types::Int) }
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a connection of objects' do
|
|
|
|
let(:object) do
|
|
|
|
Class.new(::Types::BaseObject) do
|
|
|
|
graphql_name 'Foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:resolver) do
|
|
|
|
conn = object.connection_type
|
|
|
|
|
|
|
|
Class.new(described_class) do
|
|
|
|
type conn, null: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(object) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a list type' do
|
|
|
|
let(:resolver) do
|
|
|
|
Class.new(described_class) do
|
2021-07-22 08:10:04 -04:00
|
|
|
type [::GraphQL::Types::String], null: true
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
it { is_expected.to eq(::GraphQL::Types::String) }
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a scalar type' do
|
|
|
|
let(:resolver) do
|
|
|
|
Class.new(described_class) do
|
2021-07-22 08:10:04 -04:00
|
|
|
type ::GraphQL::Types::Boolean, null: true
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
it { is_expected.to eq(::GraphQL::Types::Boolean) }
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-12 08:51:10 -05:00
|
|
|
describe '.single' do
|
|
|
|
it 'returns a subclass from the resolver' do
|
|
|
|
expect(resolver.single.superclass).to eq(resolver)
|
|
|
|
end
|
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
it 'has the correct (singular) type' do
|
2021-07-22 08:10:04 -04:00
|
|
|
expect(resolver.single.type).to eq(::GraphQL::Types::Int)
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
|
2019-02-12 08:51:10 -05:00
|
|
|
it 'returns the same subclass every time' do
|
|
|
|
expect(resolver.single.object_id).to eq(resolver.single.object_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a resolver that gives the first result from the original resolver' do
|
|
|
|
result = resolve(resolver.single, args: { test: 1 })
|
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
expect(result).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.when_single' do
|
|
|
|
let(:resolver) do
|
|
|
|
Class.new(described_class) do
|
2021-07-22 08:10:04 -04:00
|
|
|
type [::GraphQL::Types::Int], null: true
|
2020-11-09 19:08:52 -05:00
|
|
|
|
|
|
|
when_single do
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :foo, ::GraphQL::Types::Int, required: true
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(foo: 1)
|
|
|
|
[foo * foo] # rubocop: disable Lint/BinaryOperatorWithIdenticalOperands
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not apply the block to the resolver' do
|
|
|
|
expect(resolver.field_options).to include(
|
|
|
|
arguments: be_empty
|
|
|
|
)
|
|
|
|
result = resolve(resolver)
|
|
|
|
|
|
|
|
expect(result).to eq([1])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'applies the block to the single version of the resolver' do
|
|
|
|
expect(resolver.single.field_options).to include(
|
|
|
|
arguments: match('foo' => an_instance_of(::Types::BaseArgument))
|
|
|
|
)
|
|
|
|
result = resolve(resolver.single, args: { foo: 7 })
|
|
|
|
|
|
|
|
expect(result).to eq(49)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'multiple when_single blocks' do
|
|
|
|
let(:resolver) do
|
|
|
|
Class.new(described_class) do
|
2021-07-22 08:10:04 -04:00
|
|
|
type [::GraphQL::Types::Int], null: true
|
2020-11-09 19:08:52 -05:00
|
|
|
|
|
|
|
when_single do
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :foo, ::GraphQL::Types::Int, required: true
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
when_single do
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :bar, ::GraphQL::Types::Int, required: true
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(foo: 1, bar: 2)
|
|
|
|
[foo * bar]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'applies both blocks to the single version of the resolver' do
|
|
|
|
expect(resolver.single.field_options).to include(
|
|
|
|
arguments: match('foo' => ::Types::BaseArgument, 'bar' => ::Types::BaseArgument)
|
|
|
|
)
|
|
|
|
result = resolve(resolver.single, args: { foo: 7, bar: 5 })
|
|
|
|
|
|
|
|
expect(result).to eq(35)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'inheritance' do
|
|
|
|
let(:subclass) do
|
|
|
|
Class.new(resolver) do
|
|
|
|
when_single do
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :inc, ::GraphQL::Types::Int, required: true
|
2020-11-09 19:08:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(foo:, inc:)
|
|
|
|
super(foo: foo + inc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'applies both blocks to the single version of the resolver' do
|
|
|
|
expect(resolver.single.field_options).to include(
|
|
|
|
arguments: match('foo' => ::Types::BaseArgument)
|
|
|
|
)
|
|
|
|
expect(subclass.single.field_options).to include(
|
|
|
|
arguments: match('foo' => ::Types::BaseArgument, 'inc' => ::Types::BaseArgument)
|
|
|
|
)
|
|
|
|
result = resolve(subclass.single, args: { foo: 7, inc: 1 })
|
|
|
|
|
|
|
|
expect(result).to eq(64)
|
|
|
|
end
|
2019-02-12 08:51:10 -05:00
|
|
|
end
|
|
|
|
end
|
2019-05-06 17:24:19 -04:00
|
|
|
|
2020-05-21 14:08:27 -04:00
|
|
|
context 'when the resolver returns early' do
|
|
|
|
let(:resolver) do
|
|
|
|
Class.new(described_class) do
|
2021-07-22 08:10:04 -04:00
|
|
|
type [::GraphQL::Types::String], null: true
|
2020-11-09 19:08:52 -05:00
|
|
|
|
2020-05-21 14:08:27 -04:00
|
|
|
def ready?(**args)
|
2020-11-09 19:08:52 -05:00
|
|
|
[false, %w[early return]]
|
2020-05-21 14:08:27 -04:00
|
|
|
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
|
|
|
|
|
2019-11-14 07:06:30 -05:00
|
|
|
describe '.last' do
|
|
|
|
it 'returns a subclass from the resolver' do
|
2020-05-21 14:08:27 -04:00
|
|
|
expect(last_resolver.last.ancestors).to include(last_resolver)
|
2019-11-14 07:06:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the same subclass every time' do
|
|
|
|
expect(last_resolver.last.object_id).to eq(last_resolver.last.object_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a resolver that gives the last result from the original resolver' do
|
|
|
|
result = resolve(last_resolver.last)
|
|
|
|
|
|
|
|
expect(result).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-04 18:52:06 -04:00
|
|
|
context 'when field is a connection' do
|
|
|
|
it 'increases complexity based on arguments' do
|
2021-07-22 08:10:04 -04:00
|
|
|
field = Types::BaseField.new(name: 'test', type: GraphQL::Types::String.connection_type, resolver_class: described_class, null: false, max_page_size: 1)
|
2019-05-06 17:24:19 -04:00
|
|
|
|
2022-03-15 14:08:10 -04:00
|
|
|
expect(field.complexity.call({}, { sort: 'foo' }, 1)).to eq 3
|
|
|
|
expect(field.complexity.call({}, { search: 'foo' }, 1)).to eq 7
|
2019-06-04 18:52:06 -04:00
|
|
|
end
|
2019-05-06 17:24:19 -04:00
|
|
|
|
2019-06-04 18:52:06 -04:00
|
|
|
it 'does not increase complexity when filtering by iids' do
|
2021-07-22 08:10:04 -04:00
|
|
|
field = Types::BaseField.new(name: 'test', type: GraphQL::Types::String.connection_type, resolver_class: described_class, null: false, max_page_size: 100)
|
2019-05-06 17:24:19 -04:00
|
|
|
|
2022-03-15 14:08:10 -04:00
|
|
|
expect(field.complexity.call({}, { sort: 'foo' }, 1)).to eq 6
|
|
|
|
expect(field.complexity.call({}, { sort: 'foo', iid: 1 }, 1)).to eq 3
|
|
|
|
expect(field.complexity.call({}, { sort: 'foo', iids: [1, 2, 3] }, 1)).to eq 3
|
2019-06-04 18:52:06 -04:00
|
|
|
end
|
2019-05-06 17:24:19 -04:00
|
|
|
end
|
2019-12-13 04:08:01 -05:00
|
|
|
|
|
|
|
describe '#object' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'returns object' do
|
|
|
|
expect_next_instance_of(resolver) do |r|
|
|
|
|
expect(r).to receive(:process).with(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
resolve(resolver, obj: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when object is a presenter' do
|
|
|
|
it 'returns presented object' do
|
|
|
|
expect_next_instance_of(resolver) do |r|
|
|
|
|
expect(r).to receive(:process).with(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
resolve(resolver, obj: UserPresenter.new(user))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-26 10:09:30 -05:00
|
|
|
|
|
|
|
describe '#offset_pagination' do
|
|
|
|
let(:instance) { resolver_instance(resolver) }
|
|
|
|
|
2021-01-20 04:10:52 -05:00
|
|
|
it 'is sugar for OffsetPaginatedRelation.new' do
|
|
|
|
expect(instance.offset_pagination(User.none)).to be_a(::Gitlab::Graphql::Pagination::OffsetPaginatedRelation)
|
2020-11-26 10:09:30 -05:00
|
|
|
end
|
|
|
|
end
|
2019-02-12 08:51:10 -05:00
|
|
|
end
|