apollo icon indicating copy to clipboard operation
apollo copied to clipboard

Disable cache??

Open macin40 opened this issue 8 years ago • 11 comments

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

macin40 avatar Aug 11 '18 08:08 macin40

set your fetchPolicy to 'no-cache' then.

paulpdaniels avatar Aug 11 '18 15:08 paulpdaniels

@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

macin40 avatar Aug 13 '18 07:08 macin40

Disable it entirely? Or for a specific query?

paulpdaniels avatar Aug 15 '18 03:08 paulpdaniels

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

macin40 avatar Aug 16 '18 06:08 macin40

@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'
  }
}

paulpdaniels avatar Aug 16 '18 06:08 paulpdaniels

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!!

macin40 avatar Aug 16 '18 06:08 macin40

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.

muyu66 avatar Nov 17 '18 09:11 muyu66

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.

erindru avatar May 21 '19 19:05 erindru

@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 avatar Jun 26 '19 09:06 sirb0rab0g1

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

rahulnpadalkar avatar Jul 18 '20 20:07 rahulnpadalkar

Can you give an working example in case of Vue 3 integration. It is not working for following setup code:

new ApolloClient({ link: errorLink.concat(httpLink), cache, defaultOptions: { watchQuery: { fetchPolicy: 'no-cache' } } })

I am using "@apollo/client": "^3.7.14" with Vue 3.2.47

vikashpisces avatar May 22 '23 14:05 vikashpisces