forked from bhoriuchi/graphql-go-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
values.go
189 lines (169 loc) · 4.68 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
package tools
// taken from https://github.com/graphql-go/graphql/values.go
// since none of these functions are exported
import (
"fmt"
"math"
"reflect"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
)
// Prepares an object map of argument values given a list of argument
// definitions and list of argument AST nodes.
func GetArgumentValues(argDefs []*graphql.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
}
// fix for checking that non nulls are not null
typeString := argDef.Type.String()
isNonNull := typeString[len(typeString)-1:] == "!"
if isNonNull && isNullish(value) {
locs := []string{}
for _, a := range argASTs {
locs = append(locs, fmt.Sprintf("%d:%d", a.Loc.Start, a.Loc.End))
}
return nil, fmt.Errorf(`graphql input %q @ %q cannot be null`, name, locs)
}
if !isNullish(value) {
results[name] = value
}
}
return results, nil
}
// Returns true if a value is null, undefined, or NaN.
func isNullish(src interface{}) bool {
if src == nil {
return true
}
value := reflect.ValueOf(src)
if value.Kind() == reflect.Ptr {
value = value.Elem()
}
switch value.Kind() {
case reflect.String:
// if src is ptr type and len(string)=0, it returns false
if !value.IsValid() {
return true
}
case reflect.Int:
return math.IsNaN(float64(value.Int()))
case reflect.Float32, reflect.Float64:
return math.IsNaN(float64(value.Float()))
}
return false
}
/**
* 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 graphql.Input, variables map[string]interface{}) interface{} {
if ttype, ok := ttype.(*graphql.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.(*graphql.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.(*graphql.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]
fieldValue := field.DefaultValue
if !ok || fieldAST == nil {
if fieldValue == nil {
continue
}
} else {
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 *graphql.Scalar:
parsed := ttype.ParseLiteral(valueAST)
if !isNullish(parsed) {
return parsed
}
case *graphql.Enum:
parsed := ttype.ParseLiteral(valueAST)
if !isNullish(parsed) {
return parsed
}
}
return nil
}