gpt4 book ai didi

typescript - 如何正确输入 Apollo Client defaultOptions?

转载 作者:行者123 更新时间:2023-12-04 13:18:21 26 4
gpt4 key购买 nike

我正在这样设置 Apollo Client。

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 给出了这个错误:

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>'

根据文档,this是应该怎么做的。

如何使用正确的类型构造 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';

因此,对于查询选项,您应该为 FetchPolicy 传递任何有效值,而 'cache-and-network' 不是有效值。

在此处查看文档: https://github.com/apollographql/apollo-client/blob/main/src/core/watchQueryOptions.ts

关于typescript - 如何正确输入 Apollo Client defaultOptions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57472440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com