graphQLErrors未定义,如何从阿波罗响应中提取错误

时间:2019-08-02 06:27:42

标签: graphql apollo react-apollo

我在服务器上实现了class-validator的注册表。我的目标是捕获错误并通过错误将错误消息写到特定字段。

我读了docs,应该从graphQLErrors中得到错误,但就我而言,它是未定义的。

我的突变:

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

错误对象:

"[
  {
    "message": "Argument Validation Error",
    "locations": [
      {
        "line": 2,
        "column": 3
      }
    ],
    "path": [
      "registrationWithEmail"
    ],
    "extensions": {
      "code": "INTERNAL_SERVER_ERROR",
      "exception": {
        "validationErrors": [
          {
            "target": {
              "role": "creator",
              "email": "myemail@email.com",
              "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)"
        ]
      }
    }
  }
]"

1 个答案:

答案 0 :(得分:0)

如果这可能对某人有所帮助,您可以通过以下方式访问错误属性

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