替换 Query 对象的 getPosts 字段时,缓存数据可能会丢失。阿波罗
Cache data may be lost when replacing the getPosts field of a Query object. Apollo
我正在设置我的按钮来点赞 post。如果不喜欢则显示基本,如果喜欢则显示彩色。但是,当单个用户点赞并且 post 他无法取消赞时,点赞计数器中点赞的数量不断增加,并且不能 return 达到基本水平。请帮帮我。单击喜欢 post 后,如何设置为与 post 不同的用户。
这是我的代码:
function LikeButton({
user,
post: {
id,
likeCount,
likes
}
}) {
const [liked, setLiked] = useState(false);
useEffect(() => {
if (user && likes.find((like) => like.username === user.username)) {
setLiked(true);
} else setLiked(false);
}, [user, likes]);
const [likePost] = useMutation(LIKE_POST_MUTATION, {
variables: {
postId: id
}
});
const likeButton = user ? (
liked ? ( <
Button color = "black" >
<
Icon name = "heart" / >
<
/Button>
) : ( <
Button color = "black"
basic >
<
Icon name = "heart" / >
<
/Button>
)
) : ( <
Button as = {
Link
}
to = "/login"
color = "black"
basic >
<
Icon name = "heart" / >
<
/Button>
);
return ( <
Button as = 'div'
labelPosition = 'right'
onClick = {
likePost
} > {
likeButton
} <
Label basic color = 'black'
pointing = 'left' > {
likeCount
} <
/Label> <
/Button>
)
}
const LIKE_POST_MUTATION = gql `
mutation likePost($postId: ID!){
likePost(postId : $postId){
id
likes{
id username
}
likeCount
}
}
`
export default LikeButton;
const cache: new InMemoryCache({
typePolicies: {
getPosts: {
likes: {
merge(existing, incoming, {
mergeObjects
}) {
// Correct, thanks to invoking nested merge functions.
return mergeObjects(existing, incoming);
}
}
}
}
})
我正在设置我的按钮来点赞 post。如果不喜欢则显示基本,如果喜欢则显示彩色。但是,当单个用户点赞并且 post 他无法取消赞时,点赞计数器中点赞的数量不断增加,并且不能 return 达到基本水平。请帮帮我。单击喜欢 post 后,如何设置为与 post 不同的用户。 这是我的代码:
function LikeButton({
user,
post: {
id,
likeCount,
likes
}
}) {
const [liked, setLiked] = useState(false);
useEffect(() => {
if (user && likes.find((like) => like.username === user.username)) {
setLiked(true);
} else setLiked(false);
}, [user, likes]);
const [likePost] = useMutation(LIKE_POST_MUTATION, {
variables: {
postId: id
}
});
const likeButton = user ? (
liked ? ( <
Button color = "black" >
<
Icon name = "heart" / >
<
/Button>
) : ( <
Button color = "black"
basic >
<
Icon name = "heart" / >
<
/Button>
)
) : ( <
Button as = {
Link
}
to = "/login"
color = "black"
basic >
<
Icon name = "heart" / >
<
/Button>
);
return ( <
Button as = 'div'
labelPosition = 'right'
onClick = {
likePost
} > {
likeButton
} <
Label basic color = 'black'
pointing = 'left' > {
likeCount
} <
/Label> <
/Button>
)
}
const LIKE_POST_MUTATION = gql `
mutation likePost($postId: ID!){
likePost(postId : $postId){
id
likes{
id username
}
likeCount
}
}
`
export default LikeButton;
const cache: new InMemoryCache({
typePolicies: {
getPosts: {
likes: {
merge(existing, incoming, {
mergeObjects
}) {
// Correct, thanks to invoking nested merge functions.
return mergeObjects(existing, incoming);
}
}
}
}
})