-
Notifications
You must be signed in to change notification settings - Fork 12
/
registry.go
360 lines (318 loc) · 8.93 KB
/
registry.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
package tools
import (
"context"
"errors"
"fmt"
"strings"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
)
var errUnresolvedDependencies = errors.New("unresolved dependencies")
// registry the registry holds all of the types
type registry struct {
ctx context.Context
types map[string]graphql.Type
directives map[string]*graphql.Directive
schema *graphql.Schema
resolverMap resolverMap
directiveMap SchemaDirectiveVisitorMap
schemaDirectives []*ast.Directive
document *ast.Document
extensions []graphql.Extension
unresolvedDefs []ast.Node
maxIterations int
iterations int
dependencyMap DependencyMap
}
// newRegistry creates a new registry
func newRegistry(
ctx context.Context,
resolvers map[string]interface{},
directiveMap SchemaDirectiveVisitorMap,
extensions []graphql.Extension,
document *ast.Document,
) (*registry, error) {
if ctx == nil {
ctx = context.Background()
}
r := ®istry{
ctx: ctx,
types: map[string]graphql.Type{
"ID": graphql.ID,
"String": graphql.String,
"Int": graphql.Int,
"Float": graphql.Float,
"Boolean": graphql.Boolean,
"DateTime": graphql.DateTime,
},
directives: map[string]*graphql.Directive{
"include": graphql.IncludeDirective,
"skip": graphql.SkipDirective,
"deprecated": graphql.DeprecatedDirective,
"hide": HideDirective,
},
resolverMap: resolverMap{},
directiveMap: directiveMap,
schemaDirectives: []*ast.Directive{},
document: document,
extensions: extensions,
unresolvedDefs: document.Definitions,
iterations: 0,
maxIterations: len(document.Definitions),
}
// import each resolver to the correct location
for name, resolver := range resolvers {
if err := r.importResolver(name, resolver); err != nil {
return nil, err
}
}
return r, nil
}
// looks up a resolver by name or returns nil
func (c *registry) getResolver(name string) Resolver {
if c.resolverMap != nil {
if resolver, ok := c.resolverMap[name]; ok {
return resolver
}
}
return nil
}
// gets an object from the registry
func (c *registry) getObject(name string) (*graphql.Object, error) {
obj, err := c.getType(name)
if err != nil {
return nil, err
}
switch o := obj.(type) {
case *graphql.Object:
return o, nil
}
return nil, nil
}
// converts the type map to an array
func (c *registry) typeArray() []graphql.Type {
a := make([]graphql.Type, 0)
for _, t := range c.types {
a = append(a, t)
}
return a
}
// Get gets a type from the registry
func (c *registry) getType(name string) (graphql.Type, error) {
if val, ok := c.types[name]; ok {
return val, nil
}
if !c.willResolve(name) {
return nil, fmt.Errorf("no definition found for type %q", name)
}
return nil, errUnresolvedDependencies
}
// Get gets a directive from the registry
func (c *registry) getDirective(name string) (*graphql.Directive, error) {
if val, ok := c.directives[name]; ok {
return val, nil
}
return nil, errUnresolvedDependencies
}
// gets the extensions for the current type
func (c *registry) getExtensions(name, kind string) []*ast.ObjectDefinition {
extensions := []*ast.ObjectDefinition{}
for _, def := range c.document.Definitions {
if def.GetKind() == kinds.TypeExtensionDefinition {
extDef := def.(*ast.TypeExtensionDefinition).Definition
if extDef.Name.Value == name && extDef.GetKind() == kind {
extensions = append(extensions, extDef)
}
}
}
return extensions
}
// imports a resolver from an interface
func (c *registry) importResolver(name string, resolver interface{}) error {
switch res := resolver.(type) {
case *graphql.Directive:
// allow @ to be prefixed to a directive in the event there is a type with the same
// name to allow both to be defined in the resolver map but strip it from the
// directive before adding it to the registry
name = strings.TrimLeft(name, "@")
if _, ok := c.directives[name]; !ok {
c.directives[name] = res
}
case *graphql.InputObject:
if _, ok := c.types[name]; !ok {
c.types[name] = res
}
case *graphql.Scalar:
if _, ok := c.types[name]; !ok {
c.types[name] = res
}
case *graphql.Enum:
if _, ok := c.types[name]; !ok {
c.types[name] = res
}
case *graphql.Object:
if _, ok := c.types[name]; !ok {
c.types[name] = res
}
case *graphql.Interface:
if _, ok := c.types[name]; !ok {
c.types[name] = res
}
case *graphql.Union:
if _, ok := c.types[name]; !ok {
c.types[name] = res
}
case *ScalarResolver:
if _, ok := c.resolverMap[name]; !ok {
c.resolverMap[name] = res
}
case *EnumResolver:
if _, ok := c.resolverMap[name]; !ok {
c.resolverMap[name] = res
}
case *ObjectResolver:
if _, ok := c.resolverMap[name]; !ok {
c.resolverMap[name] = res
}
case *InterfaceResolver:
if _, ok := c.resolverMap[name]; !ok {
c.resolverMap[name] = res
}
case *UnionResolver:
if _, ok := c.resolverMap[name]; !ok {
c.resolverMap[name] = res
}
default:
return fmt.Errorf("invalid resolver type for %s", name)
}
return nil
}
func getNodeName(node ast.Node) string {
switch node.GetKind() {
case kinds.ObjectDefinition:
return node.(*ast.ObjectDefinition).Name.Value
case kinds.ScalarDefinition:
return node.(*ast.ScalarDefinition).Name.Value
case kinds.EnumDefinition:
return node.(*ast.EnumDefinition).Name.Value
case kinds.InputObjectDefinition:
return node.(*ast.InputObjectDefinition).Name.Value
case kinds.InterfaceDefinition:
return node.(*ast.InterfaceDefinition).Name.Value
case kinds.UnionDefinition:
return node.(*ast.UnionDefinition).Name.Value
case kinds.DirectiveDefinition:
return node.(*ast.DirectiveDefinition).Name.Value
}
return ""
}
// determines if a node will resolve eventually or with a thunk
// false if there is no possibility
func (c *registry) willResolve(name string) bool {
if _, ok := c.types[name]; ok {
return true
}
for _, n := range c.unresolvedDefs {
if getNodeName(n) == name {
return true
}
}
return false
}
// iteratively resolves dependencies until all types are resolved
func (c *registry) resolveDefinitions() error {
unresolved := []ast.Node{}
for len(c.unresolvedDefs) > 0 && c.iterations < c.maxIterations {
c.iterations = c.iterations + 1
for _, definition := range c.unresolvedDefs {
switch nodeKind := definition.GetKind(); nodeKind {
case kinds.DirectiveDefinition:
if err := c.buildDirectiveFromAST(definition.(*ast.DirectiveDefinition)); err != nil {
if err == errUnresolvedDependencies {
unresolved = append(unresolved, definition)
} else {
return err
}
}
case kinds.ScalarDefinition:
if err := c.buildScalarFromAST(definition.(*ast.ScalarDefinition)); err != nil {
if err == errUnresolvedDependencies {
unresolved = append(unresolved, definition)
} else {
return err
}
}
case kinds.EnumDefinition:
if err := c.buildEnumFromAST(definition.(*ast.EnumDefinition)); err != nil {
if err == errUnresolvedDependencies {
unresolved = append(unresolved, definition)
} else {
return err
}
}
case kinds.InputObjectDefinition:
if err := c.buildInputObjectFromAST(definition.(*ast.InputObjectDefinition)); err != nil {
if err == errUnresolvedDependencies {
unresolved = append(unresolved, definition)
} else {
return err
}
}
case kinds.ObjectDefinition:
if err := c.buildObjectFromAST(definition.(*ast.ObjectDefinition)); err != nil {
if err == errUnresolvedDependencies {
unresolved = append(unresolved, definition)
} else {
return err
}
}
case kinds.InterfaceDefinition:
if err := c.buildInterfaceFromAST(definition.(*ast.InterfaceDefinition)); err != nil {
if err == errUnresolvedDependencies {
unresolved = append(unresolved, definition)
} else {
return err
}
}
case kinds.UnionDefinition:
if err := c.buildUnionFromAST(definition.(*ast.UnionDefinition)); err != nil {
if err == errUnresolvedDependencies {
unresolved = append(unresolved, definition)
} else {
return err
}
}
case kinds.SchemaDefinition:
if err := c.buildSchemaFromAST(definition.(*ast.SchemaDefinition)); err != nil {
if err == errUnresolvedDependencies {
unresolved = append(unresolved, definition)
} else {
return err
}
}
}
}
// check if everything has been resolved
if len(unresolved) == 0 {
return nil
}
// prepare the next loop
c.unresolvedDefs = unresolved
if c.iterations < c.maxIterations {
unresolved = []ast.Node{}
}
}
if len(unresolved) > 0 {
names := []string{}
for _, n := range unresolved {
if name := getNodeName(n); name != "" {
names = append(names, name)
} else {
names = append(names, n.GetKind())
}
}
return fmt.Errorf("failed to resolve all type definitions: %v", names)
}
return nil
}