Skip to content

Commit

Permalink
fix: 🐛 Ajoute un système de retry lors d'une erreur 'Network' (#1476)
Browse files Browse the repository at this point in the history
Closes: #1470
  • Loading branch information
benguedj authored Oct 21, 2022
1 parent 9900d6a commit 7ab77fa
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion front/src/services/graphQL/graphQLClient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,40 @@ import type {
ApolloClientOptions,
NormalizedCacheObject,
} from "@apollo/client";
import { ApolloClient, InMemoryCache } from "@apollo/client";
import {
ApolloClient,
ApolloLink,
HttpLink,
InMemoryCache,
} from "@apollo/client";
import { RetryLink } from "@apollo/client/link/retry";

const cache = new InMemoryCache();
const headers = { "content-type": "application/json" };
const uri = `${process.env.API_URL}/graphql`;

// Apollo RetryLink : https://www.apollographql.com/docs/react/api/link/apollo-link-retry
const MAX_ATTEMPTS = 10;
const INITIAL_DELAY = 300;
const retryLink = new RetryLink({
attempts: {
max: MAX_ATTEMPTS,
retryIf: (error) => !!error,
},
delay: {
initial: INITIAL_DELAY,
jitter: true,
max: Infinity,
},
});

const getGraphQLClientOptions = (
withNoCacheUri: boolean
): ApolloClientOptions<NormalizedCacheObject> => {
return {
cache,
headers,
link: ApolloLink.from([retryLink, new HttpLink({ uri })]),
uri: withNoCacheUri ? `${uri}?nocache` : uri,
};
};
Expand Down

0 comments on commit 7ab77fa

Please sign in to comment.