meteor - Apollo graphQL 多突变乐观优化
问题描述
我正在使用 Meteor 和 Apollo v.2 客户端开发应用程序。
该应用程序有一个项目列表。每个项目都可以被忽略并移动到另一个列表。包含商品的订单由 ORDERS 订阅接收。
我刚刚为它添加了一个乐观的用户界面,但效果不佳。
我希望它能够立即将所有需要的项目移动到相应的部分。但是当我在按钮上单击太快时-
,例如项目开始跳转(我怀疑这是因为我的所有更改都作为单独的请求发送,并且本地乐观更改被服务器端版本重写)。
这是一个 gif 演示 — http://www.giphy.com/gifs/w8f2lp51GuRFVRVnNM
toogleItemIgnore(item) {
const { toggleItemIgnore, resetShipmentAndWeight, order } = this.props;
toggleItemIgnore({
variables: { itemId: item._id },
optimisticResponse: {
__typename: 'Mutation',
toogleItemIgnore: {
...item,
isIgnored: !item.isIgnored
}
},
update: (proxy, { data: { toogleItemIgnore } }) => {
const updatedItem = toogleItemIgnore;
const orderQuery = {
query: ORDER_QUERY,
variables: {
_id: order._id
}
};
const query = proxy.readQuery(orderQuery);
const resultItems = [...query.order.items.filter(item => item._id !== updatedItem._id), updatedItem];
const result = {
...query,
order: {
...query.order,
items: resultItems
}
};
proxy.writeQuery({...orderQuery, data: result});
}
})
似乎我需要以某种方式优化我的查询和突变。因此,所有突变都在本地工作(乐观),然后我将其中的一批发送到服务器并立即更新所有项目。
用 Apollo 实施它的正确策略是什么?
解决方案
推荐阅读
- octave - 我的八度函数返回“ans = 0”-Coursera 机器学习第 2 周问题
- javascript - Google Map API 返回错误的 GPS 坐标
- java - KeyListener 未与程序的其余部分正确交互
- python-3.x - 在 S3 和 Redshift 之间测试数据
- python - 遍历Python ast时获取函数定义来自哪个模块
- firebase - 未处理的 JS 异常:需要未知模块“1”
- c++ - 将公共但非静态成员函数与 ALGLIB 集成
- excel - Excel:根据部分匹配对单元格进行分类
- javascript - 使用外部文件更新 DIV
- java - 如何跟踪java项目中执行的代码行?