突变后无法更新缓存 [英] unable to update cache after mutation
问题描述
我有一个屏幕,我可以在其中运行查询并根据结果呈现一些项目:
I have a screen where I run a query and render some items based on the results:
query getMyFavoritePlaces($myID: String) {
favorite_place(where: { user_id: { _eq: $myID } }) {
id
location
label
user{
id
}
user_id
}
}
const { data, error } = useGetMyFavoritePlacesQuery({ variables: { myID: profile.id }});
{
data && (
<FlatList
data={data.favorite_place}
renderItem={({ item }) => <FavoriteLocationEntry place={item} />}
keyExtractor={(item) => item.id.toString()} />
)
}
当我通过单击 flatlets 中的任何项目来运行此更改时:
When I run this mutation by clicking on any of the items from the flatlets:
mutation RemoveFavoriteLocation($id: Int!) {
delete_favorite_place_by_pk(id: $id) {
id
location
label
user{
favorite_places {
id
location
label
}
}
user_id
}
}
该对象已成功删除,并且 UI 也已更新.但是,我收到警告
The object gets deleted successfully and the UI is also updated. However, I get a warning that
替换 Query 对象的 favourite_place 字段时,缓存数据可能会丢失.为了解决这个问题,为 Query.favorite_place 字段定义一个自定义合并函数,这样 InMemoryCache 可以安全地合并这些对象:{ ... }
Cache data may be lost when replacing the favourite_place field of a Query object. To address this problem define a custom merge function for the Query.favorite_place field, so InMemoryCache can safely merge these objects: { ... }
此处显示的值为 ID.我已经在查询和变异中都返回了 id.那我该如何解决?
The values shown here are the ids. I am already returning ids in both, the query and mutation. How can I fix it then?
可能无法在突变内运行 favourite_place
查询本身.
It might not be possible to run the favourite_place
query itself within the mutation.
推荐答案
您的变更需要包含用户的 id 字段,否则可能会错误地删除其他用户的 favourite_place 字段:
Your mutation needs to include an id field for the user or it may mistakenly delete a favourite_place field for a different user:
mutation RemoveFavoriteLocation($id: Int!) {
delete_favorite_place_by_pk(id: $id) {
id
location
label
user{
**id**
favorite_places {
id
location
label
}
}
user_id
}
}
P.s 请不要包含星号,只是为了将焦点放在缺失的属性上.
P.s please don't include the asterisks, just there to bring focus to the missing property.
这篇关于突变后无法更新缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!