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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With