apollo
apollo copied to clipboard
fetchPolicy no-cache config not working
Version
Reproduction link
https://stackoverflow.com/questions/57415569/nuxt-js-apollo-client-how-to-disable-cache
Steps to reproduce
I'm calling the query and mutation methods inside vuex actions, through this.app.apolloProvider.defaultClient
After making a mutation, I dispatch another action querying the updated data but the nuxt page would display the old data still, and the request wasn't displayed in the network tab of devtools. I'd have to reload the whole page in the browser to get the client to fetch the new data.
I disabled the caching in nuxt.config.js
as documented but the problem wasn't solved.
After several days searching for solutions, I finally found this answer in stackoverflow that saved me. After setting the fetchPolicy to 'no-cache' in a plugin, the problem was finally solved.
What is expected ?
Setting fetchPolicy to 'no-cache' in nuxt.config.js
should work.
What is actually happening?
The fetchPolicy configuration in nuxt.config.js
isn't working, I had to override it using a plugin.
@sharoneh you saved my day! Overriding default client's options works like a charm!
This came in clutch! I've been on this issue for while now. Worked like a charm, thanks @sharoneh
i think u can use it in your smart query too like this
apollo: { postBy: { query: gql' query getPostBySlug ($slug: String = "") { postBy(slug: $slug) { id title slug date } }',error() { return false },fetchPolicy: "no-cache||network-only||cache-and-network",errorPolicy:"all|none||ignore",prefetch: true,variables() { return { slug: this.$route.params.slug } }, }, }
i think u can use it in your smart query too like this
apollo: { postBy: { query: gql' query getPostBySlug ($slug: String = "") { postBy(slug: $slug) { id title slug date } }',error() { return false },fetchPolicy: "no-cache||network-only||cache-and-network",errorPolicy:"all|none||ignore",prefetch: true,variables() { return { slug: this.$route.params.slug } }, }, }
i used graphql-tag and i change " ` " in my code to " ' "
@sharoneh Your solution works properly for the global configuration change, but what if I want to make a specific query/mutation as no-cache, and without changing the global config using the plugin approach, this didn't work for me, any solution for that?