forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor_schema_test.go
315 lines (295 loc) · 6.93 KB
/
executor_schema_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
package graphql_test
import (
"fmt"
"reflect"
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
)
// TODO: have a separate package for other tests for eg `parser`
// maybe for:
// - tests that supposed to be black-boxed (no reason to access private identifiers)
// - tests that create internal tests structs, we might not want to pollute the package with too many test structs
type testPic struct {
Url string `json:"url"`
Width string `json:"width"`
Height string `json:"height"`
}
type testPicFn func(width, height string) *testPic
type testAuthor struct {
Id int `json:"id"`
Name string `json:"name"`
Pic testPicFn `json:"pic"`
RecentArticle *testArticle `json:"recentArticle"`
}
type testArticle struct {
Id string `json:"id"`
IsPublished string `json:"isPublished"`
Author *testAuthor `json:"author"`
Title string `json:"title"`
Body string `json:"body"`
Hidden string `json:"hidden"`
Keywords []interface{} `json:"keywords"`
}
func getPic(id int, width, height string) *testPic {
return &testPic{
Url: fmt.Sprintf("cdn://%v", id),
Width: width,
Height: height,
}
}
var johnSmith *testAuthor
func article(id interface{}) *testArticle {
return &testArticle{
Id: fmt.Sprintf("%v", id),
IsPublished: "true",
Author: johnSmith,
Title: fmt.Sprintf("My Article %v", id),
Body: "This is a post",
Hidden: "This data is not exposed in the schema",
Keywords: []interface{}{
"foo", "bar", 1, true, nil,
},
}
}
func TestExecutesUsingAComplexSchema(t *testing.T) {
johnSmith = &testAuthor{
Id: 123,
Name: "John Smith",
Pic: func(width string, height string) *testPic {
return getPic(123, width, height)
},
RecentArticle: article("1"),
}
blogImage := graphql.NewObject(graphql.ObjectConfig{
Name: "Image",
Fields: graphql.Fields{
"url": &graphql.Field{
Type: graphql.String,
},
"width": &graphql.Field{
Type: graphql.Int,
},
"height": &graphql.Field{
Type: graphql.Int,
},
},
})
blogAuthor := graphql.NewObject(graphql.ObjectConfig{
Name: "Author",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
},
"name": &graphql.Field{
Type: graphql.String,
},
"pic": &graphql.Field{
Type: blogImage,
Args: graphql.FieldConfigArgument{
"width": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"height": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if author, ok := p.Source.(*testAuthor); ok {
width := fmt.Sprintf("%v", p.Args["width"])
height := fmt.Sprintf("%v", p.Args["height"])
return author.Pic(width, height), nil
}
return nil, nil
},
},
"recentArticle": &graphql.Field{},
},
})
blogArticle := graphql.NewObject(graphql.ObjectConfig{
Name: "Article",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
},
"isPublished": &graphql.Field{
Type: graphql.Boolean,
},
"author": &graphql.Field{
Type: blogAuthor,
},
"title": &graphql.Field{
Type: graphql.String,
},
"body": &graphql.Field{
Type: graphql.String,
},
"keywords": &graphql.Field{
Type: graphql.NewList(graphql.String),
},
},
})
blogAuthor.AddFieldConfig("recentArticle", &graphql.Field{
Type: blogArticle,
})
blogQuery := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"article": &graphql.Field{
Type: blogArticle,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.ID,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
id := p.Args["id"]
return article(id), nil
},
},
"feed": &graphql.Field{
Type: graphql.NewList(blogArticle),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return []*testArticle{
article(1),
article(2),
article(3),
article(4),
article(5),
article(6),
article(7),
article(8),
article(9),
article(10),
}, nil
},
},
},
})
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
request := `
{
feed {
id,
title
},
article(id: "1") {
...articleFields,
author {
id,
name,
pic(width: 640, height: 480) {
url,
width,
height
},
recentArticle {
...articleFields,
keywords
}
}
}
}
fragment articleFields on Article {
id,
isPublished,
title,
body,
hidden,
notdefined
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"article": map[string]interface{}{
"title": "My Article 1",
"body": "This is a post",
"author": map[string]interface{}{
"id": "123",
"name": "John Smith",
"pic": map[string]interface{}{
"url": "cdn://123",
"width": 640,
"height": 480,
},
"recentArticle": map[string]interface{}{
"id": "1",
"isPublished": bool(true),
"title": "My Article 1",
"body": "This is a post",
"keywords": []interface{}{
"foo",
"bar",
"1",
"true",
nil,
},
},
},
"id": "1",
"isPublished": bool(true),
},
"feed": []interface{}{
map[string]interface{}{
"id": "1",
"title": "My Article 1",
},
map[string]interface{}{
"id": "2",
"title": "My Article 2",
},
map[string]interface{}{
"id": "3",
"title": "My Article 3",
},
map[string]interface{}{
"id": "4",
"title": "My Article 4",
},
map[string]interface{}{
"id": "5",
"title": "My Article 5",
},
map[string]interface{}{
"id": "6",
"title": "My Article 6",
},
map[string]interface{}{
"id": "7",
"title": "My Article 7",
},
map[string]interface{}{
"id": "8",
"title": "My Article 8",
},
map[string]interface{}{
"id": "9",
"title": "My Article 9",
},
map[string]interface{}{
"id": "10",
"title": "My Article 10",
},
},
},
}
// parse query
ast := testutil.TestParse(t, request)
// execute
ep := graphql.ExecuteParams{
Schema: blogSchema,
AST: ast,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}