Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,13 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
if !s.res.QueryResolver.IsValid() {
panic("schema created without resolver, can not exec")
}
execF := s.exec
var execF Exec = func(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) *Response {
return s.exec(ctx, queryString, operationName, variables, s.res)
}
for _, m := range s.middlewares {
execF = m(execF)
}
return execF(ctx, queryString, operationName, variables, s.res)
return execF(ctx, queryString, operationName, variables)
}

func (s *Schema) exec(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, res *resolvable.Schema) *Response {
Expand Down
9 changes: 4 additions & 5 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/graph-gophers/graphql-go/example/social"
"github.com/graph-gophers/graphql-go/example/starwars"
"github.com/graph-gophers/graphql-go/gqltesting"
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
"github.com/graph-gophers/graphql-go/introspection"
"github.com/graph-gophers/graphql-go/trace/tracer"
"strings"
Expand Down Expand Up @@ -4712,15 +4711,15 @@ func TestMiddlewares_In_Exec(t *testing.T) {
`, r,
graphql.WithMiddlewares(
func(next graphql.Exec) graphql.Exec {
return func(ctx context.Context, q string, o string, v map[string]interface{}, r *resolvable.Schema) *graphql.Response {
return func(ctx context.Context, q string, o string, v map[string]interface{}) *graphql.Response {
m1Called = true
return next(ctx, q, o, v, r)
return next(ctx, q, o, v)
}
},
func(next graphql.Exec) graphql.Exec {
return func(ctx context.Context, q string, o string, v map[string]interface{}, r *resolvable.Schema) *graphql.Response {
return func(ctx context.Context, q string, o string, v map[string]interface{}) *graphql.Response {
m2Called = true
return next(ctx, q, o, v, r)
return next(ctx, q, o, v)
}
},
),
Expand Down
11 changes: 5 additions & 6 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"context"

"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
)

// Exec executes the given query with the schema's resolver.
type Exec func(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, res *resolvable.Schema) *Response
type Exec func(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) *Response

// Middleware can wrap Exec to add additional behaviour
type Middleware func(next Exec) Exec

func ParseErrorsMiddleware(parseErrors func([]*errors.QueryError) []*errors.QueryError) Middleware {
return func(next Exec) Exec {
return func(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, res *resolvable.Schema) *Response {
return func(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) *Response {
// perform the original query
response := next(ctx, queryString, operationName, variables, res)
response := next(ctx, queryString, operationName, variables)
// mutate the errors
response.Errors = parseErrors(response.Errors)
// return the response
Expand All @@ -30,12 +29,12 @@ func ParseErrorsMiddleware(parseErrors func([]*errors.QueryError) []*errors.Quer
// If no response is returned, we simply continue by calling next().
func InspectInputMiddleware(inspectInput func(queryString string, operationName string, variables map[string]interface{}) *Response) Middleware {
return func(next Exec) Exec {
return func(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, res *resolvable.Schema) *Response {
return func(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) *Response {
if response := inspectInput(queryString, operationName, variables); response != nil {
return response
}

return next(ctx, queryString, operationName, variables, res)
return next(ctx, queryString, operationName, variables)
}
}
}
Loading