Does the Apollo client cache nested objects in React?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 18:39:33

问题


Take the following queries:

query Foo {
  foo {
    id
    bar(id: 1) {
      id
      baz
    }
  }
}

query Bar {
  bar(id: 1) {
    id
    baz
  }
}

Sometimes, running the 2nd query gets me a cached version of bar. Other times, it doesn't, but I'm not sure if this is because the query is run multiples times or because that's the default behaviour of the Apollo client in React.


回答1:


No, it doesn't (as of Nov 2019, at least). To put the bar object in the cache when running the Foo query, you need to create the in-memory cache like this:

import { InMemoryCache } from 'apollo-cache-inmemory';

const cache = new InMemoryCache({
  cacheRedirects: {
    Query: {
      bar: (_, args, { getCacheKey }) =>
        getCacheKey({ __typename: 'Bar', id: args.id })
    },
  },
});

Also see:

  • Configuring the cache
  • Cache redirects
  • Why do we need cacheRedirects at all?


来源:https://stackoverflow.com/questions/58842929/does-the-apollo-client-cache-nested-objects-in-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!