Skip to content

Commit

Permalink
Merge pull request #205 from movio/intercept-response
Browse files Browse the repository at this point in the history
Add InterceptResponse to the Plugin interface
  • Loading branch information
pkqk authored May 3, 2023
2 parents b2865c4 + 8bba076 commit 9be616a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
23 changes: 15 additions & 8 deletions executable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *ExecutableSchema) ExecuteQuery(ctx context.Context) *graphql.Response {
})

if err != nil {
return graphql.ErrorResponse(ctx, err.Error())
return s.interceptResponse(ctx, operation.Name, operationCtx.RawQuery, variables, graphql.ErrorResponse(ctx, err.Error()))
}

extensions := make(map[string]interface{})
Expand Down Expand Up @@ -196,9 +196,9 @@ func (s *ExecutableSchema) ExecuteQuery(ctx context.Context) *graphql.Response {
qe := newQueryExecution(ctx, operationCtx.OperationName, s.GraphqlClient, filteredSchema, s.BoundaryQueries, int32(s.MaxRequestsPerQuery))
results, executeErrs := qe.Execute(plan)
if len(executeErrs) > 0 {
return &graphql.Response{
return s.interceptResponse(ctx, operation.Name, operationCtx.RawQuery, variables, &graphql.Response{
Errors: executeErrs,
}
})
}

for _, result := range results {
Expand All @@ -224,9 +224,9 @@ func (s *ExecutableSchema) ExecuteQuery(ctx context.Context) *graphql.Response {
if err != nil {
errs = append(errs, &gqlerror.Error{Message: err.Error()})
AddField(ctx, "errors", errs)
return &graphql.Response{
return s.interceptResponse(ctx, operation.Name, operationCtx.RawQuery, variables, &graphql.Response{
Errors: errs,
}
})
}

bubbleErrs, err := bubbleUpNullValuesInPlace(filteredSchema, operation.SelectionSet, mergedResult)
Expand All @@ -235,9 +235,9 @@ func (s *ExecutableSchema) ExecuteQuery(ctx context.Context) *graphql.Response {
} else if err != nil {
errs = append(errs, &gqlerror.Error{Message: err.Error()})
AddField(ctx, "errors", errs)
return &graphql.Response{
return s.interceptResponse(ctx, operation.Name, operationCtx.RawQuery, variables, &graphql.Response{
Errors: errs,
}
})
}

errs = append(errs, bubbleErrs...)
Expand All @@ -251,10 +251,17 @@ func (s *ExecutableSchema) ExecuteQuery(ctx context.Context) *graphql.Response {
AddField(ctx, "errors", errs)
}

return &graphql.Response{
return s.interceptResponse(ctx, operation.Name, operationCtx.RawQuery, variables, &graphql.Response{
Data: formattedResponse,
Errors: errs,
})
}

func (s *ExecutableSchema) interceptResponse(ctx context.Context, operationName, rawQuery string, variables map[string]interface{}, response *graphql.Response) *graphql.Response {
for _, plugin := range s.plugins {
response = plugin.InterceptResponse(ctx, operationName, rawQuery, variables, response)
}
return response
}

// Schema returns the merged schema
Expand Down
8 changes: 8 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"

"github.com/99designs/gqlgen/graphql"
log "github.com/sirupsen/logrus"
)

Expand All @@ -28,6 +29,7 @@ type Plugin interface {
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
Expand Down Expand Up @@ -58,6 +60,12 @@ func (p *BasePlugin) GraphqlQueryPath() (bool, string) {
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
Expand Down

0 comments on commit 9be616a

Please sign in to comment.