【问题标题】:graphQLErrors is undefined, how extract errors from apollo responsegraphQLErrors 未定义,如何从 apollo 响应中提取错误
【发布时间】:2019-12-10 18:12:34
【问题描述】:

我在服务器上实现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)"
        ]
      }
    }
  }
]"

【问题讨论】:

    标签: graphql apollo react-apollo


    【解决方案1】:

    如果这可能对某人有所帮助,您可以通过

    访问错误属性

    yourMutation().catch(err =&gt; console.log(err.graphQLErrors[0].extensions.code))

    【讨论】:

    • graphQLErrors 对象是否总是只包含一项?还是可以包含更多?
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2019-01-20
    • 2021-10-27
    • 2019-03-01
    • 2019-09-11
    • 2019-02-09
    相关资源
    最近更新 更多