MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!

Home Page:https://typegraphql.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

formatError not receiving validationErrors

opened this issue · comments

Describe the Bug
When I use class-validators, according to this doc page, the validationErrors should come as part of the extensions object when I receive it in the formatError method.
But they are not.

To Reproduce
Add some class-validators to an InputType and call the corresponding mutation.
Create a custom formatError method (below is shown how I workaround it).

Expected Behavior
validationErrors should be included as part of the gqlError in the method formatError.

To solve it I had to do this:

formatError: (gqlError, error) => {
        const originalError = unwrapResolverError(error);

        // Validation
        if (originalError instanceof ArgumentValidationError) {
            let originalMessage = originalError.message;

            // show the first error
            if (originalError.validationErrors.length > 0) {
                const messages = Object.values(originalError.validationErrors[0].constraints || {});
                if (messages.length > 0) {
                    originalMessage = messages[0];
                }
            }

            return new ValidationError(originalMessage);
        }

        return gqlError;
    },

Environment (please complete the following information):

  • OS: [e.g. Windows] Windows
  • Node (e.g. 10.5.0) Node 18
  • Package version [e.g. 0.12.2] 2.0.beta
  • TypeScript version (e.g. 2.8.2) 5

Additional Context
My dependencies

"dependencies": {
        "@apollo/server": "^4.5.0",
        "@as-integrations/aws-lambda": "^2.0.1",
        "@graphql-tools/schema": "^9.0.17",
        "apollo-server-errors": "^3.3.1",
        "class-validator": "^0.14.0",
        "graphql": "^16.6.0",
        "reflect-metadata": "^0.1.13",
        "type-graphql": "^2.0.0-beta.1"
    },
    "devDependencies": {
        "serverless": "^3.28.1",
        "serverless-plugin-typescript": "^2.1.4",
        "serverless-prune-plugin": "^2.0.2",
        "ts-jest": "^29.0.5",
        "ts-node": "^10.9.1",
        "typescript": "^4.9.5"
    }

** This could be considered a bug in the code OR a bug in the documentation ** So it'd be ok if either is fixed.

Duplicate of #1397
Note that in V2 (when PR #1400 will be merged) all "runtime" errors extends GraphQLError -> extensions field contains information about the error (i.e. code and validationErrors)