Skip to content

Commit

Permalink
rename result handler to response handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Oct 30, 2019
1 parent 4a69bcd commit 72c47c9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion graphql/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Resolver func(ctx context.Context) (res interface{}, err error)
type ResultMiddleware func(ctx context.Context, next ResultHandler) *Response
type ResultMiddleware func(ctx context.Context, next ResponseHandler) *Response
type OperationMiddleware func(ctx context.Context, next OperationHandler, writer Writer)
type FieldMiddleware func(ctx context.Context, next Resolver) (res interface{}, err error)
type ComplexityLimitFunc func(ctx context.Context) int
Expand Down
26 changes: 13 additions & 13 deletions graphql/result_context.go → graphql/context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/vektah/gqlparser/gqlerror"
)

type resultContext struct {
type responseContext struct {
errors gqlerror.List
errorsMu sync.Mutex

Expand All @@ -18,18 +18,18 @@ type resultContext struct {

var resultCtx key = "result_context"

func getResultContext(ctx context.Context) *resultContext {
val, _ := ctx.Value(resultCtx).(*resultContext)
func getResponseContext(ctx context.Context) *responseContext {
val, _ := ctx.Value(resultCtx).(*responseContext)
return val
}

func WithResultContext(ctx context.Context) context.Context {
return context.WithValue(ctx, resultCtx, &resultContext{})
func WithResponseContext(ctx context.Context) context.Context {
return context.WithValue(ctx, resultCtx, &responseContext{})
}

// AddErrorf writes a formatted error to the client, first passing it through the error presenter.
func AddErrorf(ctx context.Context, format string, args ...interface{}) {
c := getResultContext(ctx)
c := getResponseContext(ctx)

c.errorsMu.Lock()
defer c.errorsMu.Unlock()
Expand All @@ -39,7 +39,7 @@ func AddErrorf(ctx context.Context, format string, args ...interface{}) {

// AddError sends an error to the client, first passing it through the error presenter.
func AddError(ctx context.Context, err error) {
c := getResultContext(ctx)
c := getResponseContext(ctx)

c.errorsMu.Lock()
defer c.errorsMu.Unlock()
Expand All @@ -49,7 +49,7 @@ func AddError(ctx context.Context, err error) {

// HasFieldError returns true if the given field has already errored
func HasFieldError(ctx context.Context, rctx *ResolverContext) bool {
c := getResultContext(ctx)
c := getResponseContext(ctx)

c.errorsMu.Lock()
defer c.errorsMu.Unlock()
Expand All @@ -65,7 +65,7 @@ func HasFieldError(ctx context.Context, rctx *ResolverContext) bool {

// GetFieldErrors returns a list of errors that occurred in the given field
func GetFieldErrors(ctx context.Context, rctx *ResolverContext) gqlerror.List {
c := getResultContext(ctx)
c := getResponseContext(ctx)

c.errorsMu.Lock()
defer c.errorsMu.Unlock()
Expand All @@ -81,7 +81,7 @@ func GetFieldErrors(ctx context.Context, rctx *ResolverContext) gqlerror.List {
}

func GetErrors(ctx context.Context) gqlerror.List {
resCtx := getResultContext(ctx)
resCtx := getResponseContext(ctx)
resCtx.errorsMu.Lock()
defer resCtx.errorsMu.Unlock()

Expand All @@ -100,7 +100,7 @@ func GetErrors(ctx context.Context) gqlerror.List {

// RegisterExtension allows you to add a new extension into the graphql response
func RegisterExtension(ctx context.Context, key string, value interface{}) {
c := getResultContext(ctx)
c := getResponseContext(ctx)
c.extensionsMu.Lock()
defer c.extensionsMu.Unlock()

Expand All @@ -117,7 +117,7 @@ func RegisterExtension(ctx context.Context, key string, value interface{}) {

// GetExtensions returns any extensions registered in the current result context
func GetExtensions(ctx context.Context) map[string]interface{} {
ext := getResultContext(ctx).extensions
ext := getResponseContext(ctx).extensions
if ext == nil {
return map[string]interface{}{}
}
Expand All @@ -126,7 +126,7 @@ func GetExtensions(ctx context.Context) map[string]interface{} {
}

func GetExtension(ctx context.Context, name string) interface{} {
ext := getResultContext(ctx).extensions
ext := getResponseContext(ctx).extensions
if ext == nil {
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions graphql/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type (
OperationHandler func(ctx context.Context, writer Writer)
ResultHandler func(ctx context.Context) *Response
ResponseHandler func(ctx context.Context) *Response
ResponseStream func() *Response
Writer func(Status, *Response)
Status int
Expand All @@ -33,10 +33,10 @@ type (
//
// +--- REQUEST POST /graphql --------------------------------------------+
// | +- OPERATION query OpName { viewer { name } } -----------------------+ |
// | | RESULT { "data": { "viewer": { "name": "bob" } } } | |
// | | RESPONSE { "data": { "viewer": { "name": "bob" } } } | |
// | +- OPERATION subscription OpName2 { chat { message } } --------------+ |
// | | RESULT { "data": { "chat": { "message": "hello" } } } | |
// | | RESULT { "data": { "chat": { "message": "byee" } } } | |
// | | RESPONSE { "data": { "chat": { "message": "hello" } } } | |
// | | RESPONSE { "data": { "chat": { "message": "byee" } } } | |
// | +--------------------------------------------------------------------+ |
// +------------------------------------------------------------------------+
HandlerPlugin interface{}
Expand All @@ -58,10 +58,10 @@ type (
InterceptOperation(ctx context.Context, next OperationHandler, writer Writer)
}

// ResultInterceptor is called around each graphql operation result. This can be called many times for a single
// ResponseInterceptor is called around each graphql operation response. This can be called many times for a single
// operation the case of subscriptions.
ResultInterceptor interface {
InterceptResult(ctx context.Context, next ResultHandler) *Response
ResponseInterceptor interface {
InterceptResponse(ctx context.Context, next ResponseHandler) *Response
}

// FieldInterceptor called around each field
Expand Down
4 changes: 2 additions & 2 deletions graphql/handler/apollotracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type (
}
)

var _ graphql.ResultInterceptor = ApolloTracing{}
var _ graphql.ResponseInterceptor = ApolloTracing{}
var _ graphql.FieldInterceptor = ApolloTracing{}

func New() graphql.HandlerPlugin {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (a ApolloTracing) InterceptField(ctx context.Context, next graphql.Resolver
return next(ctx)
}

func (a ApolloTracing) InterceptResult(ctx context.Context, next graphql.ResultHandler) *graphql.Response {
func (a ApolloTracing) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
rc := graphql.GetRequestContext(ctx)

start := rc.Stats.OperationStart
Expand Down
14 changes: 7 additions & 7 deletions graphql/handler/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newExecutor(es graphql.ExecutableSchema, plugins []graphql.HandlerPlugin) e
es: es,
}
e.operationMiddleware = e.executableSchemaHandler
e.resultHandler = func(ctx context.Context, next graphql.ResultHandler) *graphql.Response {
e.resultHandler = func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
return next(ctx)
}
e.responseMiddleware = func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
Expand All @@ -43,10 +43,10 @@ func newExecutor(es graphql.ExecutableSchema, plugins []graphql.HandlerPlugin) e
}
}

if p, ok := p.(graphql.ResultInterceptor); ok {
if p, ok := p.(graphql.ResponseInterceptor); ok {
previous := e.resultHandler
e.resultHandler = func(ctx context.Context, next graphql.ResultHandler) *graphql.Response {
return p.InterceptResult(ctx, func(ctx context.Context) *graphql.Response {
e.resultHandler = func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
return p.InterceptResponse(ctx, func(ctx context.Context) *graphql.Response {
return previous(ctx, next)
})
}
Expand Down Expand Up @@ -146,14 +146,14 @@ func (e *executor) executableSchemaHandler(ctx context.Context, write graphql.Wr

switch op.Operation {
case ast.Query:
resCtx := graphql.WithResultContext(ctx)
resCtx := graphql.WithResponseContext(ctx)
resp := e.resultHandler(resCtx, func(ctx context.Context) *graphql.Response {
return e.es.Query(ctx, op)
})
e.write(resCtx, resp, write)

case ast.Mutation:
resCtx := graphql.WithResultContext(ctx)
resCtx := graphql.WithResponseContext(ctx)
resp := e.resultHandler(resCtx, func(ctx context.Context) *graphql.Response {
return e.es.Mutation(ctx, op)
})
Expand All @@ -162,7 +162,7 @@ func (e *executor) executableSchemaHandler(ctx context.Context, write graphql.Wr
case ast.Subscription:
responses := e.es.Subscription(ctx, op)
for {
resCtx := graphql.WithResultContext(ctx)
resCtx := graphql.WithResponseContext(ctx)
resp := e.resultHandler(resCtx, func(ctx context.Context) *graphql.Response {
resp := responses()
if resp == nil {
Expand Down
2 changes: 1 addition & 1 deletion graphql/handler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *Server) Use(plugin graphql.HandlerPlugin) {
graphql.RequestContextMutator,
graphql.OperationInterceptor,
graphql.FieldInterceptor,
graphql.ResultInterceptor:
graphql.ResponseInterceptor:
s.plugins = append(s.plugins, plugin)
s.exec = newExecutor(s.es, s.plugins)

Expand Down

0 comments on commit 72c47c9

Please sign in to comment.