Skip to content

Commit 4747661

Browse files
committed
feat: add cookie updater
1 parent d50de54 commit 4747661

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/graphql/clients/clientSideMakeClient.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PUBLIC_GATEWAY_URL } from '@/config/app';
2-
import { ApolloClient, HttpLink, from } from '@apollo/client';
2+
import { ApolloClient, ApolloLink, HttpLink, from } from '@apollo/client';
33
import { setContext } from '@apollo/client/link/context';
44
import {
55
NextSSRApolloClient,
@@ -22,7 +22,7 @@ const client = new ApolloClient({
2222
link: from([httpLink]),
2323
});
2424

25-
const createSessionLink = () => {
25+
const sessionCreatorLink = () => {
2626
return setContext(async ({ context: { headers: currentHeaders } = {} }) => {
2727
const headers = { ...currentHeaders };
2828

@@ -48,13 +48,31 @@ const createSessionLink = () => {
4848
});
4949
};
5050

51+
const sessionUpdaterLink = new ApolloLink((operation, forward) =>
52+
forward(operation).map((response) => {
53+
const context = operation.getContext();
54+
const {
55+
response: { headers },
56+
} = context;
57+
58+
const session = headers.get('woocommerce-session');
59+
60+
if (session && !session.includes('Session')) {
61+
Cookies.set(WOO_SESSION_KEY, session, { expires: 7 });
62+
}
63+
64+
return response;
65+
}),
66+
);
67+
5168
export const makeClient = () => {
5269
return new NextSSRApolloClient({
5370
ssrMode: true,
5471
connectToDevTools: true,
5572
cache: new NextSSRInMemoryCache(),
5673
link: from([
57-
createSessionLink(),
74+
sessionCreatorLink(),
75+
sessionUpdaterLink,
5876
createErrorLink(),
5977
new SSRMultipartLink({ stripDefer: true }),
6078
httpLink,

0 commit comments

Comments
 (0)