首页 > 解决方案 > 如何在处理函数中链接 useLazyQuery 钩子

问题描述

我想在处理函数中链接 apollo useMutation 和 useQuery 钩子。我似乎无法将 useLazyQuery 挂钩的数据属性设置为别名 - 或者就此而言,突变属性加载、错误、数据。我可以使用 useMemo 获得相同的效果,但我想探索如何使用承诺链中的惰性查询。

 const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  const [requestLogin, { loading, error }] = useMutation(LOGIN_USER)
  const [getUserProfile, { 
    loading: loadingProfile, 
    error: errorProfile, 
    data: profileData 
  }] = useLazyQuery(GET_USER_PROFILE)

  const submitHandler = (event) => {
    event.preventDefault()
    requestLogin({
      variables: {
        email: email, 
        password: password
      }
    }).then(({ data }) => {
      console.log("INITIAL DATA", data)
      login(data.login.token, data.login.user)
    }).then(({ data, profileData }) => getUserProfile({
      variables: {
        account_id: data.login.user.account.id
      }
    })).then(profileData => {
      console.log("DATA", profileData)
      history.push('/')
    })
  }

标签: reactjsreact-apollo

解决方案


推荐阅读