Skip to content

Commit

Permalink
Add a signpost method to handler extension interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Nov 11, 2019
1 parent 0a39ae2 commit fc727c9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions graphql/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type (
DispatchOperation(ctx context.Context, rc *OperationContext) (ResponseHandler, context.Context)
}

// HandlerExtension interface is entirely optional, see the list of possible hook points below
// HandlerExtension adds functionality to the http handler. See the list of possible hook points below
// Its important to understand the lifecycle of a graphql request and the terminology we use in gqlgen
// before working with these
//
Expand All @@ -46,7 +46,9 @@ type (
// | | RESPONSE { "data": { "chat": { "message": "byee" } } } | |
// | +--------------------------------------------------------------------+ |
// +------------------------------------------------------------------------+
HandlerExtension interface{}
HandlerExtension interface {
ExtensionName() string
}

// OperationParameterMutator is called before creating a request context. allows manipulating the raw query
// on the way in.
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/apollotracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type (
var _ graphql.ResponseInterceptor = Tracer{}
var _ graphql.FieldInterceptor = Tracer{}

func (a Tracer) ExtensionName() string {
return "ApolloTracing"
}

func (a Tracer) InterceptField(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
rc := graphql.GetOperationContext(ctx)
td, ok := graphql.GetExtension(ctx, "tracing").(*TracingExtension)
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/extension/apq.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type AutomaticPersistedQuery struct {
Cache graphql.Cache
}

func (a AutomaticPersistedQuery) ExtensionName() string {
return "AutomaticPersistedQuery"
}

func (a AutomaticPersistedQuery) MutateRequest(ctx context.Context, rawParams *graphql.RawParams) error {
if rawParams.Extensions["persistedQuery"] == nil {
return nil
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/extension/complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func FixedComplexityLimit(limit int) graphql.HandlerExtension {
})
}

func (c ComplexityLimit) ExtensionName() string {
return "ComplexityLimit"
}

func (c ComplexityLimit) MutateOperationContext(ctx context.Context, rc *graphql.OperationContext) *gqlerror.Error {
es := graphql.GetServerContext(ctx)
op := rc.Doc.Operations.ForName(rc.OperationName)
Expand Down
4 changes: 4 additions & 0 deletions graphql/handler/extension/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Introspection struct{}

var _ graphql.OperationContextMutator = Introspection{}

func (c Introspection) ExtensionName() string {
return "Introspection"
}

func (c Introspection) MutateOperationContext(ctx context.Context, rc *graphql.OperationContext) *gqlerror.Error {
rc.DisableIntrospection = false
return nil
Expand Down
12 changes: 12 additions & 0 deletions graphql/handler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,30 @@ func sendErrorf(w http.ResponseWriter, code int, format string, args ...interfac

type OperationFunc func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler

func (r OperationFunc) ExtensionName() string {
return "InlineOperationFunc"
}

func (r OperationFunc) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
return r(ctx, next)
}

type ResponseFunc func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response

func (r ResponseFunc) ExtensionName() string {
return "InlineResponseFunc"
}

func (r ResponseFunc) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
return r(ctx, next)
}

type FieldFunc func(ctx context.Context, next graphql.Resolver) (res interface{}, err error)

func (f FieldFunc) ExtensionName() string {
return "InlineFieldFunc"
}

func (f FieldFunc) InterceptField(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
return f(ctx, next)
}
4 changes: 4 additions & 0 deletions graphql/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func DefaultRecover(ctx context.Context, err interface{}) error {

var _ OperationContextMutator = RecoverFunc(nil)

func (f RecoverFunc) ExtensionName() string {
return "RecoverFunc"
}

func (f RecoverFunc) MutateOperationContext(ctx context.Context, rc *OperationContext) *gqlerror.Error {
rc.Recover = f
return nil
Expand Down

0 comments on commit fc727c9

Please sign in to comment.