-
Notifications
You must be signed in to change notification settings - Fork 12
/
directives.go
333 lines (303 loc) · 8.98 KB
/
directives.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
package tools
import (
"context"
"fmt"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
)
const (
directiveHide = "hide"
)
// HideDirective hides a define field
var HideDirective = graphql.NewDirective(graphql.DirectiveConfig{
Name: directiveHide,
Description: "Hide a field, useful when generating types from the AST where the backend type has more fields than the graphql type",
Locations: []string{graphql.DirectiveLocationFieldDefinition},
Args: graphql.FieldConfigArgument{},
})
// SchemaDirectiveVisitor defines a schema visitor.
// This attempts to provide similar functionality to Apollo graphql-tools
// https://www.apollographql.com/docs/graphql-tools/schema-directives/
type SchemaDirectiveVisitor struct {
VisitSchema func(p VisitSchemaParams) error
VisitScalar func(p VisitScalarParams) error
VisitObject func(p VisitObjectParams) error
VisitFieldDefinition func(p VisitFieldDefinitionParams) error
VisitArgumentDefinition func(p VisitArgumentDefinitionParams) error
VisitInterface func(p VisitInterfaceParams) error
VisitUnion func(p VisitUnionParams) error
VisitEnum func(p VisitEnumParams) error
VisitEnumValue func(p VisitEnumValueParams) error
VisitInputObject func(p VisitInputObjectParams) error
VisitInputFieldDefinition func(p VisitInputFieldDefinitionParams) error
}
// VisitSchemaParams params
type VisitSchemaParams struct {
Context context.Context
Config *graphql.SchemaConfig
Node *ast.SchemaDefinition
Args map[string]interface{}
}
// VisitScalarParams params
type VisitScalarParams struct {
Context context.Context
Config *graphql.ScalarConfig
Node *ast.ScalarDefinition
Args map[string]interface{}
}
// VisitObjectParams params
type VisitObjectParams struct {
Context context.Context
Config *graphql.ObjectConfig
Node *ast.ObjectDefinition
Extensions []*ast.ObjectDefinition
Args map[string]interface{}
}
// VisitFieldDefinitionParams params
type VisitFieldDefinitionParams struct {
Context context.Context
Config *graphql.Field
Node *ast.FieldDefinition
Args map[string]interface{}
ParentName string
ParentKind string
}
// VisitArgumentDefinitionParams params
type VisitArgumentDefinitionParams struct {
Context context.Context
Config *graphql.ArgumentConfig
Node *ast.InputValueDefinition
Args map[string]interface{}
}
// VisitInterfaceParams params
type VisitInterfaceParams struct {
Context context.Context
Config *graphql.InterfaceConfig
Node *ast.InterfaceDefinition
Args map[string]interface{}
}
// VisitUnionParams params
type VisitUnionParams struct {
Context context.Context
Config *graphql.UnionConfig
Node *ast.UnionDefinition
Args map[string]interface{}
}
// VisitEnumParams params
type VisitEnumParams struct {
Context context.Context
Config *graphql.EnumConfig
Node *ast.EnumDefinition
Args map[string]interface{}
}
// VisitEnumValueParams params
type VisitEnumValueParams struct {
Context context.Context
Config *graphql.EnumValueConfig
Node *ast.EnumValueDefinition
Args map[string]interface{}
}
// VisitInputObjectParams params
type VisitInputObjectParams struct {
Context context.Context
Config *graphql.InputObjectConfig
Node *ast.InputObjectDefinition
Args map[string]interface{}
}
// VisitInputFieldDefinitionParams params
type VisitInputFieldDefinitionParams struct {
Context context.Context
Config *graphql.InputObjectFieldConfig
Node *ast.InputValueDefinition
Args map[string]interface{}
}
// SchemaDirectiveVisitorMap a map of schema directive visitors
type SchemaDirectiveVisitorMap map[string]*SchemaDirectiveVisitor
// DirectiveMap a map of directives
type DirectiveMap map[string]*graphql.Directive
// converts the directive map to an array
func (c *registry) directiveArray() []*graphql.Directive {
a := make([]*graphql.Directive, 0)
for _, d := range c.directives {
a = append(a, d)
}
return a
}
// builds directives from ast
func (c *registry) buildDirectiveFromAST(definition *ast.DirectiveDefinition) error {
name := definition.Name.Value
directiveConfig := graphql.DirectiveConfig{
Name: name,
Description: getDescription(definition),
Args: graphql.FieldConfigArgument{},
Locations: []string{},
}
for _, arg := range definition.Arguments {
if argValue, err := c.buildArgFromAST(arg); err == nil {
directiveConfig.Args[arg.Name.Value] = argValue
} else {
return err
}
}
for _, loc := range definition.Locations {
directiveConfig.Locations = append(directiveConfig.Locations, loc.Value)
}
c.directives[name] = graphql.NewDirective(directiveConfig)
return nil
}
type applyDirectiveParams struct {
config interface{}
directives []*ast.Directive
node interface{}
extensions []*ast.ObjectDefinition
parentName string
parentKind string
}
// applies directives
func (c *registry) applyDirectives(p applyDirectiveParams) error {
if c.directiveMap == nil {
return nil
}
for _, def := range p.directives {
name := def.Name.Value
visitor, hasVisitor := c.directiveMap[name]
if !hasVisitor {
continue
}
directive, err := c.getDirective(name)
if err != nil {
return err
}
args, err := GetArgumentValues(directive.Args, def.Arguments, map[string]interface{}{})
if err != nil {
return fmt.Errorf("applyDirective %v error: %s", p.config, err)
}
switch p.config.(type) {
case *graphql.SchemaConfig:
if visitor.VisitSchema != nil {
if err := visitor.VisitSchema(VisitSchemaParams{
Context: c.ctx,
Config: p.config.(*graphql.SchemaConfig),
Args: args,
Node: p.node.(*ast.SchemaDefinition),
}); err != nil {
return err
}
}
case *graphql.ScalarConfig:
if visitor.VisitScalar != nil {
if err := visitor.VisitScalar(VisitScalarParams{
Context: c.ctx,
Config: p.config.(*graphql.ScalarConfig),
Args: args,
Node: p.node.(*ast.ScalarDefinition),
}); err != nil {
return err
}
}
case *graphql.ObjectConfig:
if visitor.VisitObject != nil {
if err := visitor.VisitObject(VisitObjectParams{
Context: c.ctx,
Config: p.config.(*graphql.ObjectConfig),
Args: args,
Node: p.node.(*ast.ObjectDefinition),
Extensions: p.extensions,
}); err != nil {
return err
}
}
case *graphql.Field:
if visitor.VisitFieldDefinition != nil {
if err := visitor.VisitFieldDefinition(VisitFieldDefinitionParams{
Context: c.ctx,
Config: p.config.(*graphql.Field),
Args: args,
Node: p.node.(*ast.FieldDefinition),
ParentName: p.parentName,
ParentKind: p.parentKind,
}); err != nil {
return err
}
}
case *graphql.ArgumentConfig:
if visitor.VisitArgumentDefinition != nil {
if err := visitor.VisitArgumentDefinition(VisitArgumentDefinitionParams{
Context: c.ctx,
Config: p.config.(*graphql.ArgumentConfig),
Args: args,
Node: p.node.(*ast.InputValueDefinition),
}); err != nil {
return err
}
}
case *graphql.InterfaceConfig:
if visitor.VisitInterface != nil {
if err := visitor.VisitInterface(VisitInterfaceParams{
Context: c.ctx,
Config: p.config.(*graphql.InterfaceConfig),
Args: args,
Node: p.node.(*ast.InterfaceDefinition),
}); err != nil {
return err
}
}
case *graphql.UnionConfig:
if visitor.VisitUnion != nil {
if err := visitor.VisitUnion(VisitUnionParams{
Context: c.ctx,
Config: p.config.(*graphql.UnionConfig),
Args: args,
Node: p.node.(*ast.UnionDefinition),
}); err != nil {
return err
}
}
case *graphql.EnumConfig:
if visitor.VisitEnum != nil {
if err := visitor.VisitEnum(VisitEnumParams{
Context: c.ctx,
Config: p.config.(*graphql.EnumConfig),
Args: args,
Node: p.node.(*ast.EnumDefinition),
}); err != nil {
return err
}
}
case *graphql.EnumValueConfig:
if visitor.VisitEnumValue != nil {
if err := visitor.VisitEnumValue(VisitEnumValueParams{
Context: c.ctx,
Config: p.config.(*graphql.EnumValueConfig),
Args: args,
Node: p.node.(*ast.EnumValueDefinition),
}); err != nil {
return err
}
}
case *graphql.InputObjectConfig:
if visitor.VisitInputObject != nil {
if err := visitor.VisitInputObject(VisitInputObjectParams{
Context: c.ctx,
Config: p.config.(*graphql.InputObjectConfig),
Args: args,
Node: p.node.(*ast.InputObjectDefinition),
}); err != nil {
return err
}
}
case *graphql.InputObjectFieldConfig:
if visitor.VisitInputFieldDefinition != nil {
if err := visitor.VisitInputFieldDefinition(VisitInputFieldDefinitionParams{
Context: c.ctx,
Config: p.config.(*graphql.InputObjectFieldConfig),
Args: args,
Node: p.node.(*ast.InputValueDefinition),
}); err != nil {
return err
}
}
}
}
return nil
}