【问题标题】:Does the Apollo client cache nested objects in React?Apollo 客户端是否缓存 React 中的嵌套对象?
【发布时间】:2020-03-09 14:15:46
【问题描述】:

进行以下查询:

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

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

有时,运行第二个查询会得到bar 的缓存版本。其他时候,它不会,但我不确定这是因为查询运行多次还是因为这是 React 中 Apollo 客户端的默认行为。

【问题讨论】:

    标签: graphql apollo apollo-client apollo-cache-inmemory


    【解决方案1】:

    不,它没有(至少截至 2019 年 11 月)。要在运行Foo 查询时将bar 对象放入缓存,您需要像这样创建内存缓存:

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

    另见:

    【讨论】:

      猜你喜欢
      • 2019-07-29
      • 2021-07-30
      • 2017-03-18
      • 2021-02-01
      • 2021-04-21
      • 2020-11-03
      • 2023-03-07
      • 2020-02-03
      相关资源
      最近更新 更多