如何强制Apollo Client将缓存的数据用于详细信息视图页面 [英] How to force Apollo Client to use cached data for detail view page

查看:53
本文介绍了如何强制Apollo Client将缓存的数据用于详细信息视图页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于分页游标的查询 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 ?

类似的问题,因为没有此帖子,但我认为这不是正确的方法,应该有更好的解决方案.(我希望)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆