The scenario is:
- Web app is open but token has expired.
- User then does some action that makes an api request.
- GraphQL returns
Error: GraphQL error: unauthorized. - The app doesn't respond due to the error. Bad user experience.
onErrorfromapollo-link-errormethod catches the error and from this am able to callrefreshToken()(it requires an expired jwt).- New token is generated and stored for use.
- Next user action will go through as per normal.
This is currently my code:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
refreshToken();
}
});
const link = ApolloLink.from([errorLink, terminatingLink]);
How can we improve this flow so that the token is refreshed when the user makes a request without getting an error? Something like the request is 'put on hold' if it causes an error then once token is refreshed, then it will be processed. But I am not sure how to do this.
Or is there any other way we can improve on this flow?