Skip to content

Commit

Permalink
Merge pull request #152 from movio/request-middleware
Browse files Browse the repository at this point in the history
Add WrapGraphQLClientTransport to Plugin API
  • Loading branch information
Nicolas Maquet authored May 1, 2022
2 parents 10f30a5 + 5b7e016 commit a33268f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type ClientOpt func(*GraphQLClient)

// NewClient creates a new GraphQLClient from the given options.
func NewClient(opts ...ClientOpt) *GraphQLClient {
return NewClientWithPlugins(nil, opts...)
}

func NewClientWithPlugins(plugins []Plugin, opts ...ClientOpt) *GraphQLClient {
c := &GraphQLClient{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
Expand All @@ -38,6 +42,9 @@ func NewClient(opts ...ClientOpt) *GraphQLClient {
opt(c)
}

for _, plugin := range plugins {
c.HTTPClient.Transport = plugin.WrapGraphQLClientTransport(c.HTTPClient.Transport)
}
return c
}

Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (c *Config) Init() error {
if c.QueryHTTPClient != nil {
queryClientOptions = append(queryClientOptions, WithHTTPClient(c.QueryHTTPClient))
}
queryClient := NewClient(queryClientOptions...)
queryClient := NewClientWithPlugins(c.plugins, queryClientOptions...)
es := NewExecutableSchema(c.plugins, c.MaxRequestsPerQuery, queryClient, services...)
err = es.UpdateSchema(true)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewExecutableSchema(plugins []Plugin, maxRequestsPerQuery int64, client *Gr
}

if client == nil {
client = NewClient()
client = NewClientWithPlugins(plugins)
}

return &ExecutableSchema{
Expand Down Expand Up @@ -138,6 +138,10 @@ func (s *ExecutableSchema) ExecuteQuery(ctx context.Context) *graphql.Response {
operation := operationCtx.Operation
variables := operationCtx.Variables

for _, plugin := range s.plugins {
plugin.InterceptRequest(ctx, operation.Name, operationCtx.RawQuery, variables)
}

AddField(ctx, "operation.name", operation.Name)
AddField(ctx, "operation.type", operation.Operation)

Expand Down
14 changes: 14 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bramble

import (
"context"
"encoding/json"
"net/http"

Expand All @@ -24,6 +25,9 @@ type Plugin interface {
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{})
}

// BasePlugin is an empty plugin. It can be embedded by any plugin as a way to avoid
Expand All @@ -49,6 +53,11 @@ 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{}) {
}

// ApplyMiddlewarePublicMux ...
func (p *BasePlugin) ApplyMiddlewarePublicMux(h http.Handler) http.Handler {
return h
Expand All @@ -59,6 +68,11 @@ 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.
Expand Down

0 comments on commit a33268f

Please sign in to comment.