Gqlgen: No data returned if there are errors.

Created on 13 May 2020  路  4Comments  路  Source: 99designs/gqlgen

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

Most helpful comment

I have discovered that I am suffering from this issue as well. I 馃憤 and would love to hear about a possible solution.

All 4 comments

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Nope

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cajax picture cajax  路  4Comments

sumanthakannantha picture sumanthakannantha  路  3Comments

msmedes picture msmedes  路  4Comments

kevinmbeaulieu picture kevinmbeaulieu  路  3Comments

jszwedko picture jszwedko  路  3Comments