Apollo - update() 方法被调用两次,两次都使用乐观/假数据 [英] Apollo - update() method getting called twice, both times with optimistic/fake data

查看:51
本文介绍了Apollo - update() 方法被调用两次,两次都使用乐观/假数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全被困在 Apollo 问题上,为此我打开了一个 GitHub 问题并且对它的响应为零.

I'm completely stuck on an Apollo problem, for which I've opened a GitHub issue and had zero response on.

我正在调用 Apollo 突变,使用 optimisticResponse.据我了解,它应该工作的方式是 update() 被调用两次:首先是乐观数据,然后是来自网络的实际数据.

I'm calling an Apollo mutation, using optimisticResponse. The way it's supposed to work, as I understand it, is that update() gets called twice: first with the optimistic data, then again with the actual data coming in from the network.

但由于某种原因,我的代码不能像这样工作.我收到了两个 update() 调用,都使用了乐观数据.

But for some reason, my code is not working like this. I'm getting two update() calls, both with the optimistic data.

这是一个演示此行为的存储库:https://github.com/ffxsam/apollo-更新错误

Here's a repo that demonstrates this behavior: https://github.com/ffxsam/apollo-update-bug

  1. 纱线&&纱线开发
  2. 在浏览器中打开,打开控制台
  3. 输入一些文字并按回车
  4. 重复以上操作
  5. 注意控制台中关于重复键的错误.发生这种情况是因为临时 ID??"没有被真正的 UUID 取代(可选)如果可用,您可以打开 Vue DevTools 并检查数据以查看它是否不正确

推荐答案

我正在挖掘,我想我找到了问题的根源.不幸的是,我没有解决方案.

I was doing some digging and I think I found the source of the problem. Unfortunately, I don't have a solution.

简而言之,问题可能出在 aws-appsync 使用的名为 OfflineLink 的网络链接上.

In short, the problem might be with a network link called OfflineLink that is used by aws-appsync.

aws-appsync 有一个名为 OfflineLink 的 ApolloLink,它干预 request 函数.

aws-appsync has an ApolloLink called OfflineLink that intervenes with the request function.

发生的事情是这样的:

  1. 你调用 $apollo.mutate(...)
  2. ApolloClient.QueryManager 使用乐观响应初始化第一次触发您的 update 的突变.这发生在 ApolloClient 数据存储中,markMutationInit 调用 markMutationResult 调用您的更新.
  3. graphql 操作执行并到达网络链中的OfflineLink.
  4. OfflineLink 创建一个新的观察者和 将突变信息作为一个动作进行分发.
  5. OfflineLink 的下一行使用 optimisticResponse 调用观察者的 next 函数就好像它是执行结果一样!
  6. 这会触发您的update第二次,结果实际上是optimisticResponse.
  7. OfflineLink 调用观察者的 complete 来解决你的承诺.
  8. console.log('done!'...
  1. you call $apollo.mutate(...)
  2. ApolloClient.QueryManager initializes the mutation that triggers your update the first time with the optimistic response. That is happening inside ApolloClient data store, markMutationInit calls markMutationResult that calls your update.
  3. The graphql operation executes and reaches the OfflineLink in the network chain.
  4. OfflineLink creates a new observer and dispatches the mutation info as an action.
  5. The next line of OfflineLink calls the observer's next function with the optimisticResponse as if it was the execution result!
  6. This triggers your update the second time with the result which is actually the optimisticResponse.
  7. OfflineLink calls the observer's complete which resolves your promise.
  8. console.log('done!'...

与此同时,OfflineLink 甚至会阻止原始突变发送请求,并且会生成新的突变并使用您提供的选项发送.

Meanwhile, OfflineLink prevents the original mutation from even sending the request, and a new mutation is generated and sent with the options you've given it.

这篇关于Apollo - update() 方法被调用两次,两次都使用乐观/假数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆