Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to return multiple errors from resolvers raise than add it to stack. #2454

Merged
merged 10 commits into from
Dec 8, 2022
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
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