gpt4 book ai didi

reactjs - ApolloClient 类型错误 ApolloLink

转载 作者:行者123 更新时间:2023-12-04 00:17:33 24 4
gpt4 key购买 nike

我正在尝试使用 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 文档或 Google 中找到任何答案。

最佳答案

回复我自己的帖子:

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';
需要改为:
import {
ApolloClient,
ApolloProvider,
InMemoryCache,
HttpLink,
split
} from '@apollo/client';

import { setContext } from '@apollo/link-context';
import { getMainDefinition } from '@apollo/client/utilities';
import { WebSocketLink } from '@apollo/link-ws';
那些 @apollo/apollo-库不兼容。另外, createHttpLink替换为 HttpLink进口自 @apollo/client ,它的用法:
const httpLink = createHttpLink({ uri: 'http://localhost:4000' });
变成:
const httpLink = new HttpLink({ uri: 'http://localhost:4000' });

关于reactjs - ApolloClient 类型错误 ApolloLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62814083/

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