forked from rmosolgo/graphql-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution_error_spec.rb
329 lines (319 loc) · 10.1 KB
/
execution_error_spec.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
# frozen_string_literal: true
require "spec_helper"
describe GraphQL::ExecutionError do
let(:result) { Dummy::Schema.execute(query_string) }
if TESTING_RESCUE_FROM
describe "when returned from a field" do
let(:query_string) {%|
{
cheese(id: 1) {
id
error1: similarCheese(source: [YAK]) {
... similarCheeseFields
}
error2: similarCheese(source: [YAK]) {
... similarCheeseFields
}
nonError: similarCheese(source: [SHEEP]) {
... similarCheeseFields
}
flavor
}
allDairy {
... on Cheese {
flavor
}
... on Milk {
source
executionError
}
}
dairyErrors: allDairy(executionErrorAtIndex: 1) {
__typename
}
dairy {
milks {
source
executionError
allDairy {
__typename
... on Milk {
origin
executionError
}
}
}
}
executionError
valueWithExecutionError
}
fragment similarCheeseFields on Cheese {
id, flavor
}
|}
it "the error is inserted into the errors key and the rest of the query is fulfilled" do
expected_result = {
"data"=>{
"cheese"=>{
"id" => 1,
"error1"=> nil,
"error2"=> nil,
"nonError"=> {
"id" => 3,
"flavor" => "Manchego",
},
"flavor" => "Brie",
},
"allDairy" => [
{ "flavor" => "Brie" },
{ "flavor" => "Gouda" },
{ "flavor" => "Manchego" },
{ "source" => "COW", "executionError" => nil }
],
"dairyErrors" => [
{ "__typename" => "Cheese" },
nil,
{ "__typename" => "Cheese" },
{ "__typename" => "Milk" }
],
"dairy" => {
"milks" => [
{
"source" => "COW",
"executionError" => nil,
"allDairy" => [
{ "__typename" => "Cheese" },
{ "__typename" => "Cheese" },
{ "__typename" => "Cheese" },
{ "__typename" => "Milk", "origin" => "Antiquity", "executionError" => nil }
]
}
]
},
"executionError" => nil,
"valueWithExecutionError" => 0
},
"errors"=>[
{
"message"=>"No cheeses are made from Yak milk!",
"locations"=>[{"line"=>5, "column"=>9}],
"path"=>["cheese", "error1"]
},
{
"message"=>"No cheeses are made from Yak milk!",
"locations"=>[{"line"=>8, "column"=>9}],
"path"=>["cheese", "error2"]
},
{
"message"=>"There was an execution error",
"locations"=>[{"line"=>22, "column"=>11}],
"path"=>["allDairy", 3, "executionError"]
},
{
"message"=>"missing dairy",
"locations"=>[{"line"=>25, "column"=>7}],
"path"=>["dairyErrors", 1]
},
{
"message"=>"There was an execution error",
"locations"=>[{"line"=>31, "column"=>11}],
"path"=>["dairy", "milks", 0, "executionError"]
},
{
"message"=>"There was an execution error",
"locations"=>[{"line"=>36, "column"=>15}],
"path"=>["dairy", "milks", 0, "allDairy", 3, "executionError"]
},
{
"message"=>"There was an execution error",
"locations"=>[{"line"=>41, "column"=>7}],
"path"=>["executionError"]
},
{
"message"=>"Could not fetch latest value",
"locations"=>[{"line"=>42, "column"=>7}],
"path"=>["valueWithExecutionError"]
},
]
}
assert_equal(expected_result, result.to_h)
end
end
end
describe "named query when returned from a field" do
let(:query_string) {%|
query MilkQuery {
dairy {
milks {
source
executionError
allDairy {
__typename
... on Milk {
origin
executionError
}
}
}
}
}
|}
it "the error is inserted into the errors key and the rest of the query is fulfilled" do
expected_result = {
"data"=>{
"dairy" => {
"milks" => [
{
"source" => "COW",
"executionError" => nil,
"allDairy" => [
{ "__typename" => "Cheese" },
{ "__typename" => "Cheese" },
{ "__typename" => "Cheese" },
{ "__typename" => "Milk", "origin" => "Antiquity", "executionError" => nil }
]
}
]
}
},
"errors"=>[
{
"message"=>"There was an execution error",
"locations"=>[{"line"=>6, "column"=>11}],
"path"=>["dairy", "milks", 0, "executionError"]
},
{
"message"=>"There was an execution error",
"locations"=>[{"line"=>11, "column"=>15}],
"path"=>["dairy", "milks", 0, "allDairy", 3, "executionError"]
}
]
}
assert_equal(expected_result, result)
end
end
describe "fragment query when returned from a field" do
let(:query_string) {%|
query MilkQuery {
dairy {
...Dairy
}
}
fragment Dairy on Dairy {
milks {
source
executionError
allDairy {
__typename
...Milk
}
}
}
fragment Milk on Milk {
origin
executionError
}
|}
it "the error is inserted into the errors key and the rest of the query is fulfilled" do
expected_result = {
"data"=>{
"dairy" => {
"milks" => [
{
"source" => "COW",
"executionError" => nil,
"allDairy" => [
{ "__typename" => "Cheese" },
{ "__typename" => "Cheese" },
{ "__typename" => "Cheese" },
{ "__typename" => "Milk", "origin" => "Antiquity", "executionError" => nil }
]
}
]
}
},
"errors"=>[
{
"message"=>"There was an execution error",
"locations"=>[{"line"=>11, "column"=>9}],
"path"=>["dairy", "milks", 0, "executionError"]
},
{
"message"=>"There was an execution error",
"locations"=>[{"line"=>21, "column"=>7}],
"path"=>["dairy", "milks", 0, "allDairy", 3, "executionError"]
}
]
}
assert_equal(expected_result, result)
end
end
describe "options in ExecutionError" do
let(:query_string) {%|
{
executionErrorWithOptions
}
|}
it "the error is inserted into the errors key and the rest of the query is fulfilled" do
expected_result = {
"data"=>{"executionErrorWithOptions"=>nil},
"errors"=>
[{"message"=>"Permission Denied!",
"locations"=>[{"line"=>3, "column"=>7}],
"path"=>["executionErrorWithOptions"],
"code"=>"permission_denied"}]
}
assert_equal(expected_result, result)
end
end
describe "extensions in ExecutionError" do
let(:query_string) {%|
{
executionErrorWithExtensions
}
|}
it "the error is inserted into the errors key with custom data set in `extensions`" do
expected_result = {
"data"=>{"executionErrorWithExtensions"=>nil},
"errors"=>
[{"message"=>"Permission Denied!",
"locations"=>[{"line"=>3, "column"=>7}],
"path"=>["executionErrorWithExtensions"],
"extensions"=>{"code"=>"permission_denied"}}]
}
assert_equal(expected_result, result)
end
end
describe "more than one ExecutionError" do
let(:query_string) { %|{ multipleErrorsOnNonNullableField} |}
it "the errors are inserted into the errors key and the data is nil even for a NonNullable field " do
expected_result = {
"data"=>nil,
"errors"=>
[{"message"=>"This is an error message for some error.",
"locations"=>[{"line"=>1, "column"=>3}],
"path"=>["multipleErrorsOnNonNullableField"]},
{"message"=>"This is another error message for a different error.",
"locations"=>[{"line"=>1, "column"=>3}],
"path"=>["multipleErrorsOnNonNullableField"]}],
}
assert_equal(expected_result, result)
end
describe "more than one ExecutionError on a field defined to return a list" do
let(:query_string) { %|{ multipleErrorsOnNonNullableListField} |}
it "the errors are inserted into the errors key and the data is nil even for a NonNullable field " do
expected_result = {
"data"=>nil,
"errors"=>
[{"message"=>"This is the first error message for a field defined to return a list of types.",
"locations"=>[{"line"=>1, "column"=>3}],
"path"=>["multipleErrorsOnNonNullableListField", 0]},
{"message"=>"This is the second error message for a field defined to return a list of types.",
"locations"=>[{"line"=>1, "column"=>3}],
"path"=>["multipleErrorsOnNonNullableListField", 1]}],
}
assert_equal(expected_result, result)
end
end
end
end