Skip to content

Commit 6cdc663

Browse files
authored
Merge pull request #97 from Team-INSERT/hotfix/graphql/access-token
게시판 액세스 토큰 expired 이슈 픽스
2 parents b150e52 + 998d25f commit 6cdc663

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

src/provider/apolloClientProvider.helper.tsx

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1-
import { getToken } from "@/helpers";
2-
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
1+
import {
2+
ApolloClient,
3+
ApolloProvider,
4+
HttpLink,
5+
InMemoryCache,
6+
} from "@apollo/client";
7+
import { onError } from "@apollo/client/link/error";
8+
import { setContext } from "@apollo/client/link/context";
9+
import { RetryLink } from "@apollo/client/link/retry";
310
import { PropsWithChildren } from "react";
11+
import Storage from "@/apis/storage";
12+
import { TOKEN } from "@/constants";
13+
import { refresh } from "@/apis/token";
414

5-
const client = new ApolloClient({
15+
const authLink = setContext((_, { headers }) => {
16+
const token = Storage.getItem(TOKEN.ACCESS);
17+
return {
18+
headers: {
19+
...headers,
20+
authorization: token ? `Bearer ${token}` : "",
21+
},
22+
};
23+
});
24+
25+
const errorLink = onError(({ operation, forward }) => {
26+
refresh().then(() => forward(operation));
27+
});
28+
29+
const httpLink = new HttpLink({
630
uri: `${process.env.NEXT_PUBLIC_BASE_URL}api/graphql`,
31+
});
32+
33+
const retryLink = new RetryLink({
34+
delay: {
35+
initial: 300,
36+
max: Infinity,
37+
jitter: true,
38+
},
39+
attempts: {
40+
max: 2,
41+
retryIf: (error) => !!error,
42+
},
43+
});
44+
45+
const client = new ApolloClient({
46+
link: retryLink.concat(errorLink).concat(authLink).concat(httpLink),
747
headers: {
8-
Authorization: getToken(),
48+
Authorization: Storage.getItem(TOKEN.ACCESS) ?? "",
949
},
1050
cache: new InMemoryCache(),
1151
connectToDevTools: true,

0 commit comments

Comments
 (0)