Manually update Apollo Graphql Cache
Usually you structure your graphql queries and mutations in a way that the cache just works out of the box. But there are times that you need to manually update the graphql cache then you can call updateQuery
:
const client = useApolloClient();
const preferences = {abc: "xyz"};
function updateCache() {
client.cache.updateQuery({ query: MY_QUERY }, (data) => {
if (!data.preferences) {
return { ...data, preferences };
}
});
}
Note that the return
is optional, so if your update function returns undefined
then it will keep the cache intact, without updating anything.