I am using Apollo Server and Apollo Client and am having trouble handling unauthenticated responses. On the server, I throw AuthenticationError when a request is made and the user is not authenticated. On the client side, executing a query/mutation when unauthenticated sends an error to the catch block where I can see a graphQLErrors with "code":"UNAUTHENTICATED", exactly as expected.
What I would like to do is handle all of these unauthenticated responses in one central location so that if any request, ever, returns as unauthenticated I can redirect the user to the login page. The documentation for apollo-link-error shows how to use onError as an error handler with an example about unauthenticated responses.
The code I wrote in onError says if there are graphQLErrors that have the code "UNAUTHENTICATED", set "response.errors" to "undefined" and redirect to the login page. This logic seemingly works as expected as any unauthenticated requests are redirected to the login page.
However, the addition of response.errors = undefined as indicated in the "Ignoring errors" section of the apollo-link-error documents results in all unauthenticated requests throwing a Error writing result to store for query error. If I remove the response.errors = undefined logic, then the graphQLErrors "code":"UNAUTHENTICATED" error ends up in the catch block of every unauthenticated query/mutation which means I would need logic in every catch block to ignore that error but show others.
Am I doing something wrong here? How can I use onErrors to trap all "unauthenticated" errors and prevent them from appearing in the query/mutation catch blocks without the error listed above?