-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathresolver_error_test.go
508 lines (454 loc) · 16.6 KB
/
resolver_error_test.go
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package resolve
import (
"context"
"io/ioutil"
"testing"
dgoapi "github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/graphql/schema"
"github.com/dgraph-io/dgraph/graphql/test"
"github.com/dgraph-io/dgraph/x"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
// Tests that result completion and GraphQL error propagation are working properly.
// All the tests work on a mocked json response, rather than a running Dgraph.
// It's better to mock the Dgraph client interface in these tests and have cases
// where one can directly see the json response and how it gets modified, than
// to try and orchestrate conditions for all these complicated tests in a live
// Dgraph instance. Done on a real Dgraph, you also can't see the responses
// to see what the test is actually doing.
type executor struct {
resp string
assigned map[string]string
result map[string]interface{}
// these will be returned as extensions by .Query() and .Mutate() respectively
queryExtensions *schema.Extensions
mutateExtensions *schema.Extensions
// start reporting Dgraph fails at this point (0 = never fail, 1 = fail on
// first request, 2 = succeed once and then fail on 2nd request, etc.)
failQuery int
failMutation int
}
type QueryCase struct {
Name string
GQLQuery string
Explanation string
Response string // Dgraph json response
Expected string // Expected data from Resolve()
Errors x.GqlErrorList
}
var testGQLSchema = `
type Author {
id: ID!
name: String!
dob: DateTime
postsRequired: [Post!]!
postsElmntRequired: [Post!]
postsNullable: [Post]
postsNullableListRequired: [Post]!
}
type Post {
id: ID!
title: String!
text: String
author: Author!
}`
func (ex *executor) Query(ctx context.Context, query *gql.GraphQuery) ([]byte,
*schema.Extensions, error) {
ex.failQuery--
if ex.failQuery == 0 {
return nil, nil, schema.GQLWrapf(errors.New("_bad stuff happend_"), "Dgraph query failed")
}
return []byte(ex.resp), ex.queryExtensions, nil
}
func (ex *executor) Mutate(ctx context.Context,
query *gql.GraphQuery,
mutations []*dgoapi.Mutation) (map[string]string, map[string]interface{},
*schema.Extensions, error) {
ex.failMutation--
if ex.failMutation == 0 {
return nil, nil, nil, schema.GQLWrapf(errors.New("_bad stuff happend_"),
"Dgraph mutation failed")
}
return ex.assigned, ex.result, ex.mutateExtensions, nil
}
// Tests in resolver_test.yaml are about what gets into a completed result (addition
// of "null", errors and error propagation). Exact JSON result (e.g. order) doesn't
// matter here - that makes for easier to format and read tests for these many cases.
//
// The []bytes built by Resolve() have some other properties, such as ordering of
// fields, which are tested by TestResponseOrder().
func TestResolver(t *testing.T) {
b, err := ioutil.ReadFile("resolver_error_test.yaml")
require.NoError(t, err, "Unable to read test file")
var tests []QueryCase
err = yaml.Unmarshal(b, &tests)
require.NoError(t, err, "Unable to unmarshal tests to yaml.")
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
for _, tcase := range tests {
t.Run(tcase.Name, func(t *testing.T) {
resp := resolve(gqlSchema, tcase.GQLQuery, tcase.Response)
if diff := cmp.Diff(tcase.Errors, resp.Errors); diff != "" {
t.Errorf("errors mismatch (-want +got):\n%s", diff)
}
require.JSONEq(t, tcase.Expected, resp.Data.String(), tcase.Explanation)
})
}
}
// Ordering of results and inserted null values matters in GraphQL:
// https://graphql.github.io/graphql-spec/June2018/#sec-Serialized-Map-Ordering
func TestResponseOrder(t *testing.T) {
query := `query {
getAuthor(id: "0x1") {
name
dob
postsNullable {
title
text
}
}
}`
tests := []QueryCase{
{Name: "Response is in same order as GQL query",
GQLQuery: query,
Response: `{ "getAuthor": [ { "name": "A.N. Author", "dob": "2000-01-01", ` +
`"postsNullable": [ ` +
`{ "title": "A Title", "text": "Some Text" }, ` +
`{ "title": "Another Title", "text": "More Text" } ] } ] }`,
Expected: `{"getAuthor": {"name": "A.N. Author", "dob": "2000-01-01", ` +
`"postsNullable": [` +
`{"title": "A Title", "text": "Some Text"}, ` +
`{"title": "Another Title", "text": "More Text"}]}}`},
{Name: "Response is in same order as GQL query no matter Dgraph order",
GQLQuery: query,
Response: `{ "getAuthor": [ { "dob": "2000-01-01", "name": "A.N. Author", ` +
`"postsNullable": [ ` +
`{ "text": "Some Text", "title": "A Title" }, ` +
`{ "title": "Another Title", "text": "More Text" } ] } ] }`,
Expected: `{"getAuthor": {"name": "A.N. Author", "dob": "2000-01-01", ` +
`"postsNullable": [` +
`{"title": "A Title", "text": "Some Text"}, ` +
`{"title": "Another Title", "text": "More Text"}]}}`},
{Name: "Inserted null is in GQL query order",
GQLQuery: query,
Response: `{ "getAuthor": [ { "name": "A.N. Author", ` +
`"postsNullable": [ ` +
`{ "title": "A Title" }, ` +
`{ "title": "Another Title", "text": "More Text" } ] } ] }`,
Expected: `{"getAuthor": {"name": "A.N. Author", "dob": null, ` +
`"postsNullable": [` +
`{"title": "A Title", "text": null}, ` +
`{"title": "Another Title", "text": "More Text"}]}}`},
}
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
resp := resolve(gqlSchema, test.GQLQuery, test.Response)
require.Nil(t, resp.Errors)
require.Equal(t, test.Expected, resp.Data.String())
})
}
}
// For add and update mutations, we don't need to re-test all the cases from the
// query tests. So just test enough to demonstrate that we'll catch it if we were
// to delete the call to completeDgraphResult before adding to the response.
func TestAddMutationUsesErrorPropagation(t *testing.T) {
mutation := `mutation {
addPost(input: [{title: "A Post", text: "Some text", author: {id: "0x1"}}]) {
post {
title
text
author {
name
dob
}
}
}
}`
tests := map[string]struct {
explanation string
mutResponse map[string]string
mutQryResp map[string]interface{}
queryResponse string
expected string
errors x.GqlErrorList
}{
"Add mutation adds missing nullable fields": {
explanation: "Field 'dob' is nullable, so null should be inserted " +
"if the mutation's query doesn't return a value.",
mutResponse: map[string]string{"Post1": "0x2"},
mutQryResp: map[string]interface{}{
"Author2": []interface{}{map[string]string{"uid": "0x1"}}},
queryResponse: `{ "post" : [
{ "title": "A Post",
"text": "Some text",
"author": { "name": "A.N. Author" } } ] }`,
expected: `{ "addPost": { "post" :
[{ "title": "A Post",
"text": "Some text",
"author": { "name": "A.N. Author", "dob": null } }] } }`,
},
"Add mutation triggers GraphQL error propagation": {
explanation: "An Author's name is non-nullable, so if that's missing, " +
"the author is squashed to null, but that's also non-nullable, so the " +
"propagates to the query root.",
mutResponse: map[string]string{"Post1": "0x2"},
mutQryResp: map[string]interface{}{
"Author2": []interface{}{map[string]string{"uid": "0x1"}}},
queryResponse: `{ "post" : [
{ "title": "A Post",
"text": "Some text",
"author": { "dob": "2000-01-01" } } ] }`,
expected: `{ "addPost": { "post" : [null] } }`,
errors: x.GqlErrorList{&x.GqlError{
Message: `Non-nullable field 'name' (type String!) ` +
`was not present in result from Dgraph. GraphQL error propagation triggered.`,
Locations: []x.Location{{Column: 6, Line: 7}},
Path: []interface{}{"addPost", "post", 0, "author", "name"}}},
},
}
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
for name, tcase := range tests {
t.Run(name, func(t *testing.T) {
resp := resolveWithClient(gqlSchema, mutation, nil,
&executor{
resp: tcase.queryResponse,
assigned: tcase.mutResponse,
result: tcase.mutQryResp,
})
test.RequireJSONEq(t, tcase.errors, resp.Errors)
require.JSONEq(t, tcase.expected, resp.Data.String(), tcase.explanation)
})
}
}
func TestUpdateMutationUsesErrorPropagation(t *testing.T) {
mutation := `mutation {
updatePost(input: { filter: { id: ["0x1"] }, set: { text: "Some more text" } }) {
post {
title
text
author {
name
dob
}
}
}
}`
// There's no need to have mocks for the mutation part here because with nil results all the
// rewriting and rewriting from results will silently succeed. All we care about the is the
// result from the query that follows the mutation. In that add case we have to satisfy
// the type checking, but that's not required here.
tests := map[string]struct {
explanation string
mutResponse map[string]string
queryResponse string
expected string
errors x.GqlErrorList
}{
"Update Mutation adds missing nullable fields": {
explanation: "Field 'dob' is nullable, so null should be inserted " +
"if the mutation's query doesn't return a value.",
queryResponse: `{ "post" : [
{ "title": "A Post",
"text": "Some text",
"author": { "name": "A.N. Author" } } ] }`,
expected: `{ "updatePost": { "post" :
[{ "title": "A Post",
"text": "Some text",
"author": { "name": "A.N. Author", "dob": null } }] } }`,
},
"Update Mutation triggers GraphQL error propagation": {
explanation: "An Author's name is non-nullable, so if that's missing, " +
"the author is squashed to null, but that's also non-nullable, so the error " +
"propagates to the query root.",
queryResponse: `{ "post" : [ {
"title": "A Post",
"text": "Some text",
"author": { "dob": "2000-01-01" } } ] }`,
expected: `{ "updatePost": { "post" : [null] } }`,
errors: x.GqlErrorList{&x.GqlError{
Message: `Non-nullable field 'name' (type String!) ` +
`was not present in result from Dgraph. GraphQL error propagation triggered.`,
Locations: []x.Location{{Column: 6, Line: 7}},
Path: []interface{}{"updatePost", "post", 0, "author", "name"}}},
},
}
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
for name, tcase := range tests {
t.Run(name, func(t *testing.T) {
resp := resolveWithClient(gqlSchema, mutation, nil,
&executor{resp: tcase.queryResponse, assigned: tcase.mutResponse})
test.RequireJSONEq(t, tcase.errors, resp.Errors)
require.JSONEq(t, tcase.expected, resp.Data.String(), tcase.explanation)
})
}
}
// TestManyMutationsWithError : Multiple mutations run serially (queries would
// run in parallel) and, in GraphQL, if an error is encountered in a request with
// multiple mutations, the mutations following the error are not run. The mutations
// that have succeeded are permanent - i.e. not rolled back.
//
// There's no real way to test this E2E against a live instance because the only
// real fails during a mutation are either failure to communicate with Dgraph, or
// a bug that causes a query rewriting that Dgraph rejects. There are some other
// cases: e.g. a delete that doesn't end up deleting anything (but we interpret
// that as not an error, it just deleted 0 things), and a mutation with some error
// in the input data/query (but that gets caught by validation before any mutations
// are executed).
//
// So this mocks a failing mutation and tests that we behave correctly in the case
// of multiple mutations.
func TestManyMutationsWithError(t *testing.T) {
// add1 - should succeed
// add2 - should fail
// add3 - is never executed
multiMutation := `mutation multipleMutations($id: ID!) {
add1: addPost(input: [{title: "A Post", text: "Some text", author: {id: "0x1"}}]) {
post { title }
}
add2: addPost(input: [{title: "A Post", text: "Some text", author: {id: $id}}]) {
post { title }
}
add3: addPost(input: [{title: "A Post", text: "Some text", author: {id: "0x1"}}]) {
post { title }
}
}`
tests := map[string]struct {
explanation string
idValue string
mutResponse map[string]string
mutQryResp map[string]interface{}
queryResponse string
expected string
errors x.GqlErrorList
}{
"Dgraph fail": {
explanation: "a Dgraph, network or error in rewritten query failed the mutation",
idValue: "0x1",
mutResponse: map[string]string{"Post1": "0x2"},
mutQryResp: map[string]interface{}{
"Author2": []interface{}{map[string]string{"uid": "0x1"}}},
queryResponse: `{ "post" : [{ "title": "A Post" } ] }`,
expected: `{
"add1": { "post": [{ "title": "A Post" }] },
"add2" : null
}`,
errors: x.GqlErrorList{
&x.GqlError{Message: `mutation addPost failed because ` +
`Dgraph mutation failed because _bad stuff happend_`,
Locations: []x.Location{{Line: 6, Column: 4}}},
&x.GqlError{Message: `Mutation add3 was not executed because of ` +
`a previous error.`,
Locations: []x.Location{{Line: 10, Column: 4}}}},
},
"Rewriting error": {
explanation: "The reference ID is not a uint64, so can't be converted to a uid",
idValue: "hi",
mutResponse: map[string]string{"Post1": "0x2"},
mutQryResp: map[string]interface{}{
"Author2": []interface{}{map[string]string{"uid": "0x1"}}},
queryResponse: `{ "post" : [{ "title": "A Post" } ] }`,
expected: `{
"add1": { "post": [{ "title": "A Post" }] },
"add2" : null
}`,
errors: x.GqlErrorList{
&x.GqlError{Message: `couldn't rewrite mutation addPost because ` +
`failed to rewrite mutation payload because ` +
`ID argument (hi) was not able to be parsed`},
&x.GqlError{Message: `Mutation add3 was not executed because of ` +
`a previous error.`,
Locations: []x.Location{{Line: 10, Column: 4}}}},
},
}
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
for name, tcase := range tests {
t.Run(name, func(t *testing.T) {
resp := resolveWithClient(
gqlSchema,
multiMutation,
map[string]interface{}{"id": tcase.idValue},
&executor{
resp: tcase.queryResponse,
assigned: tcase.mutResponse,
failMutation: 2})
if diff := cmp.Diff(tcase.errors, resp.Errors); diff != "" {
t.Errorf("errors mismatch (-want +got):\n%s", diff)
}
require.JSONEq(t, tcase.expected, resp.Data.String())
})
}
}
func TestQueriesPropagateExtensions(t *testing.T) {
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
query := `
query {
getAuthor(id: "0x1") {
name
}
}`
resp := resolveWithClient(gqlSchema, query, nil,
&executor{
queryExtensions: &schema.Extensions{TouchedUids: 2},
mutateExtensions: &schema.Extensions{TouchedUids: 5},
})
expectedExtensions := &schema.Extensions{TouchedUids: 2}
require.NotNil(t, resp)
require.Equal(t, expectedExtensions, resp.Extensions)
}
func TestMutationsPropagateExtensions(t *testing.T) {
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
mutation := `mutation {
addPost(input: [{title: "A Post", author: {id: "0x1"}}]) {
post {
title
}
}
}`
resp := resolveWithClient(gqlSchema, mutation, nil,
&executor{
queryExtensions: &schema.Extensions{TouchedUids: 2},
mutateExtensions: &schema.Extensions{TouchedUids: 5},
})
// as both .Mutate() and .Query() should get called, so we should get their merged result
expectedExtensions := &schema.Extensions{TouchedUids: 7}
require.NotNil(t, resp)
require.Equal(t, expectedExtensions, resp.Extensions)
}
func resolve(gqlSchema schema.Schema, gqlQuery string, dgResponse string) *schema.Response {
return resolveWithClient(gqlSchema, gqlQuery, nil, &executor{resp: dgResponse})
}
func resolveWithClient(
gqlSchema schema.Schema,
gqlQuery string,
vars map[string]interface{},
ex *executor) *schema.Response {
resolver := New(
gqlSchema,
NewResolverFactory(nil, nil).WithConventionResolvers(gqlSchema, &ResolverFns{
Qrw: NewQueryRewriter(),
Arw: NewAddRewriter,
Urw: NewUpdateRewriter,
Qe: ex,
Me: ex,
}))
return resolver.Resolve(context.Background(), &schema.Request{Query: gqlQuery, Variables: vars})
}