forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor_resolve_test.go
266 lines (238 loc) · 7 KB
/
executor_resolve_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
package graphql_test
import (
"encoding/json"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"reflect"
"testing"
)
func testSchema(t *testing.T, testField *graphql.Field) graphql.Schema {
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"test": testField,
},
}),
})
if err != nil {
t.Fatalf("Invalid schema: %v", err)
}
return schema
}
func TestExecutesResolveFunction_DefaultFunctionAccessesProperties(t *testing.T) {
schema := testSchema(t, &graphql.Field{Type: graphql.String})
source := map[string]interface{}{
"test": "testValue",
}
expected := map[string]interface{}{
"test": "testValue",
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test }`,
RootObject: source,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
}
func TestExecutesResolveFunction_DefaultFunctionCallsMethods(t *testing.T) {
schema := testSchema(t, &graphql.Field{Type: graphql.String})
source := map[string]interface{}{
"test": func() interface{} {
return "testValue"
},
}
expected := map[string]interface{}{
"test": "testValue",
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test }`,
RootObject: source,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
}
func TestExecutesResolveFunction_UsesProvidedResolveFunction(t *testing.T) {
schema := testSchema(t, &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"aStr": &graphql.ArgumentConfig{Type: graphql.String},
"aInt": &graphql.ArgumentConfig{Type: graphql.Int},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
b, err := json.Marshal(p.Args)
return string(b), err
},
})
expected := map[string]interface{}{
"test": "{}",
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
expected = map[string]interface{}{
"test": `{"aStr":"String!"}`,
}
result = graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test(aStr: "String!") }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
expected = map[string]interface{}{
"test": `{"aInt":-123,"aStr":"String!"}`,
}
result = graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test(aInt: -123, aStr: "String!") }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
}
func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_WithoutJSONTags(t *testing.T) {
// For structs without JSON tags, it will map to upper-cased exported field names
type SubObjectWithoutJSONTags struct {
Str string
Int int
}
schema := testSchema(t, &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "SubObject",
Description: "Maps GraphQL Object `SubObject` to Go struct `SubObjectWithoutJSONTags`",
Fields: graphql.Fields{
"Str": &graphql.Field{Type: graphql.String},
"Int": &graphql.Field{Type: graphql.Int},
},
}),
Args: graphql.FieldConfigArgument{
"aStr": &graphql.ArgumentConfig{Type: graphql.String},
"aInt": &graphql.ArgumentConfig{Type: graphql.Int},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
aStr, _ := p.Args["aStr"].(string)
aInt, _ := p.Args["aInt"].(int)
return &SubObjectWithoutJSONTags{
Str: aStr,
Int: aInt,
}, nil
},
})
expected := map[string]interface{}{
"test": map[string]interface{}{
"Str": nil,
"Int": 0,
},
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test { Str, Int } }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
expected = map[string]interface{}{
"test": map[string]interface{}{
"Str": "String!",
"Int": 0,
},
}
result = graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test(aStr: "String!") { Str, Int } }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
expected = map[string]interface{}{
"test": map[string]interface{}{
"Str": "String!",
"Int": -123,
},
}
result = graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test(aInt: -123, aStr: "String!") { Str, Int } }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
}
func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_WithJSONTags(t *testing.T) {
// For structs without JSON tags, it will map to upper-cased exported field names
type SubObjectWithJSONTags struct {
OtherField string `json:""`
Str string `json:"str"`
Int int `json:"int"`
}
schema := testSchema(t, &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "SubObject",
Description: "Maps GraphQL Object `SubObject` to Go struct `SubObjectWithJSONTags`",
Fields: graphql.Fields{
"str": &graphql.Field{Type: graphql.String},
"int": &graphql.Field{Type: graphql.Int},
},
}),
Args: graphql.FieldConfigArgument{
"aStr": &graphql.ArgumentConfig{Type: graphql.String},
"aInt": &graphql.ArgumentConfig{Type: graphql.Int},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
aStr, _ := p.Args["aStr"].(string)
aInt, _ := p.Args["aInt"].(int)
return &SubObjectWithJSONTags{
Str: aStr,
Int: aInt,
}, nil
},
})
expected := map[string]interface{}{
"test": map[string]interface{}{
"str": nil,
"int": 0,
},
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test { str, int } }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
expected = map[string]interface{}{
"test": map[string]interface{}{
"str": "String!",
"int": 0,
},
}
result = graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test(aStr: "String!") { str, int } }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
expected = map[string]interface{}{
"test": map[string]interface{}{
"str": "String!",
"int": -123,
},
}
result = graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test(aInt: -123, aStr: "String!") { str, int } }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
}