Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set fetchPolicy globally on apollo-client queries?

Tags:

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.

like image 954
Eduardo Polmann Avatar asked Sep 15 '17 19:09

Eduardo Polmann


People also ask

How do you query using Apollo Client?

Executing a query To run a query within a React component, call useQuery and pass it a GraphQL query string. When your component renders, useQuery returns an object from Apollo Client that contains loading , error , and data properties you can use to render your UI.

What is Uri in Apollo Client?

uri specifies the URL of our GraphQL server. cache is an instance of InMemoryCache , which Apollo Client uses to cache query results after fetching them.

What is the difference between useQuery and useLazyQuery?

This hook acts just like useQuery , with one key exception: when useLazyQuery is called, it does not immediately execute its associated query. Instead, it returns a function in its result tuple that you can call whenever you're ready to execute the query.

How do you update the Apollo Client cache after a mutation?

If a mutation updates a single existing entity, Apollo Client can automatically update that entity's value in its cache when the mutation returns. To do so, the mutation must return the id of the modified entity, along with the values of the fields that were modified.


1 Answers

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

like image 132
Cyril Gandon Avatar answered Sep 18 '22 03:09

Cyril Gandon