我正在构建一个基于JWT的身份验证系统。
JWT已超时。当JWT过期时,我使用apollo-link-error捕获JWT expired错误。我想调用apolloClient.resetStore()方法来重置缓存。
下面是我的代码:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(error => {
// console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
if (error.code === 1001) {
auth.signout();
// how can I get apollo client here?
//client.resetStore();
}
});
if (networkError) console.log(`[Network error]: ${networkError}`);
});
const client = new ApolloClient({
cache,
link: from([authMiddleware, errorLink, terminalLink])
});我不确定apollo-link-error是不是处理过期JWT错误的正确位置。
发布于 2018-09-13 07:30:01
您应该能够直接调用客户端:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
client.resetStore();
}
});
const client = new ApolloClient({
cache: new InMemoryCache({ fragmentMatcher }),
link: ApolloLink.from([
errorLink,
// otherLink,
// otherLink,
]),
});您甚至可以从嵌套的配置函数中调用它:
const client = new ApolloClient({
cache: new InMemoryCache({ fragmentMatcher }),
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
client.resetStore();
}
}),
// otherLink,
// otherLink,
]),
});发布于 2019-08-06 10:00:27
Mikael的回答对我很有效。只需在onError中使用client.resetStore();,就像Mikael的答案一样。
但是,也要确保将errorLink链接到客户端,如下所示:
const client = new ApolloClient({
cache,
link: authLink.concat(errorLink).concat(restLink).concat(httpLink),
resolvers
});https://stackoverflow.com/questions/51664533
复制相似问题