1

Using GraphQL.NET, I've defined a non nullable return type (e.g. LoginPayload) for a mutation like this:

type MyMutation {
  login(input: LoginInput!): LoginPayload!
}

In C#, it looks something like this:

    FieldAsync<NonNullGraphType<LoginPayloadType>>(
        "login",
        arguments: new QueryArguments(
            new QueryArgument<NonNullGraphType<LoginInputType>> { Name = "input" }),
        resolve: async context =>
        {
            //...
        });

Based on this schema definition, the client expects the response data to never be null. However, if there's an exception thrown in the resolver, GraphQL.NET responds like this:

{
  "data": {
    "login": null
  },
  "errors": [
    {
      "message": "GraphQL.ExecutionError: some exception thrown",
      ...
    }
  ]
}

How can I configure GraphQL.Net to exclude the data property when there's an error so it looks like this?

{
  "errors": [
    {
      "message": "GraphQL.ExecutionError: some exception thrown",
      ...
    }
  ]
}
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275

2 Answers2

1

If this is the behavior you're actually seeing, then it's a bug since that's not what should happen according to the spec.

Since Non-Null type fields cannot be null, field errors are propagated to be handled by the parent field. If the parent field may be null then it resolves to null, otherwise if it is a Non-Null type, the field error is further propagated to it’s parent field... If all fields from the root of the request to the source of the field error return Non-Null types, then the "data" entry in the response should be null.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • 1
    This is great feedback! I'll create a simple repo later today to ensure that the problem is reproducible and submit a bug report. – Johnny Oshika Jan 29 '20 at 17:12
  • 1
    Issue reported here: https://github.com/graphql-dotnet/graphql-dotnet/issues/1516 and sample project to reproduce the problem is here: https://github.com/johnnyoshika/graphql-dotnet-null – Johnny Oshika Jan 30 '20 at 07:04
0

This problem has been fixed in GraphQL.NET 3.5 prerelease.

Example 1: NonNullGraphType

Query field:

    Field<NonNullGraphType<WidgetType>>(
        "exception",
        resolve: context =>
        {
            throw new ExecutionError("Something went wrong!");
        }
    );

Version 3.4 Response:

{
  "data": {
    "exception": null
  },
  "errors": [
    {
      "message": "Something went wrong!",
      ...
    }
  ]
}

Version 3.5 Response:

{
  "errors": [
    {
      "message": "Something went wrong!",
      ...
    }
  ],
  "extensions": {
     ...
  }
}

Example 2: Nullable

Query field:

    Field<WidgetType>(
        "exceptionAndNullable",
        resolve: context =>
        {
            throw new ExecutionError("Something went wrong!");
        }
    );

Version 3.4 Response:

{
  "data": {
    "exceptionAndNullable": null
  },
  "errors": [
    {
      "message": "Something went wrong!",
      "...
    }
  ]
}

Version 3.5 Response:

{
  "data": {
    "exceptionAndNullable": null
  },
  "errors": [
    {
      "message": "Something went wrong!",
      ...
  ],
  "extensions": {
    ...
  }
}

Notice in Example 1, data is not longer returned in version 3.5, while in Example 2, the response is largely unchanged between the versions.

Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275