From d4489f691b312891013767f7e39d92a9b0863387 Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Tue, 12 Dec 2023 11:40:33 +0100
Subject: [PATCH] fix(front-end): fix issues with expired accessToken
 refreshment queue

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
---
 src/apollo/error-link.ts | 45 +++++++++++++++++++++++-----------------
 src/apollo/link.ts       |  4 ++--
 2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/src/apollo/error-link.ts b/src/apollo/error-link.ts
index faaf7f00a..6d1955a9e 100644
--- a/src/apollo/error-link.ts
+++ b/src/apollo/error-link.ts
@@ -9,7 +9,12 @@ let isRefreshing = false;
 let pendingRequests: any[] = [];
 
 const resolvePendingRequests = () => {
-  pendingRequests.map((callback) => callback());
+  console.debug("resolving pending requests");
+  pendingRequests.map((callback) => {
+    console.debug("calling callback", callback);
+    return callback();
+  });
+  console.debug("emptying pending requests after resolving them all");
   pendingRequests = [];
 };
 
@@ -21,7 +26,23 @@ const isAuthError = (graphQLError: GraphQLError | undefined) => {
 
 const errorLink = onError(
   ({ graphQLErrors, networkError, forward, operation }) => {
-    console.debug("We have an apollo error", [graphQLErrors, networkError]);
+    if (graphQLErrors) {
+      graphQLErrors.map(
+        (graphQLError: GraphQLError & { status_code?: number }) => {
+          if (graphQLError?.status_code !== 401) {
+            console.debug(
+              `[GraphQL error]: Message: ${graphQLError.message}, Location: ${graphQLError.locations}, Path: ${graphQLError.path}`
+            );
+          }
+        }
+      );
+    }
+
+    if (networkError) {
+      console.error(`[Network error]: ${networkError}`);
+      console.debug(JSON.stringify(networkError));
+    }
+
     if (
       graphQLErrors?.some((graphQLError) => isAuthError(graphQLError)) ||
       // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -67,6 +88,9 @@ const errorLink = onError(
             })
         ).filter((value) => Boolean(value));
       } else {
+        console.debug(
+          "Skipping refreshing as isRefreshing is already to true, adding requests to pending"
+        );
         forwardOperation = fromPromise(
           new Promise((resolve) => {
             // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -78,23 +102,6 @@ const errorLink = onError(
 
       return forwardOperation.flatMap(() => forward(operation));
     }
-
-    if (graphQLErrors) {
-      graphQLErrors.map(
-        (graphQLError: GraphQLError & { status_code?: number }) => {
-          if (graphQLError?.status_code !== 401) {
-            console.debug(
-              `[GraphQL error]: Message: ${graphQLError.message}, Location: ${graphQLError.locations}, Path: ${graphQLError.path}`
-            );
-          }
-        }
-      );
-    }
-
-    if (networkError) {
-      console.error(`[Network error]: ${networkError}`);
-      console.debug(JSON.stringify(networkError));
-    }
   }
 );
 
diff --git a/src/apollo/link.ts b/src/apollo/link.ts
index 5e63dd2d4..675d80653 100644
--- a/src/apollo/link.ts
+++ b/src/apollo/link.ts
@@ -34,7 +34,7 @@ if (!import.meta.env.VITE_HISTOIRE_ENV) {
 
 const retryLink = new RetryLink();
 
-export const fullLink = authMiddleware
-  .concat(retryLink)
+export const fullLink = retryLink
   .concat(errorLink)
+  .concat(authMiddleware)
   .concat(link ?? uploadLink);