2021:
If you're using react, the useQuery()
hook is already following behaviour of watchQuery
instead of query
. So you are allowed to do this:
const { loading, error, data, refetch } = useQuery(query, {
fetchPolicy: "cache-and-network"
});
Or set it as defaultOptions when creating the apollo client:
import {
ApolloClient,
InMemoryCache,
HttpLink,
DefaultOptions
} from "@apollo/client";
const defaultOptions: DefaultOptions = {
watchQuery: {
fetchPolicy: "cache-and-network",
errorPolicy: "ignore",
notifyOnNetworkStatusChange: true
}
};
export const platformClient = new ApolloClient({
link: new HttpLink({
uri: "https://xxxxx",
credentials: "same-origin"
}),
cache: new InMemoryCache(),
defaultOptions
});
Some explanation on why cache-and-network
is not allowed for query
intentionally.
The reason is explained back in 2019 here:
https://github.com/apollographql/apollo-client/issues/3130#issuecomment-478409066
Basically, the behaviour/logic of query()
is not supporting this fetching strategy. So, they block it at typing level.