Alright, I tried all 3 and updateQuery seems to do what I want more easily.
But now knowing how to use these I stumbled upon another problem which is caused by me relying on Apollo’s concatPagination function which I use for pagination on the “posts” query.
It concats any new results from my useQuery to the posts array.
The issue is that it does the same thing when using updateQuery.
Here’s my mutation:
await createPost({
variables: { post: { title, content, team} },
update: (cache, { data: { createPost } }) => {
cache.updateQuery({
query: ALL_POSTS_QUERY,
variables: {
team: team
}
}, ({posts}) => ({ // update function
posts: [createPost, ...posts]
}));
},
});
typePolicy:
typePolicies: {
Query: {
fields: {
posts: concatPagination(["team"]),
}
}
},
I believe the concatPagination function forces every new Post to be concatenated to the bottom of the posts array.
Is there any way I can escape this behavior when doing a mutation?
If not, is there some way I can see the source code of concatPagination that is not in TypeScipt?
expected result:
before mutation - posts: [post3, post2, post1]
after mutation - posts: [post4, post3, post2, post1]
actual result:
before mutation - posts: [post3, post2, post1]
after mutation - posts: [post3, post2, post1, post4, post3, post2, post1]