Apollo-CLIENT REDCECH查询
我有一个带有商品的购物车,可以选择您选择删除物品, 它可以很好地工作,您无需刷新页面即可查看结果,但是如果您只在购物车中添加1个项目并尝试删除该项目,则除非您刷新页面,否则不会删除。.无法理解为什么要想要要获得一些提示。
我主持了记录的购物车信息,如果有超过1个项目将删除并将控制,但是如果只有一个,则不会登录并且不会删除。 (同样,仅当我刷新时)
删除产品突变
const [deleteProduct, { loading: deleteLoading, error: deleteError }] =
useMutation(DELETE_FROM_CART, {
variables: { productId, userId: cart?.userId },
refetchQueries: [
{
query: GET_USER_CART,
variables: { userId: cart?.userId },
awaitRefetchQueries: true,
},
],
});
-get_user_cart:
const GET_USER_CART = gql`
query ($userId: ID!) {
getUserCart(userId: $userId) {
userId
cartProducts {
productId
size
productPrice
}
}
}
`;
const { data: cartData, loading: cartLoading } = useQuery(GET_USER_CART, {
variables: { userId: userInfo?.id },
});
也尝试更新缓存而不是恢复查询,但结果相同
const [deleteProduct, { loading: deleteLoading, error: deleteError }] =
useMutation(DELETE_FROM_CART, {
variables: { productId, userId: cart?.userId },
update(cache, { data }) {
const updatedCart = data?.deleteProductFromCart;
const existCart = cache.readQuery({
query: GET_USER_CART,
variables: { userId: cart?.userId },
});
if (existCart && updatedCart) {
cache.writeQuery({
query: GET_USER_CART,
variables: { userId: cart?.userId },
data: {
getUserCart: { ...existCart.getUserCart, updatedCart },
},
});
}
},
});
I got a cart with items with the option to delete items by your choice,
It works nice and you dont need to refresh the page to see results, but if you only add 1 item to the cart and trying to remove that item it won't remove unless you refresh the page.. can't understand why would like to get some hints .
I console logged cart info , if there is more than 1 item it will delete and will console, but if there's only one it won't log and won't delete. ( again, only if I refresh )
Delete product mutation -
const [deleteProduct, { loading: deleteLoading, error: deleteError }] =
useMutation(DELETE_FROM_CART, {
variables: { productId, userId: cart?.userId },
refetchQueries: [
{
query: GET_USER_CART,
variables: { userId: cart?.userId },
awaitRefetchQueries: true,
},
],
});
GET_USER_CART:
const GET_USER_CART = gql`
query ($userId: ID!) {
getUserCart(userId: $userId) {
userId
cartProducts {
productId
size
productPrice
}
}
}
`;
const { data: cartData, loading: cartLoading } = useQuery(GET_USER_CART, {
variables: { userId: userInfo?.id },
});
Also tried to update cache instead refetching query but the same result
const [deleteProduct, { loading: deleteLoading, error: deleteError }] =
useMutation(DELETE_FROM_CART, {
variables: { productId, userId: cart?.userId },
update(cache, { data }) {
const updatedCart = data?.deleteProductFromCart;
const existCart = cache.readQuery({
query: GET_USER_CART,
variables: { userId: cart?.userId },
});
if (existCart && updatedCart) {
cache.writeQuery({
query: GET_USER_CART,
variables: { userId: cart?.userId },
data: {
getUserCart: { ...existCart.getUserCart, updatedCart },
},
});
}
},
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多种方法可以从缓存中删除已删除的数据。
当您使用USEQUERY时,您可以稍后使用Refetch Refection数据。
There are multiple ways to remove the deleted data from cache.
When you use useQuery, you can later use refetch to refetch the data.