type-graphql: Generic argument validation error is thrown with no indication of the field that is causing it

Describe the bug Given an InputType field with validation

@InputType()
export class CatalogInput implements Partial<Catalog> {
  @Field()
  id: number;
  @MaxLength(1000)
  @Field({ nullable: true })
  description?: string;
}

and a mutation

@Mutation(returns => CatalogUpdateResult)
updateCatalog(
  @Arg("catalog", () => CatalogInput, { validate: true })
  catalog: CatalogInput
): CatalogUpdateResult {
  return { catalog };
}

when I try to sent mutation to the server with a value exceeding the limit, then I get a very generic error that does not indicate which field caused it, the locations array is empty:

{
  "errors": [
    {
      "message": "Argument Validation Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "updateCatalog"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "errors": [
            {
              "message": "Argument Validation Error",
              "locations": [],
              "path": [
                "updateCatalog"
              ]
            }
          ],
 ...

Expected behavior I would expected to have the information about what caused the failure, i.e. the location array containing the information.

Enviorment (please complete the following information):

  • OS: Windows
  • Node 10.15.3
  • Package version 0.17.1
  • TypeScript version 3.3.3333

Thanks, Karel

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23 (10 by maintainers)

Most upvoted comments

since you are using Apollo server anyway.

I don’t use Apollo Server. TypeGraphQL works with Apollo server, GraphQL Yoga, express-graphql and even Apollo Client. It’s weird to change the error for compatibility only with one lib.

You can create your own global middleware that will catch the error and transform it to the format you want.

Can you share the code you tested it with?

It’s master branch of this repo, examples->validation.