Skip to content

Commit

Permalink
Ability to return multiple errors from resolvers raise than add it to…
Browse files Browse the repository at this point in the history
… stack. (#2454)

* Remove DO NOT EDIT

Sometimes vscode warn about this while editing resolvers code.
Finally the resolver's code is editable and generated at the same time.

* Ability to return multiple errors from resolver.

* Multiple errors return example

* Fix missing import

* reformat

* gofmt

* go generate ./...

* go generate ./...

* Regenerate

Signed-off-by: Steve Coffman <steve@khanacademy.org>

* remove trailing period

Signed-off-by: Steve Coffman <steve@khanacademy.org>

Signed-off-by: Steve Coffman <steve@khanacademy.org>
Co-authored-by: Roman A. Grigorovich <ragrigorov@mts.ru>
Co-authored-by: Steve Coffman <steve@khanacademy.org>
  • Loading branch information
3 people authored Dec 8, 2022
1 parent db1e3b8 commit a44685b
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 11 deletions.
2 changes: 1 addition & 1 deletion _examples/config/schema.resolvers.go

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

2 changes: 1 addition & 1 deletion _examples/config/todo.resolvers.go

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

2 changes: 1 addition & 1 deletion _examples/config/user.resolvers.go

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

2 changes: 1 addition & 1 deletion _examples/federation/accounts/graph/entity.resolvers.go

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

2 changes: 1 addition & 1 deletion _examples/federation/accounts/graph/schema.resolvers.go

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

2 changes: 1 addition & 1 deletion _examples/federation/products/graph/entity.resolvers.go

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

2 changes: 1 addition & 1 deletion _examples/federation/products/graph/schema.resolvers.go

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

2 changes: 1 addition & 1 deletion _examples/federation/reviews/graph/entity.resolvers.go

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

2 changes: 1 addition & 1 deletion _examples/federation/reviews/graph/schema.resolvers.go

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

54 changes: 54 additions & 0 deletions docs/content/reference/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package foo

import (
"context"
"errors"

"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/99designs/gqlgen/graphql"
)

// DoThings add errors to the stack.
func (r Query) DoThings(ctx context.Context) (bool, error) {
// Print a formatted string
graphql.AddErrorf(ctx, "Error %d", 1)
Expand Down Expand Up @@ -60,6 +62,58 @@ They will be returned in the same order in the response, eg:
}
```

or you can simply return multiple errors

```go
package foo

import (
"context"
"errors"

"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/99designs/gqlgen/graphql"
)

var errSomethingWrong = errors.New("some validation failed")

// DoThingsReturnMultipleErrors collect errors and returns it if any.
func (r Query) DoThingsReturnMultipleErrors(ctx context.Context) (bool, error) {
errList := gqlerror.List{}

// Add existing error
errList = append(errList, gqlerror.Wrap(errSomethingWrong))

// Create new formatted and append
errList = append(errList, gqlerror.Errorf("invalid value: %s", "invalid"))

// Or fully customize the error and append
errList = append(errList, &gqlerror.Error{
Path: graphql.GetPath(ctx),
Message: "A descriptive error message",
Extensions: map[string]interface{}{
"code": "10-4",
},
})

return false, errList
}
```

They will be returned in the same order in the response, eg:
```json
{
"data": {
"todo": null
},
"errors": [
{ "message": "some validation failed", "path": [ "todo" ] },
{ "message": "invalid value: invalid", "path": [ "todo" ] },
{ "message": "A descriptive error message", "path": [ "todo" ], "extensions": { "code": "10-4" } },
]
}
```

## Hooks

### The error presenter
Expand Down
8 changes: 8 additions & 0 deletions graphql/context_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)

// Deprecated: Please update all references to OperationContext instead
Expand Down Expand Up @@ -109,6 +110,13 @@ func (c *OperationContext) Errorf(ctx context.Context, format string, args ...in
// Error sends an error to the client, passing it through the formatter.
// Deprecated: use graphql.AddError(ctx, err) instead
func (c *OperationContext) Error(ctx context.Context, err error) {
if errList, ok := err.(gqlerror.List); ok {
for _, e := range errList {
AddError(ctx, e)
}
return
}

AddError(ctx, err)
}

Expand Down

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

2 changes: 1 addition & 1 deletion plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (m *Plugin) generatePerSchema(data *codegen.Data) error {
FileNotice: `
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version ` + graphql.Version + ` DO NOT EDIT.`,
// Code generated by github.com/99designs/gqlgen version ` + graphql.Version,
Filename: filename,
Data: resolverBuild,
Packages: data.Config.Packages,
Expand Down

0 comments on commit a44685b

Please sign in to comment.