-
Notifications
You must be signed in to change notification settings - Fork 3
/
field.go
332 lines (292 loc) · 9.42 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
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
package builders
import (
"context"
"fmt"
"strings"
"github.com/iancoleman/strcase"
"github.com/99designs/gqlgen/graphql"
"github.com/spf13/cast"
"github.com/vektah/gqlparser/v2/ast"
)
type fieldType string
const (
TypeScalar fieldType = "Scalar"
TypeRelation fieldType = "Relation"
TypeAggregate fieldType = "Aggregate"
TypeObject fieldType = "Object"
)
type OperationType string
const (
QueryOperation OperationType = "query"
InsertOperation OperationType = "insert"
DeleteOperation OperationType = "delete"
UpdateOperation OperationType = "update"
UnknownOperation OperationType = "unknown"
)
type Fields []Field
func (ff Fields) HasSelection(sel string) bool {
for _, f := range ff {
if f.Name == sel {
return true
}
}
return false
}
// Field is a helpful wrapper over *ast.Field adding more custom information when collecting the GraphQL query AST
type Field struct {
// Original *ast.Field
*ast.Field
// Field sub selection, if field type is TypeScalar, selections will be empty
Selections Fields
// FieldType i.e Scalar/Relation/Aggregate/Object etc'
FieldType fieldType
// Arguments passed to this field if any.
Arguments map[string]interface{}
// TypeDefinition is the ast.Schema Type, this is saved for easier access
TypeDefinition *ast.Definition
// Parent field of this field
Parent *Field
}
func NewField(parent *Field, field *ast.Field, schema *ast.Schema, args map[string]interface{}) Field {
typeName := getTypeName(field)
typeDef := schema.Types[typeName]
return Field{
Field: field,
Selections: nil,
FieldType: parseFieldType(field, typeDef),
Arguments: args,
TypeDefinition: typeDef,
Parent: parent,
}
}
func (f Field) ForName(name string) (Field, error) {
for _, s := range f.Selections {
if s.Name == name {
return s, nil
}
}
return Field{}, fmt.Errorf("field doesn't exist")
}
func (f Field) GetTypeName() string {
return f.TypeDefinition.Name
}
// Relation directive on field, if it exists
func (f Field) Relation() *RelationDirective {
return GetRelationDirective(f.Definition)
}
// Table directive on field, if it exists
func (f Field) Table() *TableDirective {
d := f.TypeDefinition.Directives.ForName("table")
if d == nil {
return nil
}
return &TableDirective{
Name: GetArgumentValue(d.Arguments, "name"),
Schema: GetArgumentValue(d.Arguments, "schema"),
Dialect: GetArgumentValue(d.Arguments, "dialect"),
}
}
func GetFilterInput(s *ast.Schema, f *ast.Definition) *ast.Definition {
return s.Types[fmt.Sprintf("%sFilterInput", f.Name)]
}
func GetOperationType(ctx context.Context) OperationType {
opCtx := graphql.GetOperationContext(ctx)
if opCtx.Operation.Operation == "mutation" {
sel := opCtx.Operation.SelectionSet[0]
field := sel.(*ast.Field)
switch {
case strings.HasPrefix(field.Name, "delete"):
return DeleteOperation
case strings.HasPrefix(field.Name, "create"):
return InsertOperation
case strings.HasPrefix(field.Name, "update"):
return UpdateOperation
}
return UnknownOperation
}
return QueryOperation
}
func GetAggregateField(parentField, aggField Field) Field {
fieldName := strings.Split(aggField.Name, "Aggregate")[0][1:]
f, _ := parentField.ForName(fieldName)
return f
}
func CollectOrdering(ordering interface{}) ([]OrderField, error) {
switch orderings := ordering.(type) {
case map[string]interface{}:
return buildOrderingHelper(orderings), nil
case []interface{}:
var orderFields []OrderField
for _, o := range orderings {
argMap, ok := o.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid value type")
}
orderFields = append(orderFields, buildOrderingHelper(argMap)...)
}
return orderFields, nil
default:
panic(fmt.Sprintf("unknown ordering type %v", orderings))
}
}
func CollectFields(ctx context.Context, schema *ast.Schema) Field {
resCtx := graphql.GetFieldContext(ctx)
opCtx := graphql.GetOperationContext(ctx)
filterCtx := GetFieldFilterContext(ctx)
args := resCtx.Field.ArgumentMap(graphql.GetOperationContext(ctx).Variables)
if filterCtx != nil {
if _, ok := args["filter"]; !ok {
args["filter"] = filterCtx.Filters
} else {
for k, v := range filterCtx.Filters {
args["filter"].(map[string]interface{})[k] = v
}
}
}
f := NewField(nil, resCtx.Field.Field, schema, args)
f.Selections = collectFields(&f, schema, opCtx, make(map[string]bool))
return f
}
func CollectFromQuery(field *ast.Field, schema *ast.Schema, opCtx *graphql.OperationContext, args map[string]interface{}) Field {
f := NewField(nil, field, schema, args)
f.Selections = collectFields(&f, schema, opCtx, make(map[string]bool))
return f
}
func getTypeName(f *ast.Field) string {
typeName := f.Definition.Type.Name()
if strings.HasSuffix(f.Name, "Aggregate") {
originalFieldName := strings.Split(f.Name, "Aggregate")[0][1:]
typeName = f.ObjectDefinition.Fields.ForName(originalFieldName).Type.Name()
}
return typeName
}
func collectFields(parent *Field, schema *ast.Schema, opCtx *graphql.OperationContext, visited map[string]bool) []Field {
groupedFields := make([]Field, 0)
for _, sel := range parent.Field.SelectionSet {
switch sel := sel.(type) {
case *ast.Field:
if !shouldIncludeNode(sel.Directives, opCtx.Variables) {
continue
}
selField := getOrCreateAndAppendField(&groupedFields, sel.Alias, sel.ObjectDefinition, func() Field {
return NewField(parent, sel, schema, resolveArguments(sel, opCtx.Variables))
})
// Add filter fields for relation form different provider, so they are returned by builder query
if selField.FieldType == TypeRelation && selField.Table().Dialect != parent.Table().Dialect {
for _, relationField := range selField.Relation().Fields {
groupedFields = append(groupedFields, Field{
Field: &ast.Field{
Name: strcase.ToLowerCamel(relationField),
ObjectDefinition: parent.ObjectDefinition,
},
FieldType: TypeScalar,
Parent: parent,
})
}
// No need to add the original selection as it exists in a different source
continue
}
if selField.Field.SelectionSet != nil {
// Add any sub selections of this field
selField.Selections = append(selField.Selections, collectFields(selField, schema, opCtx, map[string]bool{})...)
}
case *ast.InlineFragment:
if !shouldIncludeNode(sel.Directives, opCtx.Variables) {
continue
}
for _, childField := range collectFields(parent, schema, opCtx, visited) {
f := getOrCreateAndAppendField(&groupedFields, childField.Name, childField.ObjectDefinition, func() Field { return childField })
f.Selections = append(f.Selections, childField.Selections...)
}
case *ast.FragmentSpread:
if !shouldIncludeNode(sel.Directives, opCtx.Variables) {
continue
}
fragmentName := sel.Name
if _, seen := visited[fragmentName]; seen {
continue
}
visited[fragmentName] = true
fragment := opCtx.Doc.Fragments.ForName(fragmentName)
if fragment == nil {
// should never happen, validator has already run
panic(fmt.Errorf("missing fragment %s", fragmentName))
}
for _, childField := range collectFields(parent, schema, opCtx, visited) {
f := getOrCreateAndAppendField(&groupedFields, childField.Name, childField.ObjectDefinition, func() Field { return childField })
f.Selections = append(f.Selections, childField.Selections...)
}
default:
panic(fmt.Errorf("unsupported %T", sel))
}
}
return groupedFields
}
func getOrCreateAndAppendField(c *[]Field, name string, objectDefinition *ast.Definition, creator func() Field) *Field {
for i, cf := range *c {
if cf.Alias == name && (cf.ObjectDefinition == objectDefinition || (cf.ObjectDefinition != nil && objectDefinition != nil && cf.ObjectDefinition.Name == objectDefinition.Name)) {
return &(*c)[i]
}
}
f := creator()
*c = append(*c, f)
return &(*c)[len(*c)-1]
}
func shouldIncludeNode(directives ast.DirectiveList, variables map[string]interface{}) bool {
if len(directives) == 0 {
return true
}
skip, include := false, true
if d := directives.ForName("skip"); d != nil {
skip = resolveIfArgument(d, variables)
}
if d := directives.ForName("include"); d != nil {
include = resolveIfArgument(d, variables)
}
return !skip && include
}
func resolveIfArgument(d *ast.Directive, variables map[string]interface{}) bool {
arg := d.Arguments.ForName("if")
if arg == nil {
panic(fmt.Sprintf("%s: argument 'if' not defined", d.Name))
}
value, err := arg.Value.Value(variables)
if err != nil {
panic(err)
}
ret, ok := value.(bool)
if !ok {
panic(fmt.Sprintf("%s: argument 'if' is not a boolean", d.Name))
}
return ret
}
func resolveArguments(f *ast.Field, variables map[string]interface{}) map[string]interface{} {
if f.Definition == nil {
return variables
}
return f.ArgumentMap(variables)
}
func buildOrderingHelper(argMap map[string]interface{}) []OrderField {
orderFields := make([]OrderField, 0)
for k, v := range argMap {
orderFields = append(orderFields, OrderField{
Key: k,
Type: OrderingTypes(cast.ToString(v)),
})
}
return orderFields
}
// parseFieldType returns the fieldType based on the name/directive or type of the *ast.Field
func parseFieldType(field *ast.Field, typeDef *ast.Definition) fieldType {
switch {
case strings.HasSuffix(field.Name, "Aggregate"):
return TypeAggregate
case typeDef.IsCompositeType():
if d := field.Definition.Directives.ForName("relation"); d != nil {
return TypeRelation
}
return TypeObject
default:
return TypeScalar
}
}