-
Notifications
You must be signed in to change notification settings - Fork 12
/
helpers.go
396 lines (355 loc) · 8.49 KB
/
helpers.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
package tools
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
)
// gets the field resolve function for a field
func (c *registry) getFieldResolveFn(kind, typeName, fieldName string) graphql.FieldResolveFn {
if r := c.getResolver(typeName); r != nil && kind == r.getKind() {
switch kind {
case kinds.ObjectDefinition:
if fn, ok := r.(*ObjectResolver).Fields[fieldName]; ok {
return fn.Resolve
}
case kinds.InterfaceDefinition:
if fn, ok := r.(*InterfaceResolver).Fields[fieldName]; ok {
return fn.Resolve
}
}
}
return graphql.DefaultResolveFn
}
func (c *registry) getFieldSubscribeFn(kind, typeName, fieldName string) graphql.FieldResolveFn {
if r := c.getResolver(typeName); r != nil && kind == r.getKind() {
switch kind {
case kinds.ObjectDefinition:
if fieldResolve, ok := r.(*ObjectResolver).Fields[fieldName]; ok {
return fieldResolve.Subscribe
}
case kinds.InterfaceDefinition:
if fieldResolve, ok := r.(*InterfaceResolver).Fields[fieldName]; ok {
return fieldResolve.Subscribe
}
}
}
return nil
}
// Recursively builds a complex type
func (c *registry) buildComplexType(astType ast.Type) (graphql.Type, error) {
switch kind := astType.GetKind(); kind {
case kinds.List:
t, err := c.buildComplexType(astType.(*ast.List).Type)
if err != nil {
return nil, err
}
return graphql.NewList(t), nil
case kinds.NonNull:
t, err := c.buildComplexType(astType.(*ast.NonNull).Type)
if err != nil {
return nil, err
}
return graphql.NewNonNull(t), nil
case kinds.Named:
t := astType.(*ast.Named)
return c.getType(t.Name.Value)
}
return nil, fmt.Errorf("invalid kind")
}
// gets the description or defaults to an empty string
func getDescription(node ast.DescribableNode) string {
if desc := node.GetDescription(); desc != nil {
return desc.Value
}
return ""
}
func parseDefaultValue(inputType ast.Type, value interface{}) (interface{}, error) {
if value == nil {
return nil, nil
}
switch t := inputType.(type) {
// non-null call parse on type
case *ast.NonNull:
return parseDefaultValue(t.Type, value)
// list parse each item in the list
case *ast.List:
switch a := value.(type) {
case []ast.Value:
arr := []interface{}{}
for _, v := range a {
val, err := parseDefaultValue(t.Type, v.GetValue())
if err != nil {
return nil, err
}
arr = append(arr, val)
}
return arr, nil
}
// parse the specific type
case *ast.Named:
switch t.Name.Value {
case "Int":
value = graphql.Int.ParseValue(value)
case "Float":
value = graphql.Float.ParseValue(value)
case "Boolean":
value = graphql.Boolean.ParseValue(value)
case "ID":
value = graphql.ID.ParseValue(value)
case "String":
value = graphql.String.ParseValue(value)
}
}
return value, nil
}
// gets the default value or defaults to nil
func getDefaultValue(input *ast.InputValueDefinition) (interface{}, error) {
if input.DefaultValue == nil {
return nil, nil
}
defaultValue, err := parseDefaultValue(input.Type, input.DefaultValue.GetValue())
if err != nil {
return nil, err
}
return defaultValue, err
}
// ReadSourceFiles reads all source files from a specified path
func ReadSourceFiles(p string, recursive ...bool) (string, error) {
typeDefs := []string{}
abs, err := filepath.Abs(p)
if err != nil {
return "", err
}
var readFunc = func(p string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
switch ext := strings.ToLower(filepath.Ext(info.Name())); ext {
case ".gql", ".graphql":
data, err := ioutil.ReadFile(p)
if err != nil {
return err
}
typeDefs = append(typeDefs, string(data))
return nil
default:
return nil
}
}
if len(recursive) > 0 && recursive[0] {
if err := filepath.Walk(abs, readFunc); err != nil {
return "", err
}
} else {
files, err := ioutil.ReadDir(abs)
if err != nil {
return "", err
}
for _, file := range files {
if err := readFunc(abs, file, nil); err != nil {
return "", err
}
}
}
result := strings.Join(typeDefs, "\n")
return result, err
}
// UnaliasedPathArray gets the path array for a resolve function without aliases
func UnaliasedPathArray(info graphql.ResolveInfo) []interface{} {
return unaliasedPathArray(info.Operation.GetSelectionSet(), info.Path.AsArray(), []interface{}{})
}
// gets the actual field path for a selection by removing aliases
func unaliasedPathArray(set *ast.SelectionSet, remaining []interface{}, current []interface{}) []interface{} {
if len(remaining) == 0 {
return current
}
for _, sel := range set.Selections {
switch field := sel.(type) {
case *ast.Field:
if field.Alias != nil && field.Alias.Value == remaining[0] {
return unaliasedPathArray(sel.GetSelectionSet(), remaining[1:], append(current, field.Name.Value))
} else if field.Name.Value == remaining[0] {
return unaliasedPathArray(sel.GetSelectionSet(), remaining[1:], append(current, field.Name.Value))
}
}
}
return current
}
// GetPathFieldSubSelections gets the subselectiond for a path
func GetPathFieldSubSelections(info graphql.ResolveInfo, field ...string) (names []string, err error) {
names = []string{}
if len(info.FieldASTs) == 0 {
return
}
fieldAST := info.FieldASTs[0]
if fieldAST.GetSelectionSet() == nil {
return
}
// get any sub selections
for _, f := range field {
for _, sel := range fieldAST.GetSelectionSet().Selections {
switch fragment := sel.(type) {
case *ast.InlineFragment:
for _, ss := range fragment.GetSelectionSet().Selections {
switch subField := ss.(type) {
case *ast.Field:
if subField.Name.Value == f {
fieldAST = subField
break
}
}
}
case *ast.Field:
subField := sel.(*ast.Field)
if subField.Name.Value == f {
fieldAST = subField
continue
}
}
}
}
for _, sel := range fieldAST.GetSelectionSet().Selections {
switch fragment := sel.(type) {
case *ast.InlineFragment:
for _, ss := range fragment.GetSelectionSet().Selections {
switch field := ss.(type) {
case *ast.Field:
names = append(names, field.Name.Value)
}
}
case *ast.Field:
field := sel.(*ast.Field)
names = append(names, field.Name.Value)
}
}
return
}
// determines if a field is hidden
func isHiddenField(field *ast.FieldDefinition) bool {
hide := false
for _, dir := range field.Directives {
if dir.Name.Value == directiveHide {
return true
}
}
return hide
}
// Merges object definitions
func MergeExtensions(obj *ast.ObjectDefinition, extensions ...*ast.ObjectDefinition) *ast.ObjectDefinition {
merged := &ast.ObjectDefinition{
Kind: obj.Kind,
Loc: obj.Loc,
Name: obj.Name,
Description: obj.Description,
Interfaces: append([]*ast.Named{}, obj.Interfaces...),
Directives: append([]*ast.Directive{}, obj.Directives...),
Fields: append([]*ast.FieldDefinition{}, obj.Fields...),
}
for _, ext := range extensions {
merged.Interfaces = append(merged.Interfaces, ext.Interfaces...)
merged.Directives = append(merged.Directives, ext.Directives...)
merged.Fields = append(merged.Fields, ext.Fields...)
}
return merged
}
const IntrospectionQuery = `query IntrospectionQuery {
__schema {
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type {
...TypeRef
}
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}`