Search code examples
graphql-jsreact-apolloapollo-client

How to set fetchPolicy globally on apollo-client queries?


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.


Solution

  • 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