Apollo - update() method getting called twice, both times with optimistic/fake data
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 by aws-appsync
.
Explanation
aws-appsync
has an ApolloLink called OfflineLink
that intervenes with the request
function.
What happens is something like this:
- 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!'...
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.