Vue-apollo: Disable cache??

Created on 11 Aug 2018  路  10Comments  路  Source: vuejs/vue-apollo

I want to disable the cache, can anyone help me, please. Thanks in advance

Most helpful comment

@macin40

To do it entirely just set the defaultOptions when setting up the client:

new ApolloClient({
  cache,
  link,
  defaultOptions: {
    fetchPolicy: 'no-cache'
  }
})

If you want to do it for a specific query then set it in the query options:

apollo: {
  someQuery: {
    query: SOME_QUERY,
    fetchPolicy: 'no-cache'
  }
}

All 10 comments

set your fetchPolicy to 'no-cache' then.

@paulpdaniels
export class GraphQLModule { constructor(apollo: Apollo, httpLink: HttpLink) { apollo.create({ link: httpLink.create({uri:${AppConstant.BASE_URL}graphql}), cache: new InMemoryCache({ dataIdFromObject: o => o['_id'] }) }); } }

I have this code , can you please help me to disable it.
Thanks

Disable it entirely? Or for a specific query?

@paulpdaniels Well I want to disable it completely but could you please suggest both the ways. Thanks in advance

@macin40

To do it entirely just set the defaultOptions when setting up the client:

new ApolloClient({
  cache,
  link,
  defaultOptions: {
    fetchPolicy: 'no-cache'
  }
})

If you want to do it for a specific query then set it in the query options:

apollo: {
  someQuery: {
    query: SOME_QUERY,
    fetchPolicy: 'no-cache'
  }
}
new ApolloClient({
  cache,
  link,
  defaultOptions: {
    fetchPolicy: 'no-cache'
  }
})

fetchPolicy was not directly available in defaultOptions, I found it on query key.

apollo.create({
      link: httpLink.create({uri: `${AppConstant.BASE_URL}graphql`}),
      cache: new InMemoryCache({
        dataIdFromObject: o => o['_id']
      }),
      defaultOptions: {
        query: {
          fetchPolicy: 'network-only'
        }
      }
    });

@paulpdaniels Thanks for the help!!

yes, i found it in 3.0.0-beta.26
fetchPolicy was not directly available in global defaultOptions, but it can run in a specific query.

In case someone stumbles across this and is using apollo-boost (which doesn't allow setting this directly), the following worked for me:

import ApolloClient from 'apollo-boost';

const apolloClient = new ApolloClient({
    uri: "/api/graphql"    
});

apolloClient.defaultOptions.query = {
    fetchPolicy: "no-cache"
};

The reason that this works is because apollo-boost's ApolloClient extends the main ApolloClient class, and just passes a subset of args to the constructor. So this just monkey-patches the value directly.

@macin40

To do it entirely just set the defaultOptions when setting up the client:

new ApolloClient({
  cache,
  link,
  defaultOptions: {
    fetchPolicy: 'no-cache'
  }
})

If you want to do it for a specific query then set it in the query options:

apollo: {
  someQuery: {
    query: SOME_QUERY,
    fetchPolicy: 'no-cache'
  }
}

if using methods use

created () {
  console.log('em here')
  this.search()
},
methods: {
  this.search() {
      this.$apollo.query({
        query: query,
        fetchPolicy: 'no-cache',
        variables: { ... }
      })
    }
}

@sirb0rab0g1 You sir, are awesome. It's 2am and I was so close to pulling by hair out. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Acidic9 picture Acidic9  路  15Comments

dietergoetelen picture dietergoetelen  路  14Comments

lorensr picture lorensr  路  19Comments

ashishwadekar picture ashishwadekar  路  24Comments

DougDavenport picture DougDavenport  路  115Comments