Skip to content

Commit

Permalink
Refactor ResolverMiddleware to FieldMiddleware
Browse files Browse the repository at this point in the history
This will allow us to include DirectiveMiddleware in the same middleware
setup, that will run after Resolver middlewares.
  • Loading branch information
Mathew Byrne committed Jul 26, 2018
1 parent 66593ff commit 764c6fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
6 changes: 1 addition & 5 deletions codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
rctx.PushField(field.Alias)
defer rctx.Pop()
{{- end }}
resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
{{- if $field.IsResolver }}
return ec.resolvers.{{ $field.ShortInvocation }}
{{- else if $field.GoVarName }}
Expand All @@ -59,10 +59,6 @@
{{- end }}
{{- end }}
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
return graphql.Null
}
Expand Down
39 changes: 27 additions & 12 deletions graphql/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Resolver func(ctx context.Context) (res interface{}, err error)
type ResolverMiddleware func(ctx context.Context, next Resolver) (res interface{}, err error)
type FieldMiddleware func(ctx context.Context, next Resolver) (res interface{}, err error)
type RequestMiddleware func(ctx context.Context, next func(ctx context.Context) []byte) []byte

type RequestContext struct {
Expand All @@ -19,10 +19,11 @@ type RequestContext struct {
Doc *ast.QueryDocument
// ErrorPresenter will be used to generate the error
// message from errors given to Error().
ErrorPresenter ErrorPresenterFunc
Recover RecoverFunc
ResolverMiddleware ResolverMiddleware
RequestMiddleware RequestMiddleware
ErrorPresenter ErrorPresenterFunc
Recover RecoverFunc
ResolverMiddleware FieldMiddleware
DirectiveMiddleware FieldMiddleware
RequestMiddleware RequestMiddleware

errorsMu sync.Mutex
Errors gqlerror.List
Expand All @@ -32,19 +33,33 @@ func DefaultResolverMiddleware(ctx context.Context, next Resolver) (res interfac
return next(ctx)
}

func DefaultDirectiveMiddleware(ctx context.Context, next Resolver) (res interface{}, err error) {
return next(ctx)
}

func DefaultRequestMiddleware(ctx context.Context, next func(ctx context.Context) []byte) []byte {
return next(ctx)
}

func (c *RequestContext) FieldMiddleware(ctx context.Context, next Resolver) interface{} {
res, err := c.ResolverMiddleware(ctx, next)
if err != nil {
c.Error(ctx, err)
return nil
}
return res
}

func NewRequestContext(doc *ast.QueryDocument, query string, variables map[string]interface{}) *RequestContext {
return &RequestContext{
Doc: doc,
RawQuery: query,
Variables: variables,
ResolverMiddleware: DefaultResolverMiddleware,
RequestMiddleware: DefaultRequestMiddleware,
Recover: DefaultRecover,
ErrorPresenter: DefaultErrorPresenter,
Doc: doc,
RawQuery: query,
Variables: variables,
ResolverMiddleware: DefaultResolverMiddleware,
DirectiveMiddleware: DefaultDirectiveMiddleware,
RequestMiddleware: DefaultRequestMiddleware,
Recover: DefaultRecover,
ErrorPresenter: DefaultErrorPresenter,
}
}

Expand Down
4 changes: 2 additions & 2 deletions handler/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Config struct {
upgrader websocket.Upgrader
recover graphql.RecoverFunc
errorPresenter graphql.ErrorPresenterFunc
resolverHook graphql.ResolverMiddleware
resolverHook graphql.FieldMiddleware
requestHook graphql.RequestMiddleware
}

Expand Down Expand Up @@ -74,7 +74,7 @@ func ErrorPresenter(f graphql.ErrorPresenterFunc) Option {

// ResolverMiddleware allows you to define a function that will be called around every resolver,
// useful for tracing and logging.
func ResolverMiddleware(middleware graphql.ResolverMiddleware) Option {
func ResolverMiddleware(middleware graphql.FieldMiddleware) Option {
return func(cfg *Config) {
if cfg.resolverHook == nil {
cfg.resolverHook = middleware
Expand Down
2 changes: 1 addition & 1 deletion opentracing/opentracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/vektah/gqlgen/graphql"
)

func ResolverMiddleware() graphql.ResolverMiddleware {
func ResolverMiddleware() graphql.FieldMiddleware {
return func(ctx context.Context, next graphql.Resolver) (interface{}, error) {
rctx := graphql.GetResolverContext(ctx)
span, ctx := opentracing.StartSpanFromContext(ctx, rctx.Object+"_"+rctx.Field.Name,
Expand Down

0 comments on commit 764c6fd

Please sign in to comment.