I'm using the following code to get a list of post objects:
const { data, fetchMore, loading } = usePostsQuery({
variables: {
type: code as string, // TODO: remove type casting
fromDate,
},
})
Then in my deletePost mutation I'm doing a write query:
deletePost({
variables: { postId },
update(cache, { data }) {
// TODO: move to separate function
if (!data) {
return
}
const readQueryResult = cache.readQuery<
PostsQuery,
PostsQueryVariables
>({
query: PostsDocument,
variables: { type: code as string }, // TODO: remove type casting
})
const posts = readQueryResult?.posts || []
cache.writeQuery({
query: PostsDocument,
data: { posts: posts.filter(post => post._id !== postId) },
variables: { type: code },
})
},
})
.then(() => history.push(url.replace('/create', '')))
.catch(e => console.log(e))
To get the posts to render I am looping around data?.posts:
const topics: TopicInfo[] =
data?.posts?.map(post => {
Any idea how I can get the posts to update? Should the writeQuery do it? Will usePostsQuery automatically know to get the latest posts in the cache? At the moment the data.posts array is not removing the deleted post. Any ideas?