diff --git a/graphql/context.go b/graphql/context.go index 3262bb25f19..8528cb4b343 100644 --- a/graphql/context.go +++ b/graphql/context.go @@ -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 diff --git a/graphql/result_context.go b/graphql/context_response.go similarity index 82% rename from graphql/result_context.go rename to graphql/context_response.go index a7d56f07008..b838b3cd975 100644 --- a/graphql/result_context.go +++ b/graphql/context_response.go @@ -8,7 +8,7 @@ import ( "github.com/vektah/gqlparser/gqlerror" ) -type resultContext struct { +type responseContext struct { errors gqlerror.List errorsMu sync.Mutex @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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{}{} } @@ -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 } diff --git a/graphql/handler.go b/graphql/handler.go index 41613299010..36a70d5572d 100644 --- a/graphql/handler.go +++ b/graphql/handler.go @@ -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 @@ -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{} @@ -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 diff --git a/graphql/handler/apollotracing/tracer.go b/graphql/handler/apollotracing/tracer.go index 2dafd618a0f..8373f017fcd 100644 --- a/graphql/handler/apollotracing/tracer.go +++ b/graphql/handler/apollotracing/tracer.go @@ -39,7 +39,7 @@ type ( } ) -var _ graphql.ResultInterceptor = ApolloTracing{} +var _ graphql.ResponseInterceptor = ApolloTracing{} var _ graphql.FieldInterceptor = ApolloTracing{} func New() graphql.HandlerPlugin { @@ -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 diff --git a/graphql/handler/executor.go b/graphql/handler/executor.go index 6960728ecdf..5105efb078d 100644 --- a/graphql/handler/executor.go +++ b/graphql/handler/executor.go @@ -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) { @@ -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) }) } @@ -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) }) @@ -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 { diff --git a/graphql/handler/server.go b/graphql/handler/server.go index 79d83e76365..d99b13d6884 100644 --- a/graphql/handler/server.go +++ b/graphql/handler/server.go @@ -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)