Gol*_*Jer 3 typescript apollo react-apollo apollo-client
我正在像这样设置 Apollo 客户端。
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,
});
Run Code Online (Sandbox Code Playgroud)
打字稿给出了这个错误:
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>'
Run Code Online (Sandbox Code Playgroud)
根据文档,这就是应该如何完成的。
如何defaultOptions使用正确的类型来构建?
如果你检查库的代码,那么这似乎是问题
//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';
Run Code Online (Sandbox Code Playgroud)
因此,对于查询选项,您应该在此处传递任何有效值,FetchPolicy并且'cache-and-network'不是有效值。
在此处查看文档: https: //github.com/apollographql/apollo-client/blob/main/src/core/watchQueryOptions.ts
| 归档时间: |
|
| 查看次数: |
6210 次 |
| 最近记录: |