Skip to content

Commit

Permalink
fix(QA): apollo retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubinquitous committed Oct 15, 2023
1 parent 8ad2460 commit 998d25f
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/provider/apolloClientProvider.helper.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
import { getToken } from "@/helpers";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
import {
ApolloClient,
ApolloProvider,
HttpLink,
InMemoryCache,
} from "@apollo/client";
import { onError } from "@apollo/client/link/error";
import { setContext } from "@apollo/client/link/context";
import { RetryLink } from "@apollo/client/link/retry";
import { PropsWithChildren } from "react";
import Storage from "@/apis/storage";
import { TOKEN } from "@/constants";
import { refresh } from "@/apis/token";

const client = new ApolloClient({
const authLink = setContext((_, { headers }) => {
const token = Storage.getItem(TOKEN.ACCESS);
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
},
};
});

const errorLink = onError(({ operation, forward }) => {
refresh().then(() => forward(operation));
});

const httpLink = new HttpLink({
uri: `${process.env.NEXT_PUBLIC_BASE_URL}api/graphql`,
});

const retryLink = new RetryLink({
delay: {
initial: 300,
max: Infinity,
jitter: true,
},
attempts: {
max: 2,
retryIf: (error) => !!error,
},
});

const client = new ApolloClient({
link: retryLink.concat(errorLink).concat(authLink).concat(httpLink),
headers: {
Authorization: getToken(),
Authorization: Storage.getItem(TOKEN.ACCESS) ?? "",
},
cache: new InMemoryCache(),
connectToDevTools: true,
Expand Down

0 comments on commit 998d25f

Please sign in to comment.