I'm setting up Apollo Client like this.
const defaultOptions = {
watchQuery: {
fetchPolicy: 'cache-and-network',
errorPolicy: 'ignore',
},
query: {
fetchPolicy: 'cache-and-network',
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all',
},
};
return new ApolloClient({
link: ApolloLink.from([authLink, errorLink, webSocketOrHttpLink]),
defaultOptions, // Typescript don't like this.
queryDeduplication: true,
});
Typescript gives this error:
Type '{ watchQuery: { fetchPolicy: string; errorPolicy: string; }; query: { fetchPolicy: string; errorPolicy: string; }; mutate: { errorPolicy: string; }; }' is not assignable to type 'DefaultOptions'.ts(2322)
ApolloClient.d.ts(23, 5): The expected type comes from property 'defaultOptions' which is declared here on type 'ApolloClientOptions<NormalizedCacheObject>'
Per the docs, this is how it's supposed to be done.
How can I structure defaultOptions with proper types?
if you check code of library then , it seems here is issue
//defination of default options
export interface DefaultOptions {
watchQuery?: Partial<WatchQueryOptions>;
query?: Partial<QueryOptions>;
mutate?: Partial<MutationOptions>;
}
//defination of QueryOptions
export interface QueryOptions<TVariables = OperationVariables>
extends QueryBaseOptions<TVariables> {
/**
* Specifies the {@link FetchPolicy} to be used for this query
*/
fetchPolicy?: FetchPolicy;
}
//valid value for FetchPolicy type
export type FetchPolicy =
| 'cache-first'
| 'network-only'
| 'cache-only'
| 'no-cache'
| 'standby';
export type WatchQueryFetchPolicy = FetchPolicy | 'cache-and-network';
so here for query options you should pass any valid value for FetchPolicy and 'cache-and-network' is not valid value.
check documentation here : https://github.com/apollographql/apollo-client/blob/main/src/core/watchQueryOptions.ts
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