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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GraphqlHelpers do
include GraphqlHelpers
# Normalize irrelevant whitespace to make comparison easier
def norm(query)
query.tr("\n", ' ').gsub(/\s+/, ' ').strip
end
describe 'graphql_dig_at' do
it 'transforms symbol keys to graphql field names' do
data = { 'camelCased' => 'names' }
expect(graphql_dig_at(data, :camel_cased)).to eq('names')
end
it 'supports integer indexing' do
data = { 'array' => [:boom, { 'id' => :hooray! }, :boom] }
expect(graphql_dig_at(data, :array, 1, :id)).to eq(:hooray!)
end
it 'gracefully degrades to nil' do
data = { 'project' => { 'mergeRequest' => nil } }
expect(graphql_dig_at(data, :project, :merge_request, :id)).to be_nil
end
it 'supports implicitly flat-mapping traversals' do
data = {
'foo' => {
'nodes' => [
{ 'bar' => { 'nodes' => [{ 'id' => 1 }, { 'id' => 2 }] } },
{ 'bar' => { 'nodes' => [{ 'id' => 3 }, { 'id' => 4 }] } },
{ 'bar' => nil }
]
},
'irrelevant' => 'the field is a red-herring'
}
expect(graphql_dig_at(data, :foo, :nodes, :bar, :nodes, :id)).to eq([1, 2, 3, 4])
end
end
describe 'var' do
it 'allocates a fresh name for each var' do
a = var('Int')
b = var('Int')
expect(a.name).not_to eq(b.name)
end
it 'can be used to construct correct signatures' do
a = var('Int')
b = var('String!')
q = with_signature([a, b], '{ foo bar }')
expect(q).to eq("query(#{a.to_graphql_value}: Int, #{b.to_graphql_value}: String!) { foo bar }")
end
it 'can be used to pass arguments to fields' do
a = var('ID!')
q = graphql_query_for(:project, { full_path: a }, :id)
expect(norm(q)).to eq("{ project(fullPath: #{a.to_graphql_value}){ id } }")
end
it 'can associate values with variables' do
a = var('Int')
expect(a.with(3).to_h).to eq(a.name => 3)
end
it 'does not mutate the variable when providing a value' do
a = var('Int')
three = a.with(3)
expect(three.value).to eq(3)
expect(a.value).to be_nil
end
it 'can associate many values with variables' do
a = var('Int').with(3)
b = var('String').with('foo')
expect(serialize_variables([a, b])).to eq({ a.name => 3, b.name => 'foo' }.to_json)
end
end
describe '.query_nodes' do
it 'can produce a basic connection selection' do
selection = query_nodes(:users)
expected = query_graphql_path([:users, :nodes], all_graphql_fields_for('User', max_depth: 1))
expect(selection).to eq(expected)
end
it 'allows greater depth' do
selection = query_nodes(:users, max_depth: 2)
expected = query_graphql_path([:users, :nodes], all_graphql_fields_for('User', max_depth: 2))
expect(selection).to eq(expected)
end
it 'accepts fields' do
selection = query_nodes(:users, :id)
expected = query_graphql_path([:users, :nodes], :id)
expect(selection).to eq(expected)
end
it 'accepts arguments' do
args = { username: 'foo' }
selection = query_nodes(:users, args: args)
expected = query_graphql_path([[:users, args], :nodes], all_graphql_fields_for('User', max_depth: 1))
expect(selection).to eq(expected)
end
it 'accepts arguments and fields' do
selection = query_nodes(:users, :id, args: { username: 'foo' })
expected = query_graphql_path([[:users, { username: 'foo' }], :nodes], :id)
expect(selection).to eq(expected)
end
it 'accepts explicit type name' do
selection = query_nodes(:members, of: 'User')
expected = query_graphql_path([:members, :nodes], all_graphql_fields_for('User', max_depth: 1))
expect(selection).to eq(expected)
end
it 'can optionally provide pagination info' do
selection = query_nodes(:users, include_pagination_info: true)
expected = query_graphql_path([:users, "#{page_info_selection} nodes"], all_graphql_fields_for('User', max_depth: 1))
expect(selection).to eq(expected)
end
end
describe '.query_graphql_path' do
it 'can build nested paths' do
selection = query_graphql_path(%i[foo bar wibble_wobble], :id)
expected = norm(<<-GQL)
foo{
bar{
wibbleWobble{
id
}
}
}
GQL
expect(norm(selection)).to eq(expected)
end
it 'can insert arguments at any point' do
selection = query_graphql_path(
[:foo, [:bar, { quux: true }], [:wibble_wobble, { eccentricity: :HIGH }]],
:id
)
expected = norm(<<-GQL)
foo{
bar(quux: true){
wibbleWobble(eccentricity: HIGH){
id
}
}
}
GQL
expect(norm(selection)).to eq(expected)
end
end
describe '.attributes_to_graphql' do
it 'can serialize hashes to literal arguments' do
x = var('Int')
args = {
an_array: [1, nil, "foo", true, [:foo, :bar]],
a_hash: {
nested: true,
value: "bar"
},
an_int: 42,
a_float: 0.1,
a_string: "wibble",
an_enum: :LOW,
null: nil,
a_bool: false,
a_var: x
}
literal = attributes_to_graphql(args)
expect(norm(literal)).to eq(norm(<<~EXP))
anArray: [1,null,"foo",true,[foo,bar]],
aHash: {nested: true, value: "bar"},
anInt: 42,
aFloat: 0.1,
aString: "wibble",
anEnum: LOW,
aBool: false,
aVar: #{x.to_graphql_value}
EXP
end
end
describe '.all_graphql_fields_for' do
it 'returns a FieldSelection' do
selection = all_graphql_fields_for('User', max_depth: 1)
expect(selection).to be_a(::Graphql::FieldSelection)
end
it 'returns nil if the depth is too shallow' do
selection = all_graphql_fields_for('User', max_depth: 0)
expect(selection).to be_nil
end
it 'can select just the scalar fields' do
selection = all_graphql_fields_for('User', max_depth: 1)
paths = selection.paths.map(&:join)
# A sample, tested using include to save churn as fields are added
expect(paths)
.to include(*%w[avatarUrl email groupCount id location name state username webPath webUrl])
expect(selection.paths).to all(have_attributes(size: 1))
end
it 'selects only as far as 3 levels by default' do
selection = all_graphql_fields_for('User')
expect(selection.paths).to all(have_attributes(size: (be <= 3)))
# Representative sample
expect(selection.paths).to include(
%w[userPermissions createSnippet],
%w[todos nodes id],
%w[starredProjects nodes name],
%w[authoredMergeRequests count],
%w[assignedMergeRequests pageInfo startCursor]
)
end
it 'selects only as far as requested' do
selection = all_graphql_fields_for('User', max_depth: 2)
expect(selection.paths).to all(have_attributes(size: (be <= 2)))
end
it 'omits fields that have required arguments' do
selection = all_graphql_fields_for('DesignCollection', max_depth: 3)
expect(selection.paths).not_to be_empty
expect(selection.paths).not_to include(
%w[designAtVersion id]
)
end
end
describe '.graphql_mutation' do
shared_examples 'correct mutation definition' do
it 'returns correct mutation definition' do
query = <<~MUTATION
mutation($updateAlertStatusInput: UpdateAlertStatusInput!) {
updateAlertStatus(input: $updateAlertStatusInput) {
clientMutationId
}
}
MUTATION
variables = { "updateAlertStatusInput" => { "projectPath" => "test/project" } }
is_expected.to eq(GraphqlHelpers::MutationDefinition.new(query, variables))
end
end
context 'when fields argument is passed' do
subject do
graphql_mutation(:update_alert_status, { project_path: 'test/project' }, 'clientMutationId')
end
it_behaves_like 'correct mutation definition'
end
context 'when block is passed' do
subject do
graphql_mutation(:update_alert_status, { project_path: 'test/project' }) do
'clientMutationId'
end
end
it_behaves_like 'correct mutation definition'
end
context 'when both fields and a block are passed' do
subject do
graphql_mutation(:mutation_name, { variable_name: 'variable/value' }, 'fieldName') do
'fieldName'
end
end
it 'raises an ArgumentError' do
expect { subject }.to raise_error(
ArgumentError,
'Please pass either `fields` parameter or a block to `#graphql_mutation`, but not both.'
)
end
end
end
end
|