Skip to content

Commit

Permalink
Add extensions to response struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Sep 17, 2018
1 parent bfb48f2 commit 60196b8
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 51 deletions.
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinitio
})

return &graphql.Response{
Data: buf,
Errors: ec.Errors,
}
Data: buf,
Errors: ec.Errors,
Extensions: ec.Extensions, }
{{- else }}
return graphql.ErrorResponse(ctx, "queries are not supported")
{{- end }}
Expand All @@ -146,8 +146,9 @@ func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefini
})

return &graphql.Response{
Data: buf,
Errors: ec.Errors,
Data: buf,
Errors: ec.Errors,
Extensions: ec.Extensions,
}
{{- else }}
return graphql.ErrorResponse(ctx, "mutations are not supported")
Expand Down Expand Up @@ -181,8 +182,9 @@ func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDe
}

return &graphql.Response{
Data: buf,
Errors: ec.Errors,
Data: buf,
Errors: ec.Errors,
Extensions: ec.Extensions,
}
}
{{- else }}
Expand Down
11 changes: 6 additions & 5 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions example/chat/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions example/config/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions example/dataloader/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions example/scalars/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions example/selection/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions example/starwars/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions example/todo/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions graphql/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ type RequestContext struct {
DirectiveMiddleware FieldMiddleware
RequestMiddleware RequestMiddleware

errorsMu sync.Mutex
Errors gqlerror.List
errorsMu sync.Mutex
Errors gqlerror.List
extensionsMu sync.Mutex
Extensions map[string]interface{}
}

func DefaultResolverMiddleware(ctx context.Context, next Resolver) (res interface{}, err error) {
Expand Down Expand Up @@ -176,3 +178,20 @@ func AddError(ctx context.Context, err error) {
func AddErrorf(ctx context.Context, format string, args ...interface{}) {
GetRequestContext(ctx).Errorf(ctx, format, args...)
}

// RegisterExtension registers an extension, returns error if extension has already been registered
func (c *RequestContext) RegisterExtension(key string, value interface{}) error {
c.extensionsMu.Lock()
defer c.extensionsMu.Unlock()

if c.Extensions == nil {
c.Extensions = make(map[string]interface{})
}

if _, ok := c.Extensions[key]; ok {
return fmt.Errorf("extension already registered for key %s", key)
}

c.Extensions[key] = value
return nil
}
5 changes: 3 additions & 2 deletions graphql/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

type Response struct {
Data json.RawMessage `json:"data"`
Errors gqlerror.List `json:"errors,omitempty"`
Data json.RawMessage `json:"data"`
Errors gqlerror.List `json:"errors,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}

func ErrorResponse(ctx context.Context, messagef string, args ...interface{}) *Response {
Expand Down
6 changes: 3 additions & 3 deletions integration/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 60196b8

Please sign in to comment.