forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 2
/
values.go
401 lines (371 loc) · 10.1 KB
/
values.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
package graphql
import (
"encoding/json"
"fmt"
"math"
"reflect"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
"github.com/graphql-go/graphql/language/printer"
)
// Prepares an object map of variableValues of the correct type based on the
// provided variable definitions and arbitrary input. If the input cannot be
// parsed to match the variable definitions, a GraphQLError will be returned.
func getVariableValues(schema Schema, definitionASTs []*ast.VariableDefinition, inputs map[string]interface{}) (map[string]interface{}, error) {
values := map[string]interface{}{}
for _, defAST := range definitionASTs {
if defAST == nil || defAST.Variable == nil || defAST.Variable.Name == nil {
continue
}
varName := defAST.Variable.Name.Value
varValue, err := getVariableValue(schema, defAST, inputs[varName])
if err != nil {
return values, err
}
values[varName] = varValue
}
return values, nil
}
// Prepares an object map of argument values given a list of argument
// definitions and list of argument AST nodes.
func getArgumentValues(argDefs []*Argument, argASTs []*ast.Argument, variableVariables map[string]interface{}) (map[string]interface{}, error) {
argASTMap := map[string]*ast.Argument{}
for _, argAST := range argASTs {
if argAST.Name != nil {
argASTMap[argAST.Name.Value] = argAST
}
}
results := map[string]interface{}{}
for _, argDef := range argDefs {
name := argDef.PrivateName
var valueAST ast.Value
if argAST, ok := argASTMap[name]; ok {
valueAST = argAST.Value
}
value := valueFromAST(valueAST, argDef.Type, variableVariables)
if isNullish(value) {
value = argDef.DefaultValue
}
if !isNullish(value) {
results[name] = value
}
}
return results, nil
}
// Given a variable definition, and any value of input, return a value which
// adheres to the variable definition, or throw an error.
func getVariableValue(schema Schema, definitionAST *ast.VariableDefinition, input interface{}) (interface{}, error) {
ttype, err := typeFromAST(schema, definitionAST.Type)
if err != nil {
return nil, err
}
variable := definitionAST.Variable
if ttype == nil || !IsInputType(ttype) {
return "", gqlerrors.NewError(
fmt.Sprintf(`Variable "$%v" expected value of type `+
`"%v" which cannot be used as an input type.`, variable.Name.Value, printer.Print(definitionAST.Type)),
[]ast.Node{definitionAST},
"",
nil,
[]int{},
)
}
if isValidInputValue(input, ttype) {
if isNullish(input) {
defaultValue := definitionAST.DefaultValue
if defaultValue != nil {
variables := map[string]interface{}{}
val := valueFromAST(defaultValue, ttype, variables)
return val, nil
}
}
return coerceValue(ttype, input), nil
}
if isNullish(input) {
return "", gqlerrors.NewError(
fmt.Sprintf(`Variable "$%v" of required type `+
`"%v" was not provided.`, variable.Name.Value, printer.Print(definitionAST.Type)),
[]ast.Node{definitionAST},
"",
nil,
[]int{},
)
}
inputStr := ""
b, err := json.Marshal(input)
if err == nil {
inputStr = string(b)
}
return "", gqlerrors.NewError(
fmt.Sprintf(`Variable "$%v" expected value of type `+
`"%v" but got: %v.`, variable.Name.Value, printer.Print(definitionAST.Type), inputStr),
[]ast.Node{definitionAST},
"",
nil,
[]int{},
)
}
// Given a type and any value, return a runtime value coerced to match the type.
func coerceValue(ttype Input, value interface{}) interface{} {
if ttype, ok := ttype.(*NonNull); ok {
return coerceValue(ttype.OfType, value)
}
if isNullish(value) {
return nil
}
if ttype, ok := ttype.(*List); ok {
itemType := ttype.OfType
valType := reflect.ValueOf(value)
if valType.Kind() == reflect.Slice {
values := []interface{}{}
for i := 0; i < valType.Len(); i++ {
val := valType.Index(i).Interface()
v := coerceValue(itemType, val)
values = append(values, v)
}
return values
}
val := coerceValue(itemType, value)
return []interface{}{val}
}
if ttype, ok := ttype.(*InputObject); ok {
valueMap, ok := value.(map[string]interface{})
if !ok {
valueMap = map[string]interface{}{}
}
obj := map[string]interface{}{}
for fieldName, field := range ttype.Fields() {
value, _ := valueMap[fieldName]
fieldValue := coerceValue(field.Type, value)
if isNullish(fieldValue) {
fieldValue = field.DefaultValue
}
if !isNullish(fieldValue) {
obj[fieldName] = fieldValue
}
}
return obj
}
switch ttype := ttype.(type) {
case *Scalar:
parsed := ttype.ParseValue(value)
if !isNullish(parsed) {
return parsed
}
case *Enum:
parsed := ttype.ParseValue(value)
if !isNullish(parsed) {
return parsed
}
}
return nil
}
// graphql-js/src/utilities.js`
// TODO: figure out where to organize utils
// TODO: change to *Schema
func typeFromAST(schema Schema, inputTypeAST ast.Type) (Type, error) {
switch inputTypeAST := inputTypeAST.(type) {
case *ast.List:
innerType, err := typeFromAST(schema, inputTypeAST.Type)
if err != nil {
return nil, err
}
return NewList(innerType), nil
case *ast.NonNull:
innerType, err := typeFromAST(schema, inputTypeAST.Type)
if err != nil {
return nil, err
}
return NewNonNull(innerType), nil
case *ast.Named:
nameValue := ""
if inputTypeAST.Name != nil {
nameValue = inputTypeAST.Name.Value
}
ttype := schema.Type(nameValue)
return ttype, nil
default:
return nil, invariant(inputTypeAST.GetKind() == kinds.Named, "Must be a named type.")
}
}
// isValidInputValue alias isValidJSValue
// Given a value and a GraphQL type, determine if the value will be
// accepted for that type. This is primarily useful for validating the
// runtime values of query variables.
func isValidInputValue(value interface{}, ttype Input) bool {
if ttype, ok := ttype.(*NonNull); ok {
if isNullish(value) {
return false
}
return isValidInputValue(value, ttype.OfType)
}
if isNullish(value) {
return true
}
switch ttype := ttype.(type) {
case *List:
itemType := ttype.OfType
valType := reflect.ValueOf(value)
if valType.Kind() == reflect.Ptr {
valType = valType.Elem()
}
if valType.Kind() == reflect.Slice {
for i := 0; i < valType.Len(); i++ {
val := valType.Index(i).Interface()
if !isValidInputValue(val, itemType) {
return false
}
}
return true
}
return isValidInputValue(value, itemType)
case *InputObject:
valueMap, ok := value.(map[string]interface{})
if !ok {
return false
}
fields := ttype.Fields()
// Ensure every provided field is defined.
for fieldName, _ := range valueMap {
if _, ok := fields[fieldName]; !ok {
return false
}
}
// Ensure every defined field is valid.
for fieldName, _ := range fields {
isValid := isValidInputValue(valueMap[fieldName], fields[fieldName].Type)
if !isValid {
return false
}
}
return true
}
switch ttype := ttype.(type) {
case *Scalar:
parsedVal := ttype.ParseValue(value)
return !isNullish(parsedVal)
case *Enum:
parsedVal := ttype.ParseValue(value)
return !isNullish(parsedVal)
}
return false
}
// Returns true if a value is null, undefined, or NaN.
func isNullish(value interface{}) bool {
if value, ok := value.(string); ok {
return value == ""
}
if value, ok := value.(int); ok {
return math.IsNaN(float64(value))
}
if value, ok := value.(float32); ok {
return math.IsNaN(float64(value))
}
if value, ok := value.(float64); ok {
return math.IsNaN(value)
}
return value == nil
}
/**
* Produces a value given a GraphQL Value AST.
*
* A GraphQL type must be provided, which will be used to interpret different
* GraphQL Value literals.
*
* | GraphQL Value | JSON Value |
* | -------------------- | ------------- |
* | Input Object | Object |
* | List | Array |
* | Boolean | Boolean |
* | String / Enum Value | String |
* | Int / Float | Number |
*
*/
func valueFromAST(valueAST ast.Value, ttype Input, variables map[string]interface{}) interface{} {
if ttype, ok := ttype.(*NonNull); ok {
val := valueFromAST(valueAST, ttype.OfType, variables)
return val
}
if valueAST == nil {
return nil
}
if valueAST, ok := valueAST.(*ast.Variable); ok && valueAST.Kind == kinds.Variable {
if valueAST.Name == nil {
return nil
}
if variables == nil {
return nil
}
variableName := valueAST.Name.Value
variableVal, ok := variables[variableName]
if !ok {
return nil
}
// Note: we're not doing any checking that this variable is correct. We're
// assuming that this query has been validated and the variable usage here
// is of the correct type.
return variableVal
}
if ttype, ok := ttype.(*List); ok {
itemType := ttype.OfType
if valueAST, ok := valueAST.(*ast.ListValue); ok && valueAST.Kind == kinds.ListValue {
values := []interface{}{}
for _, itemAST := range valueAST.Values {
v := valueFromAST(itemAST, itemType, variables)
values = append(values, v)
}
return values
}
v := valueFromAST(valueAST, itemType, variables)
return []interface{}{v}
}
if ttype, ok := ttype.(*InputObject); ok {
valueAST, ok := valueAST.(*ast.ObjectValue)
if !ok {
return nil
}
fieldASTs := map[string]*ast.ObjectField{}
for _, fieldAST := range valueAST.Fields {
if fieldAST.Name == nil {
continue
}
fieldName := fieldAST.Name.Value
fieldASTs[fieldName] = fieldAST
}
obj := map[string]interface{}{}
for fieldName, field := range ttype.Fields() {
fieldAST, ok := fieldASTs[fieldName]
if !ok || fieldAST == nil {
continue
}
fieldValue := valueFromAST(fieldAST.Value, field.Type, variables)
if isNullish(fieldValue) {
fieldValue = field.DefaultValue
}
if !isNullish(fieldValue) {
obj[fieldName] = fieldValue
}
}
return obj
}
switch ttype := ttype.(type) {
case *Scalar:
parsed := ttype.ParseLiteral(valueAST)
if !isNullish(parsed) {
return parsed
}
case *Enum:
parsed := ttype.ParseLiteral(valueAST)
if !isNullish(parsed) {
return parsed
}
}
return nil
}
func invariant(condition bool, message string) error {
if !condition {
return gqlerrors.NewFormattedError(message)
}
return nil
}