-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathplugin.go
103 lines (84 loc) · 3.53 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package bramble
import (
"context"
"encoding/json"
log "log/slog"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
)
// Plugin is a Bramble plugin. Plugins can be used to extend base Bramble functionalities.
type Plugin interface {
// ID must return the plugin identifier (name). This is the id used to match
// the plugin in the configuration.
ID() string
// Configure is called during initialization and every time the config is modified.
// The pluginCfg argument is the raw json contained in the "config" key for that plugin.
Configure(cfg *Config, pluginCfg json.RawMessage) error
// Init is called once on initialization
Init(schema *ExecutableSchema)
SetupPublicMux(mux *http.ServeMux)
SetupGatewayHandler(handler *handler.Server)
SetupPrivateMux(mux *http.ServeMux)
// Should return true and the query path if the plugin is a service that
// should be federated by Bramble
GraphqlQueryPath() (bool, string)
ApplyMiddlewarePublicMux(http.Handler) http.Handler
ApplyMiddlewarePrivateMux(http.Handler) http.Handler
WrapGraphQLClientTransport(http.RoundTripper) http.RoundTripper
InterceptRequest(ctx context.Context, operationName, rawQuery string, variables map[string]interface{})
InterceptResponse(ctx context.Context, operationName, rawQuery string, variables map[string]interface{}, response *graphql.Response) *graphql.Response
}
// BasePlugin is an empty plugin. It can be embedded by any plugin as a way to avoid
// declaring unnecessary methods.
type BasePlugin struct{}
// Configure ...
func (p *BasePlugin) Configure(*Config, json.RawMessage) error {
return nil
}
// Init ...
func (p *BasePlugin) Init(s *ExecutableSchema) {}
func (p *BasePlugin) SetupGatewayHandler(handler *handler.Server) {}
// SetupPublicMux ...
func (p *BasePlugin) SetupPublicMux(mux *http.ServeMux) {}
// SetupPrivateMux ...
func (p *BasePlugin) SetupPrivateMux(mux *http.ServeMux) {}
// GraphqlQueryPath ...
func (p *BasePlugin) GraphqlQueryPath() (bool, string) {
return false, ""
}
// InterceptRequest is called before bramble starts executing a request.
// It can be used to inspect the unmarshalled GraphQL request bramble receives.
func (p *BasePlugin) InterceptRequest(ctx context.Context, operationName, rawQuery string, variables map[string]interface{}) {
}
// InterceptResponse is called after bramble has finished executing a request.
// It can be used to inspect and/or modify the response bramble will return.
func (p *BasePlugin) InterceptResponse(ctx context.Context, operationName, rawQuery string, variables map[string]interface{}, response *graphql.Response) *graphql.Response {
return response
}
// ApplyMiddlewarePublicMux ...
func (p *BasePlugin) ApplyMiddlewarePublicMux(h http.Handler) http.Handler {
return h
}
// ApplyMiddlewarePrivateMux ...
func (p *BasePlugin) ApplyMiddlewarePrivateMux(h http.Handler) http.Handler {
return h
}
// WrapGraphQLClientTransport wraps the http.RoundTripper used for GraphQL requests.
func (p *BasePlugin) WrapGraphQLClientTransport(transport http.RoundTripper) http.RoundTripper {
return transport
}
var registeredPlugins = map[string]Plugin{}
// RegisterPlugin register a plugin so that it can be enabled via the configuration.
func RegisterPlugin(p Plugin) {
if _, found := registeredPlugins[p.ID()]; found {
log.With("plugin", p.ID()).Error("plugin already registered")
os.Exit(1)
}
registeredPlugins[p.ID()] = p
}
// RegisteredPlugins returned the list of registered plugins.
func RegisteredPlugins() map[string]Plugin {
return registeredPlugins
}