Search code examples
graphqlgraphql.net

GraphQL.Net - prevent null data on mutation exception


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",
      ...
    }
  ]
}

Solution

  • 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.