-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinject_schema.go
447 lines (407 loc) · 11.4 KB
/
inject_schema.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package yarql
import (
"log"
"reflect"
"sort"
h "github.com/mjarkk/yarql/helpers"
)
func (s *Schema) injectQLTypes(ctx *parseCtx) {
// Inject __Schema
ref, err := ctx.check(reflect.TypeOf(qlSchema{}), false)
if err != nil {
log.Fatal(err)
}
contents := reflect.ValueOf(s.getQLSchema())
ref.customObjValue = &contents
ref.qlFieldName = []byte("__schema")
ref.hidden = true
s.rootQuery.objContents[getObjKey(ref.qlFieldName)] = ref
// Inject __type(name: String!): __Type
typeResolver := func(ctx *Ctx, args struct{ Name string }) *qlType {
return ctx.schema.getTypeByName(args.Name)
}
typeResolverReflection := reflect.ValueOf(typeResolver)
functionObj, err := ctx.checkStructFieldFunc("__type", typeResolverReflection.Type(), false, -1)
if err != nil {
log.Fatal(err)
}
functionObj.customObjValue = &typeResolverReflection
functionObj.qlFieldName = []byte("__type")
functionObj.hidden = true
s.rootQuery.objContents[getObjKey(functionObj.qlFieldName)] = functionObj
}
func (s *Schema) getQLSchema() qlSchema {
res := qlSchema{
Types: s.getAllQLTypes,
Directives: s.getDirectives(),
QueryType: &qlType{
Kind: typeKindObject,
Name: h.StrPtr(s.rootQuery.typeName),
Description: h.PtrToEmptyStr,
Fields: func(isDeprecatedArgs) []qlField {
fields, ok := s.graphqlObjFields[s.rootQuery.typeName]
if ok {
return fields
}
res := []qlField{}
for _, item := range s.rootQuery.objContents {
if item.hidden {
continue
}
res = append(res, qlField{
Name: string(item.qlFieldName),
Args: s.getObjectArgs(item),
Type: *wrapQLTypeInNonNull(s.objToQLType(item)),
})
}
sort.Slice(res, func(a int, b int) bool { return res[a].Name < res[b].Name })
s.graphqlObjFields[s.rootQuery.typeName] = res
return res
},
Interfaces: []qlType{},
},
MutationType: &qlType{
Kind: typeKindObject,
Name: h.StrPtr(s.rootMethod.typeName),
Description: h.PtrToEmptyStr,
Fields: func(isDeprecatedArgs) []qlField {
fields, ok := s.graphqlObjFields[s.rootMethod.typeName]
if ok {
return fields
}
res := []qlField{}
for _, item := range s.rootMethod.objContents {
if item.hidden {
continue
}
res = append(res, qlField{
Name: string(item.qlFieldName),
Args: s.getObjectArgs(item),
Type: *wrapQLTypeInNonNull(s.objToQLType(item)),
})
}
sort.Slice(res, func(a int, b int) bool { return res[a].Name < res[b].Name })
s.graphqlObjFields[s.rootQuery.typeName] = res
return res
},
Interfaces: []qlType{},
},
}
// TODO: We currently don't support subscriptions
res.SubscriptionType = nil
return res
}
func (s *Schema) getDirectives() []qlDirective {
res := []qlDirective{}
for _, directiveLocation := range s.definedDirectives {
outerLoop:
for _, directive := range directiveLocation {
locations := make([]__DirectiveLocation, len(directive.Where))
for idx, location := range directive.Where {
locations[idx] = location.ToQlDirectiveLocation()
}
for _, entry := range res {
if entry.Name == directive.Name {
for _, directiveLocation := range locations {
for _, entryLocation := range entry.Locations {
if directiveLocation == entryLocation {
continue outerLoop
}
}
}
}
}
res = append(res, qlDirective{
Name: directive.Name,
Description: h.CheckStrPtr(directive.Description),
Locations: locations,
Args: s.getMethodArgs(directive.parsedMethod.inFields),
})
}
}
sort.Slice(res, func(a int, b int) bool { return res[a].Name < res[b].Name })
return res
}
func (s *Schema) getAllQLTypes() []qlType {
if s.graphqlTypesList == nil {
// Only generate s.graphqlTypesList once as the content won't change on runtime
s.graphqlTypesList = make(
[]qlType,
len(s.types)+len(s.inTypes)+len(s.definedEnums)+len(scalars)+len(s.interfaces),
)
idx := 0
for _, qlType := range s.types {
obj, _ := s.objToQLType(qlType)
s.graphqlTypesList[idx] = *obj
idx++
}
for _, in := range s.inTypes {
obj, _ := s.inputToQLType(in)
s.graphqlTypesList[idx] = *obj
idx++
}
for _, enum := range s.definedEnums {
s.graphqlTypesList[idx] = enum.qlType
idx++
}
for _, scalar := range scalars {
s.graphqlTypesList[idx] = scalar
idx++
}
for _, qlInterface := range s.interfaces {
obj, _ := s.objToQLType(qlInterface)
s.graphqlTypesList[idx] = *obj
idx++
}
sort.Slice(s.graphqlTypesList, func(a int, b int) bool { return *s.graphqlTypesList[a].Name < *s.graphqlTypesList[b].Name })
}
return s.graphqlTypesList
}
func (s *Schema) getTypeByName(name string) *qlType {
if s.graphqlTypesMap == nil {
// Build up s.graphqlTypesMap
s.graphqlTypesMap = map[string]qlType{}
all := s.getAllQLTypes()
for _, t := range all {
s.graphqlTypesMap[*t.Name] = t
}
}
t, ok := s.graphqlTypesMap[name]
if ok {
return &t
}
return nil
}
func wrapQLTypeInNonNull(t *qlType, isNonNull bool) *qlType {
if !isNonNull {
return t
}
return &qlType{
Kind: typeKindNonNull,
OfType: t,
}
}
func (s *Schema) inputToQLType(in *input) (res *qlType, isNonNull bool) {
if in.isID {
isNonNull = true
res = &scalarID
return
} else if in.isTime {
isNonNull = true
res = &scalarTime
return
} else if in.isFile {
res = &scalarFile
return
}
switch in.kind {
case reflect.Struct:
isNonNull = true
res = &qlType{
Kind: typeKindInputObject,
Name: h.StrPtr(in.structName),
Description: h.PtrToEmptyStr,
InputFields: func() []qlInputValue {
res := make([]qlInputValue, len(in.structContent))
i := 0
for key, item := range in.structContent {
res[i] = qlInputValue{
Name: key,
Description: h.PtrToEmptyStr,
Type: *wrapQLTypeInNonNull(s.inputToQLType(&item)),
DefaultValue: nil, // We do not support this atm
}
i++
}
sort.Slice(res, func(a int, b int) bool { return res[a].Name < res[b].Name })
return res
},
}
case reflect.Array, reflect.Slice:
res = &qlType{
Kind: typeKindList,
OfType: wrapQLTypeInNonNull(s.inputToQLType(in.elem)),
}
case reflect.Ptr:
// Basically sets the isNonNull to false
res, _ = s.inputToQLType(in.elem)
case reflect.Bool:
isNonNull = true
res = &scalarBoolean
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.UnsafePointer, reflect.Complex64, reflect.Complex128:
isNonNull = true
if in.isID {
res = &scalarID
} else {
res = &scalarInt
}
case reflect.Float32, reflect.Float64:
isNonNull = true
res = &scalarFloat
case reflect.String:
isNonNull = true
res = &scalarString
default:
isNonNull = true
res = &qlType{
Kind: typeKindScalar,
Name: h.PtrToEmptyStr,
Description: h.PtrToEmptyStr,
}
}
return
}
func (s *Schema) getObjectArgs(item *obj) []qlInputValue {
if item.valueType != valueTypeMethod {
return []qlInputValue{}
}
return s.getMethodArgs(item.method.inFields)
}
func (s *Schema) getMethodArgs(inputs map[string]referToInput) []qlInputValue {
res := []qlInputValue{}
for key, value := range inputs {
res = append(res, qlInputValue{
Name: key,
Description: h.PtrToEmptyStr,
Type: *wrapQLTypeInNonNull(s.inputToQLType(&value.input)),
DefaultValue: nil,
})
}
sort.Slice(res, func(a int, b int) bool { return res[a].Name < res[b].Name })
return res
}
func (s *Schema) objToQLType(item *obj) (res *qlType, isNonNull bool) {
switch item.valueType {
case valueTypeUndefined:
// WUT??, we'll just look away and continue as if nothing happened
// FIXME: maybe we should return an error here
return
case valueTypeArray:
res = &qlType{
Kind: typeKindList,
OfType: wrapQLTypeInNonNull(s.objToQLType(item.innerContent)),
}
return
case valueTypeObjRef:
return s.objToQLType(s.types[item.typeName])
case valueTypeObj:
isNonNull = true
interfaces := []qlType{}
if len(item.implementations) != 0 {
for _, implementation := range item.implementations {
interfaceType, _ := s.objToQLType(implementation)
interfaces = append(interfaces, *interfaceType)
}
}
res = &qlType{
Kind: typeKindObject,
Name: &item.typeName,
Description: h.PtrToEmptyStr,
Fields: func(args isDeprecatedArgs) []qlField {
fields, ok := s.graphqlObjFields[item.typeName]
if ok {
return fields
}
res := []qlField{}
for _, innerItem := range item.objContents {
if innerItem.hidden {
continue
}
res = append(res, qlField{
Name: string(innerItem.qlFieldName),
Args: s.getObjectArgs(innerItem),
Type: *wrapQLTypeInNonNull(s.objToQLType(innerItem)),
})
}
sort.Slice(res, func(a int, b int) bool { return res[a].Name < res[b].Name })
s.graphqlObjFields[item.typeName] = res
return res
},
Interfaces: interfaces,
}
return
case valueTypeEnum:
enumType := s.definedEnums[item.enumTypeIndex].qlType
res = &enumType
return res, true
case valueTypePtr:
// This basically sets the isNonNull to false
res, _ := s.objToQLType(item.innerContent)
return res, false
case valueTypeMethod:
res, isNonNull = s.objToQLType(&item.method.outType)
if !item.method.isTypeMethod {
isNonNull = false
}
return
case valueTypeInterfaceRef:
return s.objToQLType(s.interfaces[item.typeName])
case valueTypeInterface:
// A interface should be non null BUT as a interface in go can be nil we set it to false
isNonNull = false
res = &qlType{
Kind: typeKindInterface,
Name: &item.typeName,
Description: h.PtrToEmptyStr,
Interfaces: []qlType{},
PossibleTypes: func() []qlType {
possibleTypes := make([]qlType, len(item.implementations))
for idx, implementation := range item.implementations {
item, _ := s.objToQLType(implementation)
possibleTypes[idx] = *item
}
return possibleTypes
},
Fields: func(args isDeprecatedArgs) []qlField {
fields, ok := s.graphqlObjFields[item.typeName]
if ok {
return fields
}
res := []qlField{}
for _, innerItem := range item.objContents {
if item.hidden {
continue
}
res = append(res, qlField{
Name: string(innerItem.qlFieldName),
Args: s.getObjectArgs(innerItem),
Type: *wrapQLTypeInNonNull(s.objToQLType(innerItem)),
})
}
sort.Slice(res, func(a int, b int) bool { return res[a].Name < res[b].Name })
s.graphqlObjFields[item.typeName] = res
return res
},
}
return
default:
return resolveObjToScalar(item), true
}
}
func resolveObjToScalar(item *obj) *qlType {
var res qlType
switch item.valueType {
case valueTypeData:
if item.isID {
res = scalarID
} else {
switch item.dataValueType {
case reflect.Bool:
res = scalarBoolean
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.UnsafePointer, reflect.Complex64, reflect.Complex128:
res = scalarInt
case reflect.Float32, reflect.Float64:
res = scalarFloat
case reflect.String:
res = scalarString
default:
res = qlType{Kind: typeKindScalar, Name: h.PtrToEmptyStr, Description: h.PtrToEmptyStr}
}
}
return &res
case valueTypeTime:
res = scalarTime
return &res
}
return nil
}