cache.writeQuery in update on OptimisticResponse
See original GitHub issueOn an optimisticResponse, the cache.writeQuery inside update, triggers the query only for the optimistic data. It doesn’t trigger the query ( and hence re-render) when the update function runs the second time with the fetched data.
I would like it to trigger the query every time the cache.writeQuery runs. Below is how the mutation, optimistic response and update functions look.
await createPost({
variables,
optimisticResponse: {
__typename: 'Mutation',
createPost: {
__typename: 'Post',
id: Math.round(Math.random() * -1000000),
person: {
__typename: 'Person',
id: personId
}
}
},
update: async (store, { data: { createPost } }) => {
let data = await store.readQuery({
query: MyQuery,
variables: {
id : myId,
}
});
function updateId(id) {
return function(data) {
data.my.posts = data.my.posts.filter((p) => p.id !== id);
return data;
}
}
data.my.posts.push(createPost);
if (isNaN(Number(createPost.id))) {
data = removeOptId(data)
} else {
removeOptId = updatePostId(createPost.id)
}
await store.writeQuery({
query: MyQuery,
variables: { id: myId },
data
});
}
})
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Mutations in Apollo Client - Apollo GraphQL Docs
You can use this value to update the cache with cache.writeQuery , cache.writeFragment , or cache.modify . If your mutation specifies an optimistic...
Read more >Mutation and update cache - typescript-react-apollo - Hasura
Finally we are writing the updated todo list to the cache using cache.writeQuery . Mapping Types. Now that the update mutation is completed,...
Read more >Optimistic response not working when adding items to list
readQuery<GetAllListsQuery>({ query: GetAllListsDocument, })?.lists as TList[]) ?? []; if (data) { cache.writeQuery({ query: GetAllListsDocument ...
Read more >Optimistic UIs with React, Apollo Client and TypeScript (Part II)
writeQuery and cache.modify , to update cached state. ... provide an optimisticResponse option to the addMessages options argument, ...
Read more >How I met Apollo Cache - Medium
TL;DR. Do not use writeQuery , use writeData with optimisticResponse instead. ... refetchQueries is the simplest way of updating the cache.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
On further investigation I have found that it is not safe to modify the return value of
readQuery
and write it back to the store. Doing this may affect some cached data and give unexpected results. You should always use a copy-on-write approach with the return value ofreadQuery
.The reason my trick with doing a shallow copy of the query worked is that using a different object there affected the cache key apollo was using, so it returned a new object. Using the same document for the query meant that calls to
readQuery
and theQuery
component were sharing exactly the same object.When the
mutate
call returns, theQuery
component checks if anything changes - however, it sees no change because its object was already updated when we modified thereadQuery
result data, so it does not re-render.In the past there have been some examples and possibly even documentation suggesting that it is safe to mutate the return value of
readQuery
inside a mutation’supdate
function. However, I see some of those examples were adjusted to create a new object, and I can’t find any mention of this in the documentation (any more?).In conclusion: to avoid weird caching problems, do not modify data returned from
readQuery
directly - any changes you make should be made to new objects of your own creation.I am having a similar issue, although without using optimistic response at all.
writeQuery
is not triggering an update of affected components. The workaround, weirdly, is to use a shallow clone of the query inreadQuery
, e.g.might fix your issue, if it’s the same weird one I’m investigating.