How to set fetchPolicy globally on apollo-client queries?

5.6k Views Asked by At

I have a few mutations that should trigger some refetchQueries, but I need those queries to have a fetchPolicy other than the default.

Is there a way to set fetchPolicy globally instead of per query? So to avoid setting fetchPolicy on each query.

1

There are 1 best solutions below

2
On BEST ANSWER

It is now possible!

const defaultOptions = { 
  watchQuery: {
    fetchPolicy: 'cache-and-network',
    errorPolicy: 'ignore',
  },
  query: {
    fetchPolicy: 'network-only',
    errorPolicy: 'all',
  },
  mutate: {
    errorPolicy: 'all'
  }
}

const client = new ApolloClient({
  link,
  cache,
  defaultOptions,
})

See documentation: Apollo Client