apollo-client: `watchQuery` doesn't get the new result after a mutation

I’m experimenting a bit with mutations. I’ve noticed that in the React docs it’s mentioned that it’s a typical pattern to refetch() data after a mutation succeeded.

However the docs for watchQuery say that when a mutation happens, watchQuery will return the updated result automatically.

I first tried this with the regular apollo-client and later with the React integration, but I couldn’t get the UI to update based on a mutation without calling refetch.

Are there some boundaries for what can be automatically re-fetched based on mutations and what not? I’d also have a repo which shows the issue if that helps.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Yeah we have a lot of thoughts about how mutation results should work, but as you mentioned it can be quite complex. Hopefully in the next month or so we can provide some solutions.

Now that we can transform queries to add typenames (as of 0.3.12), we can solve this problem. To add the query transformation:

import { addTypenameToSelectionSet } from 'apollo-client/queries/queryTransform';
const client = new ApolloClient({
  ...
  queryTransformer: addTypenameToSelectionSet,
  ...
});

This adds the __typename field to every SelectionSet within each query and if we add an id field to a query (e.g. when asking for a user’s first and last name, we also ask for an id), the state change within Redux caused by the result of the mutation should lead to updated data.

Thanks for the quick reply!

Btw. here is the repo if you want to see the bug.

All mutations now call refetch so the data stays as much up-to-date as possible. But there’s still the issue if you change the title of an article and navigate back to home, the title of the article in the list won’t have changed (even though forceFetch is turned on).

This is just my personal playground, so I don’t need a solution anytime soon. I just wanted to point this out, if you don’t already know about this.