hotchocolate: How to raise multiple errors in Errors[] array

Viewed 1573

Is it possible to raise multiple errors in Errors[] array, like hotchocolate does when you try to use unknow properties ?

If yes, how can I do this ?

My use case is to return errors collection when validating an object with Validator.TryValidateObject

See below what hotchocolate returns when fields are unknow. I would like to do the same : multiple elements in Errors[] array.

{
  "Label": null,
  "Path": null,
  "Data": null,
  "Errors": [
    {
      "Message": "The field `date` does not exist on the type `EcritureConnection`.",
      "Code": null,
      "Path": {
        "Parent": null,
        "Depth": 0,
        "Name": "ecritures"
      },
      "Locations": [
        {
          "Line": 1,
          "Column": 75
        }
      ],
      "Extensions": {
        "type": "EcritureConnection",
        "field": "date",
        "responseName": "date",
        "specifiedBy": "http://spec.graphql.org/June2018/#sec-Field-Selections-on-Objects-Interfaces-and-Unions-Types"
      },
      "Exception": null
    },
    {
      "Message": "The field `intitule` does not exist on the type `EcritureConnection`.",
      "Code": null,
      "Path": {
        "Parent": null,
        "Depth": 0,
        "Name": "ecritures"
      },
      "Locations": [
        {
          "Line": 1,
          "Column": 80
        }
      ],
      "Extensions": {
        "type": "EcritureConnection",
        "field": "intitule",
        "responseName": "intitule",
        "specifiedBy": "http://spec.graphql.org/June2018/#sec-Field-Selections-on-Objects-Interfaces-and-Unions-Types"
      },
      "Exception": null
    }
  ],
  "Extensions": null,
  "ContextData": {
    "HotChocolate.Execution.ValidationErrors": true
  },
  "HasNext": null
}
1 Answers

There are multiple ways of setting errors in HotChocolate

How to build errors

Both ways require you to create a IError object. You can use the ErrorBuilder to build this error. You do not need to set all of the fields, but the more the better the error does look:

 ErrorBuilder.New()
    .SetMessage("This is the message")
    .SetCode("YOURCODE00000123")
    .SetException(ex)
    .AddLocation(context.Selection.SyntaxNode)
    .SetPath(context.Path)
    .Build()

This would generate this error in the response:

{
  "errors": [
    {
      "message": "This is the message",
      "locations": [
        {
          "line": 3,
          "column": 21
        }
      ],
      "path": [
        "hello"
      ],
      "extensions": {
        "code": "YOURCODE00000123"
      }
    }
  ],
  "data": {
    "hello": "World"
  }
}

How to raise errors

  1. You can report errors on the IResolverContext or the IMiddlewareContext

Annotation Based

 public class Query
{
    public string Hello(IResolverContext context)
    {
        context.ReportError(
            ErrorBuilder.New()
                .SetMessage("This is the message")
                .SetCode("YOURCODE00000123")
                .AddLocation(context.Selection.SyntaxNode)
                .SetPath(context.Path)
                .Build());


        return "World";
    }
}

Code First

public class QueryType : ObjectType<Query>
{
    protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
    {
        descriptor.Field(x => x.Hello)
            .Resolver(context =>
            {
                context.ReportError(
                    ErrorBuilder.New()
                        .SetMessage("This is the message")
                        .SetCode("YOURCODE00000123")
                        .AddLocation(context.Selection.SyntaxNode)
                        .SetPath(context.Path)
                        .Build());

                return "World";
            });
    }
}

  1. By throwing a GraphQlException
public class Query
{
    public string Hello()
    {
        var error1 = ErrorBuilder.New()
            .SetMessage("This is the message")
            .SetCode("YOURCODE00000123")
            .Build();
        var error2 = ErrorBuilder.New()
            .SetMessage("This is the message")
            .SetCode("YOURCODE00000123")
            .Build();
        throw new GraphQLException(error1, error2)
    }
}

Related