Siy*_*Haq 3 javascript reactjs graphql-js apollo-client
我正在使用 apollo 客户端对 graphql 查询做出反应,现在我有一个问题,虽然我使用 useLazyQuery 钩子以编程方式查询,但是当我第二次查询时, onCompleted 回调实际上没有触发,网络请求实际上已完成并且 1 索引中的状态该钩子的返回值正在更新
const [getUser, getUserStatus] = useLazyQuery(GET_USER, {
fetchPolicy: 'no-cache',
notifyOnNetworkStatusChange: true,
// this callback is not triggering 2nd call of getUser
onCompleted: (data) => {
const { customerCart, customer } = data;
if (!id) {
dispatch(updateCartId(customerCart.id));
}
if (id !== customerCart.id) {
mergeCart({
variables: { cartId: id, cartIdToMerge: customerCart.id },
});
}
dispatch(updateUser(customer));
},
onError: (error) => {
checkHasAuth(error);
},
});
useEffect(() => {
if (token) {
// here i am calling first and 2nd time
getUser();
}
}, [getUser, token]);
Run Code Online (Sandbox Code Playgroud)
小智 6
您只需使后续查询的变量与前一个查询的变量不同即可。
例如:
getUser({
variables: {
state: Math.random(),
},
});
Run Code Online (Sandbox Code Playgroud)