如何解决反应Graphql或Apollo网络错误apollo onError吗?
import {
ApolloClient,HttpLink,ApolloLink,InMemoryCache,from
} from '@apollo/client';
const httpLink = new HttpLink({
uri: 'http://local:3000/api/v1/graphql',credentials: 'include'
});
const authMiddleware = new ApolloLink((operation,forward) => {
// add the authorization to the headers
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,Authorization: `Bearer ${sessionStorage.getItem('token')}` || null
}
}));
return forward(operation);
});
const Client = new ApolloClient({
cache: new InMemoryCache(),link: from([authMiddleware,httpLink,resetToken])
});
export default Client;
createHttpLink.ts:121 POST http://local:3000/api/v1/graphql 401 (Unauthorized)
接收令牌15分钟后,令牌的有效时间结束,并产生401错误。 我想实现在令牌的有效时间结束后发生401错误时取回令牌的方法。当发生401错误时,是否可以识别事实组件中的401错误?或者,如果有人知道我要实现什么,请帮助我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。