Apollo - update() 方法被调用两次,两次都使用乐观/假数据 [英] Apollo - update() method getting called twice, both times with optimistic/fake data
问题描述
我完全被困在 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
- 纱线&&纱线开发
- 在浏览器中打开,打开控制台
- 输入一些文字并按回车
- 重复以上操作
- 注意控制台中关于重复键的错误.发生这种情况是因为临时 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.
发生的事情是这样的:
- 你调用
$apollo.mutate(...)
ApolloClient.QueryManager
使用乐观响应初始化第一次触发您的update
的突变.这发生在 ApolloClient 数据存储中,markMutationInit 调用 markMutationResult 调用您的更新.- graphql 操作执行并到达网络链中的
OfflineLink
. OfflineLink
创建一个新的观察者和 将突变信息作为一个动作进行分发.OfflineLink
的下一行使用optimisticResponse
调用观察者的next
函数就好像它是执行结果一样!强>- 这会触发您的
update
第二次,结果实际上是optimisticResponse
. OfflineLink
调用观察者的complete
来解决你的承诺.console.log('done!'...
- you call
$apollo.mutate(...)
ApolloClient.QueryManager
initializes the mutation that triggers yourupdate
the first time with the optimistic response. That is happening inside ApolloClient data store, markMutationInit calls markMutationResult that calls your update.- The graphql operation executes and reaches the
OfflineLink
in the network chain. OfflineLink
creates a new observer and dispatches the mutation info as an action.- The next line of
OfflineLink
calls the observer'snext
function with theoptimisticResponse
as if it was the execution result! - This triggers your
update
the second time with the result which is actually theoptimisticResponse
. OfflineLink
calls the observer'scomplete
which resolves your promise.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屋!