gqlgen: No data returned if there are errors.
What happened?
I have a scenario where I need to return data from a query, but also an error that occured during processing. This should be possible reading the latest version of the spec http://spec.graphql.org/draft/#sel-FAPHRJCAACGyEnlV.
If the data entry in the response is present (including if it is the value null), the errors entry in the response may contain any errors that occurred during execution. If errors occurred during execution, it should contain those errors.
Currently, if my resolver returns an error (or an error was added using graphql.AddError
, I see no data back in the response, even if that field is marked as mandatory in the schema. I only get the error back in the response:
{
"errors": [
{
"message": "some error occured",
"path": [
"todos"
]
}
],
"data": null <--- Should be populated.
}
What did you expect?
I would expect both the data and the errors object to be present.
Minimal graphql.schema and models to reproduce
Use go run github.com/99designs/gqlgen init
Resolver:
func (r *queryResolver) Todos(ctx context.Context) ([]Todo, error) {
return []Todo{
{ID: "1"},
}, errors.New("some error occured")
}
versions
gqlgen version
? latestgo version
? go1.14- dep or go modules? go modules
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 10
- Comments: 16 (3 by maintainers)
I have discovered that I am suffering from this issue as well. I đź‘Ť and would love to hear about a possible solution.
I have found a workaround for now (after skimming through the code base):
Seems like
gqlgen
ignores the returneddata
only if the resolver function directly returns an error, not the ones added viagraphql.AddError*()
.However I think
gqlgen
should also returndata
even if the resolver returns an error. The fix should also be fairly easy and I am putting up a PR now for maintainers to review.It appears this is the same issue that https://github.com/99designs/gqlgen/pull/1384 addresses . However I believe this is a misinterpretation of the spec. Specifically referring to the following in http://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability:
Marking a field as non-null will result in the null result bubbling up to the nearest nullable ancestor. See this section of the spec.. @ogrok hopefully this casts some light on your issue.
Nope