【发布时间】:2021-01-13 13:53:51
【问题描述】:
我正在尝试在返回 Apollo 客户端的类中构建一个简单的函数。这是我的代码:
import appConfig from 'config/app-config';
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
import LocalStorageKeys from 'constants/local-storage-keys';
import { setContext } from '@apollo/client/link/context';
export class ApolloClientServiceImpl {
private cache: InMemoryCache;
constructor() {
this.cache = new InMemoryCache();
}
createApolloClient(idToken: string): unknown {
const httpLink = createHttpLink({
uri: appConfig.hasura.url,
});
const authLink = setContext((_, { headers }) => {
let bearerToken = localStorage.getItem(LocalStorageKeys.TOKEN);
if (idToken) {
bearerToken = idToken;
}
return {
headers: {
...headers,
authorization: bearerToken ? `Bearer ${bearerToken}` : '',
},
};
});
return new ApolloClient({
link: authLink.concat(httpLink),
cache: this.cache,
});
}
}
我的问题在于createApolloClient 函数的返回类型。如果我将其设置为 ApolloClient<InMemoryCache> 并在 return 语句中执行以下操作:
return new ApolloClient<InMemoryCache>({
link: authLink.concat(httpLink),
cache: this.cache,
});
然后我得到以下错误:
Type 'InMemoryCache' is not assignable to type 'ApolloCache<InMemoryCache>'.
Types of property 'restore' are incompatible.
Type '(data: NormalizedCacheObject) => InMemoryCache' is not assignable to type '(serializedState: InMemoryCache) => ApolloCache<InMemoryCache>'.
Types of parameters 'data' and 'serializedState' are incompatible.
Type 'InMemoryCache' is not assignable to type 'NormalizedCacheObject'.
Index signature is missing in type 'InMemoryCache'.ts(2322)
Apollo 客户端文档上关于这个主题的文档很少,因此我的问题是,这个函数的正确返回类型是什么?
编辑:如果我只留下来:
return new ApolloClient({
link: authLink.concat(httpLink),
cache: this.cache,
});
并将返回类型更改为ApolloClient<NormalizedCacheObject>,这样就可以修复所有错误。感谢 Alluan Hadad。
【问题讨论】:
-
new ApolloClient<InMemoryCache>不好。应该是new ApolloClient -
@AluanHaddad 原来这解决了这个问题。非常感谢!
-
没问题。指定显式类型参数,尤其是在传递值时,是一种反模式。正确的打字稿尽可能地利用类型推断
标签: typescript apollo react-apollo apollo-client