useMutation 中的 onCompleted 回调没有 return 值

onCompleted callback in useMutation doesn't have return values

我正在尝试使用 apollo 在 React Native 中实现登录。 在本机应用程序中

const SIGN_IN = gql`
mutation($username: String!, $password: String!) {
  signin(password: $password, username: $username) {
    user {
      username
    }
    token
  }
}
`;

//代码被缩写了。

function LoginScreen() {
  const [signIn, { loading, error }] = useMutation(SIGN_IN, {
    onCompleted({ data }) {
      if (loading) console.log("Loading.....");
      console.log("Printing data");
      console.log(data.signin.token);
      }
  });
}

后端服务器运行良好。 但是我在控制台日志中收到一条错误消息

    [Unhandled promise rejection: TypeError: Cannot read property 'signin' of undefined]

    Stack trace:
    screens/LogInScreen.js:36:6 in useMutation$argument_1.onCompleted
    node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:635:25 in callOncomplete

数据未定义。 所以我尝试了 { data && console.log(data.signin.token) } 但它什么也没打印。 我读到文档说“onCompleted 对 useMutation 的回调将在突变完成其 return 值后调用。”

我该如何调试它?我错过了什么?有什么想法吗?

onCompleted 将整个数据对象作为其第一个参数传递,因此您的签名应如下所示:

onCompleted(data)

或使用解构

onCompleted({ signin })