I am building an authentication system based on JWT
.
JWT
has expired time. When JWT
expires, I catch JWT
expired error using apollo-link-error
. I want to invoke apolloClient.resetStore()
method to reset the cache.
Here is my code:
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])
});
I am not sure apollo-link-error
is the right place to handle the error of expired JWT
.
You should simply be able to call the client directly:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
client.resetStore();
}
});
const client = new ApolloClient({
cache: new InMemoryCache({ fragmentMatcher }),
link: ApolloLink.from([
errorLink,
// otherLink,
// otherLink,
]),
});
You can even call it from a nested configuration function:
const client = new ApolloClient({
cache: new InMemoryCache({ fragmentMatcher }),
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
client.resetStore();
}
}),
// otherLink,
// otherLink,
]),
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With