-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathresolver_test.go
413 lines (387 loc) · 17.2 KB
/
resolver_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
/*
* 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 (
"testing"
"github.com/dgraph-io/dgraph/graphql/test"
"github.com/dgraph-io/dgraph/x"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)
func TestValueCoercion(t *testing.T) {
tests := []QueryCase{
// test int/float/bool can be coerced to String
{Name: "int value should be coerced to string",
GQLQuery: `query { getAuthor(id: "0x1") { name } }`,
Response: `{ "getAuthor": { "name": 2 }}`,
Expected: `{ "getAuthor": { "name": "2"}}`},
{Name: "float value should be coerced to string",
GQLQuery: `query { getAuthor(id: "0x1") { name } }`,
Response: `{ "getAuthor": { "name": 2.134 }}`,
Expected: `{ "getAuthor": { "name": "2.134"}}`},
{Name: "bool value should be coerced to string",
GQLQuery: `query { getAuthor(id: "0x1") { name} }`,
Response: `{ "getAuthor": { "name": false } }`,
Expected: `{ "getAuthor": { "name": "false"}}`},
// test int/float/bool can be coerced to Enum
{Name: "int value should raise an error when coerced to postType",
GQLQuery: `query { getPost(postID: "0x1") { postType } }`,
Response: `{ "getPost": { "postType": 2 }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value '2' for field 'postType' to type PostType.",
Locations: []x.Location{x.Location{Line: 1, Column: 34}},
Path: []interface{}{"getPost", "postType"},
}},
Expected: `{ "getPost": { "postType": null }}`},
{Name: "float value should raise error when coerced to postType",
GQLQuery: `query { getPost(postID: "0x1") { postType } }`,
Response: `{ "getPost": { "postType": 2.134 }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value '2.134' for field 'postType' to type PostType.",
Locations: []x.Location{x.Location{Line: 1, Column: 34}},
Path: []interface{}{"getPost", "postType"},
}},
Expected: `{ "getPost": { "postType": null }}`},
{Name: "bool value should raise error when coerced to postType",
GQLQuery: `query { getPost(postID: "0x1") { postType } }`,
Response: `{ "getPost": { "postType": false }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value 'false' for field 'postType' to type PostType.",
Locations: []x.Location{x.Location{Line: 1, Column: 34}},
Path: []interface{}{"getPost", "postType"},
}},
Expected: `{ "getPost": { "postType": null }}`},
{Name: "string value should raise error it has invalid enum value",
GQLQuery: `query { getPost(postID: "0x1") { postType } }`,
Response: `{ "getPost": { "postType": "Random" }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value 'Random' for field 'postType' to type PostType.",
Locations: []x.Location{x.Location{Line: 1, Column: 34}},
Path: []interface{}{"getPost", "postType"},
}},
Expected: `{ "getPost": { "postType": null }}`},
{Name: "string value should be coerced to valid enum value",
GQLQuery: `query { getPost(postID: "0x1") { postType } }`,
Response: `{ "getPost": { "postType": "Question" }}`,
Expected: `{ "getPost": { "postType": "Question" }}`},
// test int/float/string can be coerced to Boolean
{Name: "int value should be coerced to bool",
GQLQuery: `query { getPost(postID: "0x1") { isPublished } }`,
Response: `{ "getPost": { "isPublished": 2 }}`,
Expected: `{ "getPost": { "isPublished": true}}`},
{Name: "int value should be coerced to bool with false value for 0",
GQLQuery: `query { getPost(postID: "0x1") { isPublished } }`,
Response: `{ "getPost": { "isPublished": 0 }}`,
Expected: `{ "getPost": { "isPublished": false}}`},
{Name: "float value should be coerced to bool",
GQLQuery: `query { getPost(postID: "0x1") { isPublished } }`,
Response: `{ "getPost": { "isPublished": 2.134 }}`,
Expected: `{ "getPost": { "isPublished": true}}`},
{Name: "float value should be coerced to bool with false value for 0",
GQLQuery: `query { getPost(postID: "0x1") { isPublished } }`,
Response: `{ "getPost": { "isPublished": 0.000 }}`,
Expected: `{ "getPost": { "isPublished": false}}`},
{Name: "string value should be coerced to bool",
GQLQuery: `query { getPost(postID: "0x1") { isPublished } }`,
Response: `{ "getPost": { "isPublished": "name" }}`,
Expected: `{ "getPost": { "isPublished": true }}`},
{Name: "string value should be coerced to bool false value when empty",
GQLQuery: `query { getPost(postID: "0x1") { isPublished } }`,
Response: `{ "getPost": { "isPublished": "" }}`,
Expected: `{ "getPost": { "isPublished": false }}`},
// test bool/float/string can be coerced to Int
{Name: "float value should be coerced to int",
GQLQuery: `query { getPost(postID: "0x1") { numLikes } }`,
Response: `{ "getPost": { "numLikes": 2.000 }}`,
Expected: `{ "getPost": { "numLikes": 2}}`},
{Name: "string value should be coerced to int",
GQLQuery: `query { getPost(postID: "0x1") { numLikes } }`,
Response: `{ "getPost": { "numLikes": "23" }}`,
Expected: `{ "getPost": { "numLikes": 23}}`},
{Name: "string float value should be coerced to int",
GQLQuery: `query { getPost(postID: "0x1") { numLikes } }`,
Response: `{ "getPost": { "numLikes": "23.00" }}`,
Expected: `{ "getPost": { "numLikes": 23}}`},
{Name: "bool true value should be coerced to int value 1",
GQLQuery: `query { getPost(postID: "0x1") { numLikes } }`,
Response: `{ "getPost": { "numLikes": true }}`,
Expected: `{ "getPost": { "numLikes": 1}}`},
{Name: "bool false value should be coerced to int",
GQLQuery: `query { getPost(postID: "0x1") { numLikes } }`,
Response: `{ "getPost": { "numLikes": false }}`,
Expected: `{ "getPost": { "numLikes": 0}}`},
{Name: "field should return an error when it is greater than int32" +
" without losing data",
GQLQuery: `query { getPost(postID: "0x1") { numLikes } }`,
Response: `{ "getPost": { "numLikes": 2147483648 }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value '2.147483648e+09' for field 'numLikes' to type" +
" Int.",
Locations: []x.Location{x.Location{Line: 1, Column: 34}},
Path: []interface{}{"getPost", "numLikes"},
}},
Expected: `{"getPost": {"numLikes": null}}`,
},
{Name: "field should return an error when float can't be coerced to int" +
" without losing data",
GQLQuery: `query { getPost(postID: "0x1") { numLikes } }`,
Response: `{ "getPost": { "numLikes": 123.23 }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value '123.23' for field 'numLikes' to type Int.",
Locations: []x.Location{x.Location{Line: 1, Column: 34}},
Path: []interface{}{"getPost", "numLikes"},
}},
Expected: `{"getPost": {"numLikes": null}}`,
},
{Name: "field should return an error when when it can't be coerced as int32" +
" from a string",
GQLQuery: `query { getPost(postID: "0x1") { numLikes } }`,
Response: `{ "getPost": { "numLikes": "123.23" }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value '123.23' for field 'numLikes' to type Int.",
Locations: []x.Location{x.Location{Line: 1, Column: 34}},
Path: []interface{}{"getPost", "numLikes"},
}},
Expected: `{"getPost": {"numLikes": null}}`,
},
// test bool/int/string can be coerced to Float
{Name: "int value should be coerced to float",
GQLQuery: `query { getAuthor(id: "0x1") { reputation } }`,
Response: `{ "getAuthor": { "reputation": 2 }}`,
Expected: `{ "getAuthor": { "reputation": 2.0 }}`},
{Name: "string value should be coerced to float",
GQLQuery: `query { getAuthor(id: "0x1") { reputation } }`,
Response: `{ "getAuthor": { "reputation": "23.123" }}`,
Expected: `{ "getAuthor": { "reputation": 23.123 }}`},
{Name: "bool true value should be coerced to float value 1.0",
GQLQuery: `query { getAuthor(id: "0x1") { reputation } }`,
Response: `{ "getAuthor": { "reputation": true }}`,
Expected: `{ "getAuthor": { "reputation": 1.0 }}`},
{Name: "bool false value should be coerced to float value 0.0",
GQLQuery: `query { getAuthor(id: "0x1") { reputation } }`,
Response: `{ "getAuthor": { "reputation": false }}`,
Expected: `{ "getAuthor": { "reputation": 0.0}}`},
// test bool/int/string/datetime can be coerced to Datetime
{Name: "float value should raise an error when tried to be coerced to datetime",
GQLQuery: `query { getAuthor(id: "0x1") { dob } }`,
Response: `{ "getAuthor": { "dob": "23.123" }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value '23.123' for field 'dob' to type DateTime.",
Locations: []x.Location{x.Location{Line: 1, Column: 32}},
Path: []interface{}{"getAuthor", "dob"},
}},
Expected: `{ "getAuthor": { "dob": null }}`},
{Name: "bool value should raise an error when coerced as datetime",
GQLQuery: `query { getAuthor(id: "0x1") { dob } }`,
Response: `{ "getAuthor": { "dob": true }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value 'true' for field 'dob' to type DateTime.",
Locations: []x.Location{x.Location{Line: 1, Column: 32}},
Path: []interface{}{"getAuthor", "dob"},
}},
Expected: `{ "getAuthor": { "dob": null }}`},
{Name: "invalid string value should raise an error when tried to be coerced to datetime",
GQLQuery: `query { getAuthor(id: "0x1") { dob } }`,
Response: `{ "getAuthor": { "dob": "123" }}`,
Errors: x.GqlErrorList{{
Message: "Error coercing value '123' for field 'dob' to type DateTime.",
Locations: []x.Location{x.Location{Line: 1, Column: 32}},
Path: []interface{}{"getAuthor", "dob"},
}},
Expected: `{ "getAuthor": { "dob": null}}`},
{Name: "int value should be coerced to datetime",
GQLQuery: `query { getAuthor(id: "0x1") { dob } }`,
Response: `{ "getAuthor": { "dob": 2 }}`,
Expected: `{ "getAuthor": { "dob": "1970-01-01T00:00:02Z"}}`},
{Name: "float value that can be truncated safely should be coerced to datetime",
GQLQuery: `query { getAuthor(id: "0x1") { dob } }`,
Response: `{ "getAuthor": { "dob": 2.0 }}`,
Expected: `{ "getAuthor": { "dob": "1970-01-01T00:00:02Z"}}`},
{Name: "val string value should be coerced to datetime",
GQLQuery: `query { getAuthor(id: "0x1") { dob } }`,
Response: `{ "getAuthor": { "dob": "2012-11-01T22:08:41+00:00" }}`,
Expected: `{ "getAuthor": { "dob": "2012-11-01T22:08:41+00:00" }}`},
}
gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql")
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
resp := resolve(gqlSchema, test.GQLQuery, test.Response)
if diff := cmp.Diff(test.Errors, resp.Errors); diff != "" {
t.Errorf("errors mismatch (-want +got):\n%s", diff)
}
require.JSONEq(t, test.Expected, resp.Data.String())
})
}
}
func TestQueryAlias(t *testing.T) {
tests := []QueryCase{
{Name: "top level alias",
GQLQuery: `query { auth : getAuthor(id: "0x1") { name } }`,
Response: `{ "getAuthor": [ { "name": "A.N. Author" } ] }`,
Expected: `{"auth": {"name": "A.N. Author"}}`},
{Name: "field level alias",
GQLQuery: `query { getAuthor(id: "0x1") { authName: name } }`,
Response: `{ "getAuthor": [ { "name": "A.N. Author" } ] }`,
Expected: `{"getAuthor": {"authName": "A.N. Author"}}`},
{Name: "deep alias",
GQLQuery: `query { getAuthor(id: "0x1") { name posts { theTitle : title } } }`,
Response: `{"getAuthor": [{"name": "A.N. Author", "posts": [{"title": "A Post"}]}]}`,
Expected: `{"getAuthor": {"name": "A.N. Author", "posts": [{"theTitle": "A Post"}]}}`},
{Name: "many aliases",
GQLQuery: `query {
auth : getAuthor(id: "0x1") { name myPosts : posts { theTitle : title } }
post : getPost(postID: "0x2") { postTitle: title } }`,
Response: `{
"getAuthor": [{"name": "A.N. Author", "posts": [{"title": "A Post"}]}],
"getPost": [ { "title": "A Post" } ] }`,
Expected: `{"auth": {"name": "A.N. Author", "myPosts": [{"theTitle": "A Post"}]},` +
`"post": {"postTitle": "A Post"}}`},
}
gqlSchema := test.LoadSchemaFromFile(t, "schema.graphql")
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.JSONEq(t, test.Expected, resp.Data.String())
})
}
}
func TestMutationAlias(t *testing.T) {
tests := map[string]struct {
gqlQuery string
mutResponse map[string]string
mutQryResp map[string]interface{}
queryResponse string
expected string
}{
"mutation top level alias ": {
gqlQuery: `mutation {
add: addPost(input: [{title: "A Post", text: "Some text", author: {id: "0x1"}}]) {
post { title }
}
}`,
mutResponse: map[string]string{"Post1": "0x2"},
mutQryResp: map[string]interface{}{
"Author2": []interface{}{map[string]string{"uid": "0x1"}}},
queryResponse: `{ "post" : [ { "title": "A Post" } ] }`,
expected: `{ "add": { "post" : [{ "title": "A Post"}] } }`,
},
"mutation deep alias ": {
gqlQuery: `mutation {
addPost(input: [{title: "A Post", text: "Some text", author: {id: "0x1"}}]) {
thePosts : post { postTitle : title }
}
}`,
mutResponse: map[string]string{"Post1": "0x2"},
mutQryResp: map[string]interface{}{
"Author2": []interface{}{map[string]string{"uid": "0x1"}}},
queryResponse: `{ "post" : [ { "title": "A Post" } ] }`,
expected: `{ "addPost": { "thePosts" : [{ "postTitle": "A Post"}] } }`,
},
"mutation many aliases ": {
gqlQuery: `mutation {
add1: addPost(input: [{title: "A Post", text: "Some text", author: {id: "0x1"}}]) {
thePosts : post { postTitle : title }
}
add2: addPost(input: [{title: "A Post", text: "Some text", author: {id: "0x1"}}]) {
otherPosts : post { t : title }
}
}`,
mutResponse: map[string]string{"Post1": "0x2"},
mutQryResp: map[string]interface{}{
"Author2": []interface{}{map[string]string{"uid": "0x1"}}},
queryResponse: `{ "post" : [ { "title": "A Post" } ] }`,
expected: `{
"add1": { "thePosts" : [{ "postTitle": "A Post"}] },
"add2": { "otherPosts" : [{ "t": "A Post"}] } }`,
},
}
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
for name, tcase := range tests {
t.Run(name, func(t *testing.T) {
resp := resolveWithClient(gqlSchema, tcase.gqlQuery, nil,
&executor{
resp: tcase.queryResponse,
assigned: tcase.mutResponse,
result: tcase.mutQryResp,
})
require.Nil(t, resp.Errors)
require.JSONEq(t, tcase.expected, resp.Data.String())
})
}
}
// 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"}]}}`},
{Name: "Whole operation GQL query order",
GQLQuery: `query { ` +
`getAuthor(id: "0x1") { name }` +
`getPost(id: "0x2") { title } }`,
Response: `{ "getAuthor": [ { "name": "A.N. Author" } ],` +
`"getPost": [ { "title": "A Post" } ] }`,
Expected: `{"getAuthor": {"name": "A.N. Author"},` +
`"getPost": {"title": "A Post"}}`},
}
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())
})
}
}