apollographql / react-apollo

:recycle: React integration for Apollo Client
https://www.apollographql.com/docs/react/
MIT License
6.85k stars 790 forks source link

TypeScript "network-and-cache" missing from useApolloClient's fetchPolicy options #3734

Open alienfacepalm opened 4 years ago

alienfacepalm commented 4 years ago

The network-and-cache fetchPolicy type seems to be missing from TypeScript options for useApolloClient which is not the case with the useQuery hook.

export declare type FetchPolicy = 'cache-first' | 'network-only' | 'cache-only' | 'no-cache' | 'standby';

Is this intended? Why doesn't it match the types for useQuery hook?

Screen Shot 2019-12-06 at 1 28 42 PM
dylanwulf commented 4 years ago

It's intentional: https://github.com/apollographql/apollo-client/blob/master/CHANGELOG.md#apollo-client-260-1

https://github.com/apollographql/apollo-client/issues/3130#issuecomment-478409066

basically: cache-and-network is not allowed on the one-shot query method because there's no way for a one-shot query to return more than one result. Therefore cache-and-network is only allowed for the watchQuery method (which is used by the useQuery hook). Try using the WatchQueryFetchPolicy type instead of the FetchPolicy type.