Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mutations through watchQuery use cacheAndNetwork, which yields unexpected results #774

Closed
micimize opened this issue Nov 23, 2020 · 6 comments · Fixed by #795
Closed

mutations through watchQuery use cacheAndNetwork, which yields unexpected results #774

micimize opened this issue Nov 23, 2020 · 6 comments · Fixed by #795
Labels
flutter Issue relates mainly to graphql_flutter 📦 cache Relates to caching functionality PRs encouraged PRs are welcome if stakeholders want to take ownership released on @beta released

Comments

@micimize
Copy link
Collaborator

the default fetch policy for client.mutate is networkOnly, because it is usually the case that the fields requested correspond to mutated data, and thus are expected to be stale if fetched eagerly from the cache.

However, when a mutation is made with client.watchQuery (such as with the Mutation widget), the cacheAndNetwork policy is used by default, resulting in unexpectedly stale data when an optimisticResult is not provided.

Another serious issue is that the same mutation, with the same variables, might return different entities. If those results are used in parts of the application, the results of the latest usage will overwrite those of the older ones.

@micimize micimize added flutter Issue relates mainly to graphql_flutter 📦 cache Relates to caching functionality labels Nov 23, 2020
@micimize
Copy link
Collaborator Author

To illustrate the issues further:

Mutation results are cached (more or less) under mutationName(data: ${jsonEncode(variables)}), so when an order is created with the same parameters, the previous result will be grabbed from the cache eagerly.

Lets say we have a mutation,

mutate createOrder($placeId: ID!){
  createOrder(placeId: $placeId) { ...orderFields }
}

It is called once, then that order is mutated later, but the results of the mutation are still used in the UI as a list item. That mutation sits in the cache like so:

{
  "Mutation": {
    "__typename": "Mutation",
    "createOrder({\"data\":{\"placeId\":\"bar-3-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}})": {
      "$ref": "Order:old-closed-order-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
    }
  }
}

If a new mutation is made to create a fresh order:

  • It's eager result will be the old order
  • the new order will overwrite the old order reference in the cache
  • any UI using the old order mutation results will be overwritten (incorrectly)

A potential workaround for now is to use a differentiating variable. Will have to think/investigate more.

@micimize
Copy link
Collaborator Author

This might require some significant refactoring. In apollo, useMutation calls client.mutate, which then calls markMutationOptimistic in the QueryManager, which just passes the update callback all the way down to a block within recordOptimisticTransaction. This is very different from what we do, which is to just reuse watchQuery for Mutation.

The quickest way for us to solve this IMO is to add a client.watchMutation with a new default fetch policy FetchPolicy.writeCacheFromNetwork that writes to the cache but does not re-read from the cache, and thus also does not rebroadcast (i.e. isRebroadcastSafe is always false). This would also be a good time to add rebroadcasting to the docs on FetchPolicy. This is not an ideal solution, but it is quick to implement. It also allows for the use case of "incidental post-mutation querying" which users might be making use of without noticing.

@micimize micimize added the PRs encouraged PRs are welcome if stakeholders want to take ownership label Nov 29, 2020
@micimize
Copy link
Collaborator Author

Maybe add other general constraints and errors around the mutation code path, as it allows for other jank like #792 (comment)

@micimize
Copy link
Collaborator Author

Closing this with #795 and leaving the more substantial redesign to #798

@micimize micimize unpinned this issue Jan 24, 2021
@HofmannZ
Copy link
Member

🎉 This issue has been resolved in version 4.0.0-beta.7 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@HofmannZ
Copy link
Member

🎉 This issue has been resolved in version 4.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flutter Issue relates mainly to graphql_flutter 📦 cache Relates to caching functionality PRs encouraged PRs are welcome if stakeholders want to take ownership released on @beta released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants