-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathgraphql_test.rb
347 lines (328 loc) · 8.68 KB
/
graphql_test.rb
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
require_relative 'test_helper'
class GraphQL::GraphQLTest < Minitest::Test
attr_reader :queries
def setup
@queries = []
QueryNotifier.subscriber = ->(query) { @queries << query }
end
def teardown
QueryNotifier.subscriber = nil
end
def schema_execute(query_string, **kwargs)
::Schema.execute(query_string, **kwargs)
end
def test_no_queries
query_string = '{ constant }'
result = schema_execute(query_string)
expected = {
"data" => {
"constant" => "constant value"
}
}
assert_equal expected, result
assert_equal [], queries
end
def test_single_query
query_string = <<-GRAPHQL
{
product(id: "1") {
id
title
}
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => {
"product" => {
"id" => "1",
"title" => "Shirt",
}
}
}
assert_equal expected, result
assert_equal ["Product/1"], queries
end
def test_batched_find_by_id
query_string = <<-GRAPHQL
{
product1: product(id: "1") { id, title }
product2: product(id: "2") { id, title }
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => {
"product1" => { "id" => "1", "title" => "Shirt" },
"product2" => { "id" => "2", "title" => "Pants" },
}
}
assert_equal expected, result
assert_equal ["Product/1,2"], queries
end
def test_record_missing
query_string = <<-GRAPHQL
{
product(id: "123") {
id
title
}
}
GRAPHQL
result = schema_execute(query_string)
expected = { "data" => { "product" => nil } }
assert_equal expected, result
assert_equal ["Product/123"], queries
end
def test_non_null_field_that_raises_on_nullable_parent
query_string = <<-GRAPHQL
{
product(id: "1") {
id
nonNullButRaises
}
}
GRAPHQL
result = schema_execute(query_string)
expected = { 'data' => { 'product' => nil }, 'errors' => [{ 'message' => 'Error', 'locations' => [{ 'line' => 4, 'column' => 11 }], 'path' => ['product', 'nonNullButRaises'] }] }
assert_equal expected, result
end
def test_non_null_field_that_raises_on_query_root
query_string = <<-GRAPHQL
{
nonNullButRaises {
id
}
}
GRAPHQL
result = schema_execute(query_string)
expected = { 'data' => nil, 'errors' => [{ 'message' => 'Error', 'locations' => [{ 'line' => 2, 'column' => 9 }], 'path' => ['nonNullButRaises'] }] }
assert_equal expected, result
end
def test_non_null_field_promise_raises
result = schema_execute('{ nonNullButPromiseRaises }')
expected = { 'data' => nil, 'errors' => [{ 'message' => 'Error', 'locations' => [{ 'line' => 1, 'column' => 3 }], 'path' => ['nonNullButPromiseRaises'] }] }
assert_equal expected, result
end
def test_batched_association_preload
query_string = <<-GRAPHQL
{
products(first: 2) {
id
title
variants {
id
title
}
}
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => {
"products" => [
{
"id" => "1",
"title" => "Shirt",
"variants" => [
{ "id" => "1", "title" => "Red" },
{ "id" => "2", "title" => "Blue" },
],
},
{
"id" => "2",
"title" => "Pants",
"variants" => [
{ "id" => "4", "title" => "Small" },
{ "id" => "5", "title" => "Medium" },
{ "id" => "6", "title" => "Large" },
],
}
]
}
}
assert_equal expected, result
assert_equal ["Product?limit=2", "Product/1,2/variants"], queries
end
def test_query_group_with_single_query
query_string = <<-GRAPHQL
{
products(first: 2) {
id
title
variants_count
variants {
id
title
}
}
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => {
"products" => [
{
"id" => "1",
"title" => "Shirt",
"variants_count" => 2,
"variants" => [
{ "id" => "1", "title" => "Red" },
{ "id" => "2", "title" => "Blue" },
],
},
{
"id" => "2",
"title" => "Pants",
"variants_count" => 3,
"variants" => [
{ "id" => "4", "title" => "Small" },
{ "id" => "5", "title" => "Medium" },
{ "id" => "6", "title" => "Large" },
],
}
]
}
}
assert_equal expected, result
assert_equal ["Product?limit=2", "Product/1,2/variants"], queries
end
def test_sub_queries
query_string = <<-GRAPHQL
{
product_variants_count(id: "2")
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => {
"product_variants_count" => 3
}
}
assert_equal expected, result
assert_equal ["Product/2", "Product/2/variants"], queries
end
def test_query_group_with_sub_queries
query_string = <<-GRAPHQL
{
product(id: "1") {
images { id, filename }
}
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => {
"product" => {
"images" => [
{ "id" => "1", "filename" => "shirt.jpg" },
{ "id" => "4", "filename" => "red-shirt.jpg" },
{ "id" => "5", "filename" => "blue-shirt.jpg" },
]
}
}
}
assert_equal expected, result
assert_equal ["Product/1", "Image/1", "Product/1/variants", "ProductVariant/1,2/images"], queries
end
def test_load_list_of_objects_with_loaded_field
query_string = <<-GRAPHQL
{
products(first: 2) {
id
variants {
id
image_ids
}
}
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => {
"products" => [
{
"id" => "1",
"variants" => [
{ "id" => "1", "image_ids" => ["4"] },
{ "id" => "2", "image_ids" => ["5"] },
],
},
{
"id" => "2",
"variants" => [
{ "id" => "4", "image_ids" => [] },
{ "id" => "5", "image_ids" => [] },
{ "id" => "6", "image_ids" => [] },
],
}
]
}
}
assert_equal expected, result
assert_equal ["Product?limit=2", "Product/1,2/variants", "ProductVariant/1,2,4,5,6/images"], queries
end
def test_load_error
query_string = <<-GRAPHQL
{
constant
load_execution_error
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => { "constant"=>"constant value", "load_execution_error" => nil },
"errors" => [{ "message" => "test error message", "locations"=>[{"line"=>3, "column"=>9}], "path" => ["load_execution_error"] }],
}
assert_equal expected, result
end
def test_mutation_execution
query_string = <<-GRAPHQL
mutation {
count1: counter_loader
incr1: increment_counter { value, load_value }
count2: counter_loader
incr2: increment_counter { value, load_value }
}
GRAPHQL
result = schema_execute(query_string, context: { counter: [0] })
expected = {
"data" => {
"count1" => 0,
"incr1" => { "value" => 1, "load_value" => 1 },
"count2" => 1,
"incr2" => { "value" => 2, "load_value" => 2 },
}
}
assert_equal expected, result
end
def test_mutation_batch_subselection_execution
query_string = <<-GRAPHQL
mutation {
mutation1: no_op {
product1: product(id: "1") { id, title }
product2: product(id: "2") { id, title }
}
mutation2: no_op {
product1: product(id: "2") { id, title }
product2: product(id: "3") { id, title }
}
}
GRAPHQL
result = schema_execute(query_string)
expected = {
"data" => {
"mutation1" => {
"product1" => { "id" => "1", "title" => "Shirt" },
"product2" => { "id" => "2", "title" => "Pants" },
},
"mutation2" => {
"product1" => { "id" => "2", "title" => "Pants" },
"product2" => { "id" => "3", "title" => "Sweater" },
}
}
}
assert_equal expected, result
assert_equal ["Product/1,2", "Product/3"], queries
end
end