Defines whether a query gets its data from the cache
or from the API
.
Decision of getting the most recent data from the server or getting a faster response from the cache.
By default, the error policy treats any GraphQL Errors as network errors and ends the request chain. It doesn't save any data in the cache.
You can set this policy manually as below:
const MY_QUERY = gql` query WillFail { badField goodField } `; function ShowingSomeErrors() { const { loading, error, data } = useQuery(MY_QUERY, { errorPolicy: 'all' }); if (loading) return <span>loading...</span> return ( <div> <h2>Good: {data.goodField}</h2> <pre>Bad: {error.graphQLErrors.map(({ message }, i) => ( <span key={i}>{message}</span> ))} </pre> </div> ); }
cache-first
Default fetch policy.
Favors quick response times for queries over getting the most up-to-date data.
Steps:
cache-and-network
Little more emphasis than cache-first
on keeping the cache up to date.
Could be a good fetch policy if you have data that is changing frequently and queries are returning out-of-date information.
Steps:
network-only
Prioritizes up-to-date data over response speed.
It never returns outdated information from the cache
.
Steps:
no-cache
no-cache
is similar to network-only
but skips the step to update the cache.
Steps:
cache-only
Opposite of no-cache
.
Avoids making any network request.
cache
, request will throw and error.Useful if you want to display consistent information to the user.