Edgaras Karka
Edgaras Karka

Reputation: 7852

graphQLErrors is undefined, how extract errors from apollo response

I am implemented class-validator on the server for a registration form. My goal is to catch errors and write an error message to a specific field by error.

I read in docs that should get errors from graphQLErrors but in my case it is undefined.

my mutation:

        const { data, errors } = await apolloClient.mutate<RegistrationWithEmail, RegistrationWithEmailVariables>({
          mutation: registrationWithEmailMutation,
          variables: {
            payload: {
             ....
            },
          },
        })

errors object:

"[
  {
    "message": "Argument Validation Error",
    "locations": [
      {
        "line": 2,
        "column": 3
      }
    ],
    "path": [
      "registrationWithEmail"
    ],
    "extensions": {
      "code": "INTERNAL_SERVER_ERROR",
      "exception": {
        "validationErrors": [
          {
            "target": {
              "role": "creator",
              "email": "[email protected]",
              "password": "myweakpassword",
              "firstName": "Edgaras",
              "lastName": "Karka"
            },
            "value": "myweakpassword",
            "property": "password",
            "children": [],
            "constraints": {
              "matches": "WEAK_PASSWORD"
            }
          }
        ],
        "stacktrace": [
          "Error: Argument Validation Error",
          "    at Object.<anonymous> (/node_modules/type-graphql/dist/resolvers/validate-arg.js:21:19)",
          "    at Generator.throw (<anonymous>)",
          "    at rejected (/node_modules/tslib/tslib.js:105:69)",
          "    at processTicksAndRejections (internal/process/task_queues.js:86:5)"
        ]
      }
    }
  }
]"

Upvotes: 1

Views: 1348

Answers (1)

Renata Portela
Renata Portela

Reputation: 186

In case this might help someone, you can access the error properties by

yourMutation().catch(err => console.log(err.graphQLErrors[0].extensions.code))

Upvotes: 1

Related Questions