1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Resolvers::BaseResolver do
include GraphqlHelpers
let(:resolver) do
Class.new(described_class) do
argument :test, ::GraphQL::Types::Int, required: false
type [::GraphQL::Types::Int], null: true
def resolve(test: 100)
process(object)
[test, test]
end
def process(obj); end
end
end
let(:last_resolver) do
Class.new(described_class) do
type [::GraphQL::Types::Int], null: true
def resolve(**args)
[1, 2]
end
end
end
describe '.singular_type' do
subject { resolver.singular_type }
context 'for a connection of scalars' do
let(:resolver) do
Class.new(described_class) do
type ::GraphQL::Types::Int.connection_type, null: true
end
end
it { is_expected.to eq(::GraphQL::Types::Int) }
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
type [::GraphQL::Types::String], null: true
end
end
it { is_expected.to eq(::GraphQL::Types::String) }
end
context 'for a scalar type' do
let(:resolver) do
Class.new(described_class) do
type ::GraphQL::Types::Boolean, null: true
end
end
it { is_expected.to eq(::GraphQL::Types::Boolean) }
end
end
describe '.single' do
it 'returns a subclass from the resolver' do
expect(resolver.single.superclass).to eq(resolver)
end
it 'has the correct (singular) type' do
expect(resolver.single.type).to eq(::GraphQL::Types::Int)
end
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 })
expect(result).to eq(1)
end
end
describe '.when_single' do
let(:resolver) do
Class.new(described_class) do
type [::GraphQL::Types::Int], null: true
when_single do
argument :foo, ::GraphQL::Types::Int, required: true
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
type [::GraphQL::Types::Int], null: true
when_single do
argument :foo, ::GraphQL::Types::Int, required: true
end
when_single do
argument :bar, ::GraphQL::Types::Int, required: true
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
argument :inc, ::GraphQL::Types::Int, required: true
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
end
end
context 'when the resolver returns early' do
let(:resolver) do
Class.new(described_class) do
type [::GraphQL::Types::String], null: true
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.ancestors).to include(last_resolver)
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
context 'when field is a connection' do
it 'increases complexity based on arguments' do
field = Types::BaseField.new(name: 'test', type: GraphQL::Types::String.connection_type, resolver_class: described_class, null: false, max_page_size: 1)
expect(field.to_graphql.complexity.call({}, { sort: 'foo' }, 1)).to eq 3
expect(field.to_graphql.complexity.call({}, { search: 'foo' }, 1)).to eq 7
end
it 'does not increase complexity when filtering by iids' do
field = Types::BaseField.new(name: 'test', type: GraphQL::Types::String.connection_type, resolver_class: described_class, null: false, max_page_size: 100)
expect(field.to_graphql.complexity.call({}, { sort: 'foo' }, 1)).to eq 6
expect(field.to_graphql.complexity.call({}, { sort: 'foo', iid: 1 }, 1)).to eq 3
expect(field.to_graphql.complexity.call({}, { sort: 'foo', iids: [1, 2, 3] }, 1)).to eq 3
end
end
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
describe '#offset_pagination' do
let(:instance) { resolver_instance(resolver) }
it 'is sugar for OffsetPaginatedRelation.new' do
expect(instance.offset_pagination(User.none)).to be_a(::Gitlab::Graphql::Pagination::OffsetPaginatedRelation)
end
end
end
|