Apollo-update()方法被两次调用,两次都使用乐观/伪数据 [英] Apollo - update() method getting called twice, both times with optimistic/fake data
问题描述
我完全陷入了阿波罗问题,为此我开了一个GitHub问题,对它的响应为零.
I'm completely stuck on an Apollo problem, for which I've opened a GitHub issue and had zero response on.
我正在使用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
- 纱线&&纱线开发人员
- 在浏览器中打开,打开控制台
- 输入一些文字,然后按Enter键
- 在上方重复
- 请注意控制台中有关重复键的错误.发生这种情况是因为临时ID"??"没有被真正的UUID取代 (可选)您可以打开Vue DevTools(如果有)并检查数据以查看其不正确
- yarn && yarn dev
- Open in browser, open console
- Enter some text and hit enter
- Repeat above
- Notice the error in the console about duplicate keys. This is happening because the temporary ID "??" is not being replaced with the real UUID (optional) You can open Vue DevTools if available and inspect the data to see it's incorrect
推荐答案
我正在做一些挖掘工作,我认为我找到了问题的根源. 不幸的是,我没有解决方案.
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数据存储内部发生的, markMutationResult ,其中将突变信息发送为动作. -
OfflineLink
的下一行使用optimisticResponse
调用观察者的next
函数,就像执行结果一样! - 这会触发第二次 ,其结果实际上是
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屋!