Another attempt at fixing broken retry for apollo error handling.

This commit is contained in:
Patrick Fic
2020-01-30 18:59:51 -08:00
parent 0225e34f7b
commit f1ac052a1b
3 changed files with 18 additions and 20 deletions

View File

@@ -5,13 +5,15 @@ import { auth } from "../firebase/firebase.utils";
const errorLink = onError(
({ graphQLErrors, networkError, operation, forward }) => {
// console.log("graphQLErrors", graphQLErrors);
// console.log("networkError", networkError);
// console.log("operation", operation);
// console.log("forward", forward);
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
)
);
if (networkError) console.log(`[Network error]: ${networkError}`);
let expired = false;
if (graphQLErrors) {
if (graphQLErrors[0].message.includes("JWTExpired")) {
expired = true;
@@ -33,12 +35,14 @@ const errorLink = onError(
if (token) {
console.log("Got the new token.", token);
window.localStorage.setItem("token", token);
operation.setContext(({ headers = {} }) => ({
const oldHeaders = operation.getContext().headers;
operation.setContext({
headers: {
...headers,
...oldHeaders,
authorization: token ? `Bearer ${token}` : ""
}
}));
});
return forward(operation);