如何解决定义自定义合并函数以解决 GraphQL 中的 InMemoryCache 合并
我收到警告:
Cache data may be lost when replacing the parts field of a Query object.
To address this problem (which is not a bug in Apollo Client),define a custom merge function for the Query.parts field,so InMemoryCache can safely merge these objects:
existing: [{"__ref":"Part:53"},{"__ref":"Part:55"},{"__ref":"Part:56"},{"__ref":"Part:57"},{"__ref":"Part:58"}]
incoming: [{"__ref":"Part:53"},{"__ref":"Part:57"}]
现在这是我的零件类型:
type Part {
id: ID!
created_at: DateTime!
updated_at: DateTime!
partName: String
partDescription: String
partQuantity: Long
usePercentage: Boolean
partPercentage: Float
type: String
published_at: DateTime
products(sort: String,limit: Int,start: Int,where: JSON): [Product]
partImage(sort: String,where: JSON): [UploadFile]
stockevents(sort: String,where: JSON): [Stockevent]
}
在我使用突变删除单个部分删除一个部分后触发此警告。这是:
const [partDelete] = useMutation(DELETE_PART,{
update(cache,{ data }) {
const newData = Object.values(data)
const refresh = newData.map(name => name.part)
const refined = refresh.map(item => item.id)
cache.evict({
id: cache.identify({
id: refined.id
})
})
cache.writeQuery({
query: GET_PARTS
})
},refetchQueries: [
{ query: GET_PARTS }
]
})
我在一个单独的函数中传递有效负载,一切正常,但我不断收到这个缓存警告,所以我现在想处理它。
我已经在 index.js 中更新了 InMemoryCache,但它仍然不起作用:
export const client = new ApolloClient({
link,cache: new InMemoryCache({
typePolicies: {
Part: {
merge(existing = [],incoming = []) {
return [...existing,...incoming];
}
}
}
})
});
我也尝试过只返回 ...incoming 但没有任何不同的发生。
提前致谢,干杯!
解决方法
问题出在 InMemoryCache 配置的结构中。在我把它改成这个后,它起作用了:
resources
我还从包含 evict 和 modify 的突变中删除了更新选项。
,当您看到此警告时:
为了解决这个问题(这不是 Apollo Client 中的错误),为 Query.your_query_name 字段定义一个自定义合并函数,以便 InMemoryCache 可以安全地合并这些对象:
试试这个缩短:
const client = new ApolloClient({
uri: "your_API_link",cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
your_Query_Name: {
merge: (existing = [],incoming) => {
return incoming;
},},}),});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。