graphql - Apollo 客户端是否缓存 React 中的嵌套对象?
问题描述
进行以下查询:
query Foo {
foo {
id
bar(id: 1) {
id
baz
}
}
}
query Bar {
bar(id: 1) {
id
baz
}
}
有时,运行第二个查询会给我一个缓存版本的bar
. 其他时候,它不会,但我不确定这是因为查询运行多次还是因为这是 React 中 Apollo 客户端的默认行为。
解决方案
不,它没有(至少截至 2019 年 11 月)。要bar
在运行Foo
查询时将对象放入缓存中,您需要像这样创建内存缓存:
import { InMemoryCache } from 'apollo-cache-inmemory';
const cache = new InMemoryCache({
cacheRedirects: {
Query: {
bar: (_, args, { getCacheKey }) =>
getCacheKey({ __typename: 'Bar', id: args.id })
},
},
});
另见:
推荐阅读
- php - 添加到购物车按钮单击时在数据库中添加值
- reactjs - 'onClick' 事件影响所有组件的状态,而不仅仅是一个
- ruby-on-rails - 如何使用 Ruby on Rails 从字符串中提取电子邮件地址?
- azure-stream-analytics - 应用服务状态显示 QuotaExceeded 但是当我打开配额时,一切看起来都很好
- xml - 使用正则表达式通过 XSD 验证 XML 值
- firebird - Firebird 中是否有详细的调试功能?
- ping - 如何在没有www的情况下制作ping URL?
- max - symengine 中的 Argmax 或等效替代方案
- python - 无法挤压 dim[1],预期维度为 1,得到 2
- python - 如何使用 paramiko 运行 sudo su?