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? latest
  • go 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)

Most upvoted comments

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):

func (r *queryResolver) Todos(ctx context.Context) ([]Todo, error) {
+	graphql.AddErrorf("some error occurred")
	return []Todo{
		{ID: "1"},
-	}, errors.New("some error occurred")
+	}, nil
}

Seems like gqlgen ignores the returned data only if the resolver function directly returns an error, not the ones added via graphql.AddError*().

However I think gqlgen should also return data 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:

If an error is thrown while resolving a field, it should be treated as though the field returned null, and an error must be added to the “errors” list in the response.

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.