如何解决来自授权钩子的无效响应
我正在尝试使用 websockets 实现 ApolloClient。我的问题是令牌没有在标头中使用,所以我在 authMiddleware 中使用了 apolloLink。但是现在不行了
import { ApolloClient,InMemoryCache,ApolloLink as x } from '@apollo/client';
import { split,ApolloLink } from 'apollo-link';
import { getMainDefinition } from 'apollo-utilities';
import { HttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
const cache = () =>
new InMemoryCache({
...
});
interface Definintion {
kind: string;
operation?: string;
}
const httpLink = new HttpLink({
uri: process.env.REACT_APP_URL,});
const authMiddleware = new ApolloLink((operation,forward) => {
operation.setContext({
headers: {
authorization: localStorage.getItem('token') || null,},});
// Add onto payload for WebSocket authentication
(operation as any & { authToken: string | undefined }).authToken = localStorage.getItem('token');
return forward(operation);
});
const socket = new SubscriptionClient(process.env.REACT_APP_URL2 as string,{
reconnect: true,});
const webSocketLink: WebSocketLink = new WebSocketLink(socket);
const link = split(
({ query }) => {
const { kind,operation }: Definintion = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},webSocketLink,httpLink,);
const client = new ApolloClient({
link: authMiddleware.concat(link) as any,cache: cache(),});
我无法进行任何查询,因为我收到错误
Error: Invalid response from authorization hook
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。