突变后无法更新缓存 [英] 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()} />
)
}
当我通过点击单位中的任何项目来运行此突变时:
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
替换查询对象的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屋!