forked from playlyfe/go-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
457 lines (432 loc) · 13.5 KB
/
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
448
449
450
451
452
453
454
455
456
457
package graphql
import (
"fmt"
"sort"
. "github.com/playlyfe/go-graphql/language"
)
type Schema struct {
Document *Document
QueryRoot *ObjectTypeDefinition
MutationRoot *ObjectTypeDefinition
}
const INTROSPECTION_SCHEMA = `
scalar String
scalar Boolean
scalar Int
scalar Float
scalar ID
type __Schema {
types: [__Type!]!
queryType: __Type!
mutationType: __Type
directives: [__Directive!]!
}
type __Type {
kind: __TypeKind!
name: String
description: String
# OBJECT and INTERFACE only
fields(includeDeprecated: Boolean = false): [__Field!]
# OBJECT only
interfaces: [__Type!]
# INTERFACE and UNION only
possibleTypes: [__Type!]
# ENUM only
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
# INPUT_OBJECT only
inputFields: [__InputValue!]
# NON_NULL and LIST only
ofType: __Type
}
type __Field {
name: String!
description: String
args: [__InputValue!]!
type: __Type!
isDeprecated: Boolean!
deprecationReason: String
}
type __InputValue {
name: String!
description: String
type: __Type!
defaultValue: String
}
type __EnumValue {
name: String!
description: String
isDeprecated: Boolean!
deprecationReason: String
}
enum __TypeKind {
SCALAR
OBJECT
INTERFACE
UNION
ENUM
INPUT_OBJECT
LIST
NON_NULL
}
type __Directive {
name: String!
description: String
args: [__InputValue!]!
onOperation: Boolean!
onFragment: Boolean!
onField: Boolean!
}
`
func (executor *Executor) introspectType(params *ResolveParams, typeValue interface{}, d ...int) map[string]interface{} {
depth := 0
if len(d) > 0 {
depth = d[0]
}
depth += 1
if depth > 10 {
return nil
}
schema := executor.Schema.Document
typeIndex := schema.TypeIndex
switch ttype := typeValue.(type) {
case *NonNullType:
return map[string]interface{}{
"kind": "NON_NULL",
"ofType": executor.introspectType(params, ttype.Type, depth),
}
case *ListType:
return map[string]interface{}{
"kind": "LIST",
"ofType": executor.introspectType(params, ttype.Type, depth),
}
case *NamedType:
return executor.introspectType(params, ttype.Name.Value, depth)
case string:
typeInfo := map[string]interface{}{
"name": ttype,
}
typeValue := typeIndex[ttype]
switch __type := typeValue.(type) {
case *ScalarTypeDefinition:
typeInfo["kind"] = "SCALAR"
typeInfo["description"] = __type.Description
case *ObjectTypeDefinition:
typeInfo["kind"] = "OBJECT"
typeInfo["description"] = __type.Description
case *InputObjectTypeDefinition:
typeInfo["kind"] = "INPUT_OBJECT"
typeInfo["description"] = __type.Description
inputFields := []map[string]interface{}{}
for _, inputValueDefinition := range __type.Fields {
defaultValue, err := executor.valueFromAST(params.Context, inputValueDefinition.DefaultValue, executor.resolveNamedType(inputValueDefinition.Type), nil, nil)
if err != nil {
panic(err)
}
inputFields = append(inputFields, map[string]interface{}{
"name": inputValueDefinition.Name.Value,
"description": inputValueDefinition.Description,
"type": executor.introspectType(params, inputValueDefinition.Type, depth),
"defaultValue": defaultValue,
})
}
typeInfo["inputFields"] = inputFields
case *InterfaceTypeDefinition:
typeInfo["kind"] = "INTERFACE"
typeInfo["description"] = __type.Description
possibleTypes := []map[string]interface{}{}
for _, objectType := range schema.PossibleTypesIndex[__type.Name.Value] {
possibleTypes = append(possibleTypes, executor.introspectType(params, objectType.Name.Value, depth))
}
typeInfo["possibleTypes"] = possibleTypes
case *UnionTypeDefinition:
typeInfo["kind"] = "UNION"
typeInfo["description"] = __type.Description
possibleTypes := []map[string]interface{}{}
for _, objectType := range schema.PossibleTypesIndex[__type.Name.Value] {
possibleTypes = append(possibleTypes, executor.introspectType(params, objectType.Name.Value, depth))
}
typeInfo["possibleTypes"] = possibleTypes
case *EnumTypeDefinition:
typeInfo["kind"] = "ENUM"
typeInfo["description"] = __type.Description
default:
panic(fmt.Sprintf("Unknown Type %s", ttype))
}
return typeInfo
default:
return nil
}
}
func typenameResolver(typename string) func(params *ResolveParams) (interface{}, error) {
return func(params *ResolveParams) (interface{}, error) {
return typename, nil
}
}
func NewSchema(schemaDefinition string, queryRoot string, mutationRoot string) (*Schema, map[string]interface{}, error) {
parser := &Parser{}
schema := &Schema{}
resolvers := map[string]interface{}{}
ast, err := parser.Parse(&ParseParams{
Source: schemaDefinition + INTROSPECTION_SCHEMA,
})
if err != nil {
return nil, nil, err
}
for _, definition := range ast.Definitions {
switch operationDefinition := definition.(type) {
case *ObjectTypeDefinition:
if operationDefinition.Name.Value == queryRoot {
schema.QueryRoot = operationDefinition
} else if operationDefinition.Name.Value == mutationRoot {
schema.MutationRoot = operationDefinition
}
resolvers[operationDefinition.Name.Value+"/__typename"] = typenameResolver(operationDefinition.Name.Value)
}
}
if schema.QueryRoot == nil {
return nil, nil, &GraphQLError{
Message: "The QueryRoot could not be found",
}
}
// Add implict fields to query root
schemaField := &FieldDefinition{
Name: &Name{
Value: "__schema",
},
Description: "The GraphQL schema",
Type: &NonNullType{
Type: &NamedType{
Name: &Name{
Value: "__Schema",
},
},
},
Arguments: []*InputValueDefinition{},
ArgumentIndex: map[string]*InputValueDefinition{},
}
nameArgument := &InputValueDefinition{
Name: &Name{
Value: "name",
},
Description: "The name of the Type being inspected",
Type: &NonNullType{
Type: &NamedType{
Name: &Name{
Value: "String",
},
},
},
}
typeField := &FieldDefinition{
Name: &Name{
Value: "__type",
},
Description: "GraphQL Type introspection information",
Arguments: []*InputValueDefinition{
nameArgument,
},
ArgumentIndex: map[string]*InputValueDefinition{
"name": nameArgument,
},
Type: &NamedType{
Name: &Name{
Value: "__Type",
},
},
}
schema.QueryRoot.FieldIndex["__schema"] = schemaField
schema.QueryRoot.Fields = append(schema.QueryRoot.Fields, schemaField)
schema.QueryRoot.FieldIndex["__type"] = typeField
schema.QueryRoot.Fields = append(schema.QueryRoot.Fields, typeField)
resolvers[queryRoot+"/__schema"] = func(params *ResolveParams) (interface{}, error) {
executor := params.Executor
result := map[string]interface{}{
"queryType": executor.introspectType(params, queryRoot),
"directives": []map[string]interface{}{
{
"name": "skip",
"description": "Conditionally exclude a field or fragment during execution",
"args": []map[string]interface{}{
{
"name": "if",
"description": "The condition value",
"type": map[string]interface{}{
"kind": "NON_NULL",
"ofType": map[string]interface{}{
"kind": "SCALAR",
"name": "Boolean",
"description": "A boolean value representing `true` or `false`",
},
},
},
},
"onOperation": false,
"onField": true,
"onFragment": true,
},
{
"name": "include",
"description": "Conditionally include a field or fragment during execution",
"args": []map[string]interface{}{
{
"name": "if",
"description": "The condition value",
"type": map[string]interface{}{
"kind": "NON_NULL",
"ofType": map[string]interface{}{
"kind": "SCALAR",
"name": "Boolean",
"description": "A boolean value representing `true` or `false`",
},
},
},
},
"onOperation": false,
"onField": true,
"onFragment": true,
},
},
}
//TODO: better handling for empty mutationRoot
if schema.MutationRoot != nil {
result["mutationType"] = executor.introspectType(params, mutationRoot)
}
return result, nil
}
resolvers["__Schema/types"] = func(params *ResolveParams) (interface{}, error) {
types := []map[string]interface{}{}
typeNames := []string{}
for typeName, _ := range params.Schema.TypeIndex {
typeNames = append(typeNames, typeName)
}
sort.Strings(typeNames)
for _, typeName := range typeNames {
if typeName == "__Schema" || typeName == "__Type" || typeName == "__Field" || typeName == "__InputValue" || typeName == "__EnumValue" || typeName == "__TypeKind" || typeName == "__Directive" {
continue
}
types = append(types, params.Executor.introspectType(params, typeName))
}
return types, nil
}
resolvers[queryRoot+"/__type"] = func(params *ResolveParams) (interface{}, error) {
return params.Executor.introspectType(params, params.Args["name"]), nil
}
resolvers["__Type/fields"] = func(params *ResolveParams) (interface{}, error) {
executor := params.Executor
if typeInfo, ok := params.Source.(map[string]interface{}); ok {
typeName := typeInfo["name"].(string)
if typeInfo["kind"] == "OBJECT" {
__type := params.Schema.TypeIndex[typeName].(*ObjectTypeDefinition)
fields := []map[string]interface{}{}
includeDeprecated := params.Args["includeDeprecated"].(bool)
for _, fieldDefinition := range __type.Fields {
if !includeDeprecated && fieldDefinition.IsDeprecated {
continue
}
if typeName == queryRoot {
fieldName := fieldDefinition.Name.Value
if fieldName == "__schema" || fieldName == "__type" {
continue
}
}
args := []map[string]interface{}{}
for _, inputValueDefinition := range fieldDefinition.Arguments {
defaultValue, err := executor.valueFromAST(params.Context, inputValueDefinition.DefaultValue, executor.resolveNamedType(inputValueDefinition.Type), nil, nil)
if err != nil {
return nil, err
}
args = append(args, map[string]interface{}{
"name": inputValueDefinition.Name.Value,
"description": inputValueDefinition.Description,
"type": executor.introspectType(params, inputValueDefinition.Type),
"defaultValue": defaultValue,
})
}
fields = append(fields, map[string]interface{}{
"name": fieldDefinition.Name.Value,
"description": fieldDefinition.Description,
"args": args,
"type": executor.introspectType(params, fieldDefinition.Type),
"isDeprecated": fieldDefinition.IsDeprecated,
"deprecationReason": fieldDefinition.DeprecationReason,
})
}
return fields, nil
} else if typeInfo["kind"] == "INTERFACE" {
__type := params.Schema.TypeIndex[typeName].(*InterfaceTypeDefinition)
fields := []map[string]interface{}{}
includeDeprecated := params.Args["includeDeprecated"].(bool)
for _, fieldDefinition := range __type.Fields {
if !includeDeprecated && fieldDefinition.IsDeprecated {
continue
}
args := []map[string]interface{}{}
for _, inputValueDefinition := range fieldDefinition.Arguments {
defaultValue, err := executor.valueFromAST(params.Context, inputValueDefinition.DefaultValue, executor.resolveNamedType(inputValueDefinition.Type), nil, nil)
if err != nil {
return nil, err
}
args = append(args, map[string]interface{}{
"name": inputValueDefinition.Name.Value,
"description": inputValueDefinition.Description,
"type": executor.introspectType(params, inputValueDefinition.Type),
"defaultValue": defaultValue,
})
}
fields = append(fields, map[string]interface{}{
"name": fieldDefinition.Name.Value,
"description": fieldDefinition.Description,
"args": args,
"type": executor.introspectType(params, fieldDefinition.Type),
"isDeprecated": fieldDefinition.IsDeprecated,
"deprecationReason": fieldDefinition.DeprecationReason,
})
}
return fields, nil
}
}
return nil, nil
}
resolvers["__Type/interfaces"] = func(params *ResolveParams) (interface{}, error) {
if typeInfo, ok := params.Source.(map[string]interface{}); ok {
if typeName, ok := typeInfo["name"].(string); ok {
__type := params.Schema.ObjectTypeIndex[typeName]
if __type != nil {
interfaceTypes := []map[string]interface{}{}
if __type.Interfaces != nil {
for _, namedType := range __type.Interfaces {
interfaceTypes = append(interfaceTypes, params.Executor.introspectType(params, namedType.Name.Value))
}
}
return interfaceTypes, nil
}
}
}
return nil, nil
}
resolvers["__Type/enumValues"] = func(params *ResolveParams) (interface{}, error) {
if typeInfo, ok := params.Source.(map[string]interface{}); ok {
typeName := typeInfo["name"].(string)
if typeInfo["kind"] == "ENUM" {
__type := params.Schema.TypeIndex[typeName].(*EnumTypeDefinition)
includeDeprecated := params.Args["includeDeprecated"].(bool)
enumValues := []map[string]interface{}{}
for _, enumValue := range __type.Values {
if !includeDeprecated && enumValue.IsDeprecated {
continue
}
enumValues = append(enumValues, map[string]interface{}{
"name": enumValue.Name.Value,
"description": enumValue.Description,
"isDeprecated": enumValue.IsDeprecated,
"deprecationReason": enumValue.DeprecationReason,
})
}
return enumValues, nil
}
}
return nil, nil
}
schema.Document = ast
return schema, resolvers, nil
}