javascript - Apollo-Client graphql 订阅来自缓存的类似字段策略读取查询
问题描述
是否可以使用 apollo-client 为来自缓存的订阅提供第一个结果,类似于 watchQuery 的缓存和网络 fetchPolicy?
对于查询,客户端解析器可以使用带有字段策略的读取功能。我正在寻找类似的订阅。
import { ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
book(_, { args, toReference }) {
return toReference({
__typename: 'Book',
id: args.id,
});
}
}
}
}
}
});
我尝试了以下方法,但似乎没有要求订阅这个钩子,或者我错过了什么?
import { ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
Subscriptions: { // Is it Subscription or Subscriptions?
fields: {
books(_, { args, toReference }) {
console.log("subscription read not called?", args);
return args.ids.map(
id => toReference({
__typename: 'Book',
id: id,
})
);
}
}
}
}
}
});
还留给我以下问题:
- 如何从缓存中解析多个结果以及如何返回它们?
- 只是返回 toReference 数组或使用 pub/sub rx 流?
Thx 社区,非常感谢您的帮助!
解决方案
推荐阅读
- flutter - 如何:阻止 ListView 重建 video_player 小部件?
- python - 为什么我的 python 程序找不到模块?
- python - 使用 Vue.js 在客户端运行 python 脚本
- r - 如何通过R中的第一个所需符号拆分字符串向量?
- python - 在 Pycharm 中找不到 RStudio 中安装的 R 包
- css - 即使没有错误,css 也不会加载,并且它在 ejs 中正确链接
- javascript - 为什么 promis 只适用于最后一个变量?
- javascript - 打字稿:将*一些*(不是全部)属性从一个类实例复制到另一个对象的最佳方法是什么?
- python - 无法通过 Jenkins 执行 Python 脚本
- amazon-web-services - 如何查询特定平台的最新 Beanstalk ami 以在打包器中使用?