gpt4 book ai didi

javascript - Apollo GraphQL 合并缓存数据

转载 作者:行者123 更新时间:2023-12-05 00:27:13 25 4
gpt4 key购买 nike

我有一个由 2 个组件组成的页面,每个组件都有自己的数据请求
例如

<MovieInfo movieId={queryParamsId}/>

const GET_MOVIE_INFO = `gql
query($id: String!){
movie(id: $id){
name
description
}
}`
下一个组件
<MovieActors movieId={queryParamsId}/>

const GET_MOVIE_ACTORS = `gql
query($id: String!){
movie(id: $id){
actors
}
}`
对于这些查询中的每一个,我都使用 apollo hook
const { 数据,加载,错误 } = useQuery(GET_DATA, {variable: {id: queryParamsId}}))
一切都很好,但我收到一条警告消息:

Cache data may be lost when replacing the movie field of a Query object.To address this problem (which is not a bug in Apollo Client), either ensure all objects of type Movie have IDs, or define a custom merge function for the Query.movie field, so InMemoryCache can safely merge these objects: { ... }


它可以与 google chrome 一起使用,但此错误会影响 Safari 浏览器。一切都令人崩溃。我 100% 确定这是因为这个警告信息。在第一个请求中,我在缓存中设置了电影数据,在对同一查询的第二个请求中,我只是将旧数据替换为新数据,因此之前的缓存数据是未定义的。我该如何解决这个问题?

最佳答案

解决了!

 cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
YOUR_FIELD: {
merge(existing = [], incoming: any) {
return { ...existing, ...incoming };
// this part of code is depends what you actually need to do, in my
case i had to save my incoming data as single object in cache
}
}
}
}
}
})
});

关于javascript - Apollo GraphQL 合并缓存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63123558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com