-
Notifications
You must be signed in to change notification settings - Fork 12
/
plugin.go
81 lines (70 loc) · 2.81 KB
/
plugin.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
package gqlengine
import (
"reflect"
"runtime/debug"
"github.com/karfield/graphql"
)
type Plugin interface {
BeforeCheckArgumentsStruct(baseType reflect.Type) interface{}
CheckArgumentsEmbeddedField(pluginData interface{}, field *reflect.StructField) error
CheckArgument(pluginData interface{}, name string, typ graphql.Type, tag *reflect.StructTag, goType reflect.Type, defaultValue interface{}) error
MatchAndCallArgumentsMethod(pluginData interface{}, method reflect.Method, prototype reflect.Value) error
AfterCheckArgumentsStruct(pluginData interface{}) error
BeforeCheckObjectStruct(baseType reflect.Type) interface{}
CheckObjectEmbeddedFieldTags(pluginData interface{}, field *reflect.StructField) error
CheckObjectField(pluginData interface{}, name string, typ graphql.Type, tag *reflect.StructTag, goType reflect.Type) error
MatchAndCallObjectMethod(pluginData interface{}, method reflect.Method, prototype reflect.Value) error
AfterCheckObjectStruct(pluginData interface{}, obj *graphql.Object) error
BeforeCheckInputStruct(baseType reflect.Type) interface{}
CheckInputObjectEmbeddedFieldTags(pluginData interface{}, field *reflect.StructField) error
CheckInputObjectField(pluginData interface{}, name string, typ graphql.Type, tag *reflect.StructTag, goType reflect.Type) error
MatchAndCallInputObjectMethod(pluginData interface{}, method reflect.Method, prototype reflect.Value) error
AfterCheckInputStruct(pluginData interface{}, input *graphql.InputObject) error
CheckQueryOperation(operation string, arguments reflect.Type, typ reflect.Type)
CheckMutationOperation(operation string, arguments reflect.Type, typ reflect.Type)
}
type pluginWrapper struct {
name string
plugin Plugin
}
func (engine *Engine) RegisterPlugin(name string, plugin Plugin) {
engine.plugins = append(engine.plugins, pluginWrapper{name, plugin})
}
func (engine *Engine) callPluginsSafely(do func(name string, plugin Plugin) error, handleError func(name string, err error)) {
if len(engine.plugins) == 0 {
return
}
for _, w := range engine.plugins {
(func(wrapper *pluginWrapper) {
defer func() {
if r := recover(); r != nil {
if err, ok := r.(error); ok {
if handleError != nil {
handleError(wrapper.name, err)
}
}
if engine.opts.Debug {
debug.PrintStack()
}
}
}()
if err := do(wrapper.name, wrapper.plugin); err != nil {
if handleError != nil {
handleError(wrapper.name, err)
}
}
})(&w)
}
}
func (engine *Engine) callPluginOnMethod(implType reflect.Type, call func(method reflect.Method, prototype reflect.Value)) {
if len(engine.plugins) > 0 {
if numField := implType.NumMethod(); numField > 0 {
p := newPrototype(implType)
pv := reflect.ValueOf(p)
for i := 0; i < numField; i++ {
method := implType.Method(i)
call(method, pv)
}
}
}
}