-
Notifications
You must be signed in to change notification settings - Fork 0
/
Field.go
176 lines (139 loc) · 3.65 KB
/
Field.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
package graphql
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/aerogo/mirror"
)
// Field represents a queryable field.
type Field struct {
name string
arguments Map
fields []*Field
parent FieldContainer
}
// AddField adds a field to the query.
func (field *Field) AddField(newField *Field) {
field.fields = append(field.fields, newField)
}
// Fields returns the list of fields inside the query.
func (field *Field) Fields() []*Field {
return field.fields
}
// Returns the field's parent.
func (field *Field) Parent() FieldContainer {
return field.parent
}
// Resolve resolves the field value for the given parent in the given database.
func (field *Field) Resolve(parent interface{}, api *API) (interface{}, error) {
// If we have no parent object, treat it as a root query
if parent == nil {
return field.resolveRootQuery(api)
}
// Allow querying the current type name
if field.name == "__typename" {
t := reflect.TypeOf(parent)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t.Name(), nil
}
// Aliases
name := field.name
typeName := reflect.TypeOf(parent).Name()
aliasMap := api.aliases[typeName]
if aliasMap != nil {
aliasedName, hasAlias := aliasMap[field.name]
if hasAlias {
name = aliasedName
}
}
// Fields that are direct descendants
structField, _, value, err := mirror.GetChildField(parent, name)
if err != nil {
return nil, err
}
if structField.Tag.Get("private") == "true" {
return nil, fmt.Errorf("'%s' is a private field", field.name)
}
return value.Interface(), nil
}
// resolveRootQuery resolves a root query.
func (field *Field) resolveRootQuery(api *API) (interface{}, error) {
// Custom resolvers
for _, resolve := range api.rootResolvers {
obj, err, ok := resolve(field.name, field.arguments)
if ok {
return obj, err
}
}
// Schema query
if field.name == "__schema" {
return api.schema, nil
}
// "All" queries
if strings.HasPrefix(field.name, "all") {
return field.ResolveAll(api)
}
// Return an error if the type doesn't exist
if !api.db.HasType(field.name) {
return nil, fmt.Errorf("Type '%s' does not exist", field.name)
}
// Single object queries
if len(field.arguments) != 1 {
return nil, errors.New("Single object queries require must specify an ID and nothing else")
}
for _, id := range field.arguments {
return api.db.Get(field.name, id.(string))
}
// This code is actually unreachable,
// but the linter is too dumb to realize that
// so we're going to end it with a return statement.
return nil, nil
}
// ResolveAll returns a list of objects that matches the filter arguments.
func (field *Field) ResolveAll(api *API) (interface{}, error) {
records := []interface{}{}
typeName := strings.TrimPrefix(field.name, "all")
for argName, argValue := range field.arguments {
if !strings.Contains(argName, "_") {
continue
}
delete(field.arguments, argName)
argName = strings.ReplaceAll(argName, "_", ".")
field.arguments[argName] = argValue
}
for record := range api.db.All(typeName) {
matchingFields := 0
for argName, argValue := range field.arguments {
_, _, value, err := mirror.GetPublicField(record, argName)
if err != nil {
return nil, err
}
switch argValue.(type) {
case string:
if value.String() == argValue {
matchingFields++
}
case int64:
if value.Int() == argValue {
matchingFields++
}
case float64:
if value.Float() == argValue {
matchingFields++
}
case bool:
if value.Bool() == argValue {
matchingFields++
continue
}
}
}
if matchingFields == len(field.arguments) {
records = append(records, record)
}
}
return records, nil
}