Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/apollo/ApolloClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
import { ApolloClient, InMemoryCache, createHttpLink, ApolloLink } from '@apollo/client';

const defaultOptions = {
watchQuery: {
Expand All @@ -19,9 +19,47 @@ const link = createHttpLink({
uri: `${process.env.NEXT_PUBLIC_WOO_SITE_URL}/graphql`
});

export const middleware = new ApolloLink((operation, forward) => {

let session;
console.log("Middleware")
if (typeof window !== "undefined") {
console.warn("Middleware If")
session = localStorage.getItem('woo-session');
}
if (session) {
operation.setContext(({ headers = {} }) => ({
headers: {
'woocommerce-session': `Session ${session}`,
}
}));
}

return forward(operation);
});

export const afterware = new ApolloLink((operation, forward) => {
return forward(operation).map((response) => {
const context = operation.getContext();
const { response: { headers } } = context;
const session = headers.get('woocommerce-session');
console.log("Afterware")
if (session) {
if (typeof window !== "undefined") {
if ( localStorage.getItem('woo-session') !== session ) {
console.log("Afterware If")
localStorage.setItem('woo-session', headers.get('woocommerce-session'));
}
}
}

return response;
});
});

// Apollo GraphQL client.
const client = new ApolloClient({
link,
link: middleware.concat(afterware.concat(link)),
cache,
defaultOptions
});
Expand Down