Skip to content

Commit

Permalink
graphql/executor: move setExtensions()
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Feb 19, 2020
1 parent 3acc942 commit 36365c4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
53 changes: 0 additions & 53 deletions graphql/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,59 +176,6 @@ func (e *Executor) SetRecoverFunc(f graphql.RecoverFunc) {
e.recoverFunc = f
}

func (e *Executor) setExtensions() {
e.operationMiddleware = func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
return next(ctx)
}
e.responseMiddleware = func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
return next(ctx)
}
e.fieldMiddleware = func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
}

// this loop goes backwards so the first extension is the outer most middleware and runs first.
for i := len(e.extensions) - 1; i >= 0; i-- {
p := e.extensions[i]
if p, ok := p.(graphql.OperationInterceptor); ok {
previous := e.operationMiddleware
e.operationMiddleware = func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
return p.InterceptOperation(ctx, func(ctx context.Context) graphql.ResponseHandler {
return previous(ctx, next)
})
}
}

if p, ok := p.(graphql.ResponseInterceptor); ok {
previous := e.responseMiddleware
e.responseMiddleware = func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
return p.InterceptResponse(ctx, func(ctx context.Context) *graphql.Response {
return previous(ctx, next)
})
}
}

if p, ok := p.(graphql.FieldInterceptor); ok {
previous := e.fieldMiddleware
e.fieldMiddleware = func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
return p.InterceptField(ctx, func(ctx context.Context) (res interface{}, err error) {
return previous(ctx, next)
})
}
}
}

for _, p := range e.extensions {
if p, ok := p.(graphql.OperationParameterMutator); ok {
e.operationParameterMutators = append(e.operationParameterMutators, p)
}

if p, ok := p.(graphql.OperationContextMutator); ok {
e.operationContextMutators = append(e.operationContextMutators, p)
}
}
}

// parseQuery decodes the incoming query and validates it, pulling from cache if present.
//
// NOTE: This should NOT look at variables, they will change per request. It should only parse and validate
Expand Down
53 changes: 53 additions & 0 deletions graphql/executor/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,59 @@ func (e *Executor) AroundResponses(f graphql.ResponseMiddleware) {
e.Use(ResponseFunc(f))
}

func (e *Executor) setExtensions() {
e.operationMiddleware = func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
return next(ctx)
}
e.responseMiddleware = func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
return next(ctx)
}
e.fieldMiddleware = func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
}

// this loop goes backwards so the first extension is the outer most middleware and runs first.
for i := len(e.extensions) - 1; i >= 0; i-- {
p := e.extensions[i]
if p, ok := p.(graphql.OperationInterceptor); ok {
previous := e.operationMiddleware
e.operationMiddleware = func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
return p.InterceptOperation(ctx, func(ctx context.Context) graphql.ResponseHandler {
return previous(ctx, next)
})
}
}

if p, ok := p.(graphql.ResponseInterceptor); ok {
previous := e.responseMiddleware
e.responseMiddleware = func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
return p.InterceptResponse(ctx, func(ctx context.Context) *graphql.Response {
return previous(ctx, next)
})
}
}

if p, ok := p.(graphql.FieldInterceptor); ok {
previous := e.fieldMiddleware
e.fieldMiddleware = func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
return p.InterceptField(ctx, func(ctx context.Context) (res interface{}, err error) {
return previous(ctx, next)
})
}
}
}

for _, p := range e.extensions {
if p, ok := p.(graphql.OperationParameterMutator); ok {
e.operationParameterMutators = append(e.operationParameterMutators, p)
}

if p, ok := p.(graphql.OperationContextMutator); ok {
e.operationContextMutators = append(e.operationContextMutators, p)
}
}
}

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

func (r OperationFunc) ExtensionName() string {
Expand Down

0 comments on commit 36365c4

Please sign in to comment.