I'm completely stuck on an Apollo problem, for which I've opened a GitHub issue and had zero response on.
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.
But for some reason, my code is not working like this. I'm getting two update()
calls, both with the optimistic data.
Here's a repo that demonstrates this behavior: https://github.com/ffxsam/apollo-update-bug
- 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.
In short, the problem might be with a network link called
OfflineLink
that is used byaws-appsync
.Explanation
aws-appsync
has an ApolloLink calledOfflineLink
that intervenes with therequest
function.What happens is something like this:
$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.OfflineLink
in the network chain.OfflineLink
creates a new observer and dispatches the mutation info as an action.OfflineLink
calls the observer'snext
function with theoptimisticResponse
as if it was the execution result!update
the second time with the result which is actually theoptimisticResponse
.OfflineLink
calls the observer'scomplete
which resolves your promise.console.log('done!'...
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.