如何强制 Apollo Client 将缓存数据用于详细信息视图页面 [英] How to force Apollo Client to use cached data for detail view page
问题描述
我有一个基于分页游标的查询 TODOS 和带有查询 TODO 的详细信息页面,用于按 ID 获取数据.每当我进入详细视图并使用 useQuery 和 TODO 查询(其中包含与 TODOS 查询结果完全相同的数据,它仍然尝试获取数据从服务器而不是缓存.我怎样才能实现不从服务器获取数据(因为它已经存在),我认为 Apollo 通过 id 检测并从缓存中返回但没有.有什么建议吗?
I have a paginated cursor based query TODOS and detail page with query TODO to get data by ID. Whenever I go to detail view and use useQuery with TODO query (Which contains exactly same data as TODOS query result, it still tries to get data from server not from cache. How can I achieve to not get data from server (Because it already exists), I thought Apollo detect by id and return from the cache but no. Any suggestions ?
与此类似的问题 post,但我认为那不是正确的方法,应该有更好的解决方案.(我希望)
Similar issue as no this post, but I don't think thats a right approach, there should be better solution. (I hope)
这是TODOS查询:
query TODOS(
$paginationOptions: PaginationOptionsInput
) {
todos(paginationOptions: $paginationOptions) {
pagination {
minCursor
maxCursor
sortOrder
limit
hasMoreResults
}
result {
id
...SomeTodoFields
}
}
在详细信息页面上我有第二个查询TODO
And on detail page I have second query TODO
query (
$todoId: String!
) {
todo(todoId: $todoId) {
id
...SomeTodoFields
}
}
推荐答案
因为我使用 Apollo-client <3.0 对我来说 cacheRedirect 工作得很好,你可以看看更远的 此处.仔细阅读每一个笔记,这真的很重要!我的代码示例:
Since I am using Apollo-client < 3.0 for me cacheRedirect worked fine, you can have a look farther here. Read every note carefully it is really important! My code example:
cache: new InMemoryCache({
fragmentMatcher,
cacheRedirects: {
Query: {
todo: (_, args, { getCacheKey }) => {
return getCacheKey({ __typename: 'TodoType', id: args.todoId })
}
}
}
})
})
还发现了一些很好的相关文章,您可能想检查一下.这对我有用,希望对其他人也有帮助.:)
Found some good relevant article as well, which you might want to check. This worked for me, hope it helps to someone else as well. :)
这篇关于如何强制 Apollo Client 将缓存数据用于详细信息视图页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!