How to auto refetch in Apollo after creating a new resource?

I’m having a hard time figuring out how to simply refresh the cache of say a “Posts” table after I create a new Post.

I’m not sure if optimistic updates are required? I really just want to say client.invalidateQuery("getPosts") and be done with it. I also don’t want to clear the entire apollo cache if I can help it.

For example, the user sees a list of posts and a create button. They click create and it goes to the create page. Once they click submit, it creates a new Post and navigates back to the list of Posts and their new post is not showing. I would like it to do a re-fetch at this time.

I’m using the new Apollo React 2.1 syntax as well. Thanks in advance!

Just looking through the Apollo documentation a few minutes and saw this -
https://www.apollographql.com/docs/react/essentials/queries.html#refetching

1 Like

So far the best work around (besides passing refetch), is to set the fetchPolicy to cache-and-network so it will always fetch data on first render.