【发布时间】:2020-10-30 00:03:51
【问题描述】:
我正在尝试使用 TypeScript 创建 ApolloClient,但有一些我无法解决的类型错误。谁能指出我正确的方向该怎么做?
以下是用于创建客户端的示例代码(可以使用 JavaScript):
import {
ApolloClient,
ApolloProvider,
InMemoryCache,
split
} from '@apollo/client';
import { setContext } from 'apollo-link-context';
import { createHttpLink } from 'apollo-link-http';
import { getMainDefinition } from '@apollo/client/utilities';
import { WebSocketLink } from '@apollo/link-ws';
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem('consequat-token');
return {
headers: {
...headers,
authorization: token ? `bearer ${token}` : null
}
};
});
const httpLink = createHttpLink({ uri: 'http://localhost:4000' });
const wsLink = new WebSocketLink({
uri: 'ws://localhost:4000/graphql',
options: { reconnect: true },
});
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
authLink.concat(httpLink)
);
const client = new ApolloClient({
cache: new InMemoryCache(),
link: splitLink
});
问题是authLink.concat(httpLink) 线路在抱怨:
Argument of type 'ApolloLink' is not assignable to parameter of type 'ApolloLink | RequestHandler | undefined'.
Type 'ApolloLink' is missing the following properties from type 'ApolloLink': onError, setOnError ts(2345)
我无法从 Apollo docs 或 Google 中找到任何答案。
【问题讨论】:
标签: reactjs typescript apollo-client