1

Previously, I was running a mutation somewhat like this:

export const useEditLocationName = () => {

  const [editFavoriteLocationName] = useEditFavoriteLocationNameMutation({
    refetchQueries: [{ query: GetMyFavouritePlacesDocument}],
    awaitRefetchQueries: true,
    onCompleted: () => {
      Alert.alert('Name');
    },
    onError: () => {
      Alert.alert('Oops');
    },
  });

  const editLocationName = async (
    favouritePlaceId: number,
    customisedName: string,
  ) => {
    editFavoriteLocationName({
      variables: {
        id: favouritePlaceId,
        label: customisedName
      },
    });
    return null;
  };
  return editLocationName;
};

Every time I run this mutation, the results of the useGetMyFavouritePlacesQuery i.e the GetMyFavouritePlacesDocument got updated automatically (wherever this query was being used). This worked perfectly then there were no parameters needed for the useGetMyFavouritePlacesQuery. However, now that there are some changes in the api, I need to pass an idto the query in order for it return results. How can I do so?

I cannot pass variables directly to the GetMyFavouritePlacesDocument and I can also not use the hook here. How else can I efficiently refetch data every time I run this mutation?

1 Answers1

0

If you return the id in the mutation response and possibly a little other unique data about the data then Apollo will automatically update the cache

Here is an example https://www.freecodecamp.org/news/how-to-update-the-apollo-clients-cache-after-a-mutation-79a0df79b840/

twcardenas
  • 115
  • 2
  • 10
  • This seemed to work but then it gives me this error https://stackoverflow.com/questions/63423578/cache-data-may-be-lost-when-replacing-the-my-field-of-a-query-object even though I am already returning unique ids & some other info. The error suggests me to define a custom merge function. How could I do so? –  Sep 16 '20 at 10:25
  • I added more code & details here https://stackoverflow.com/questions/63918330/unable-to-update-cache-after-mutation –  Sep 16 '20 at 10:44