Files
manoon-headless/src/lib/saleor/client.ts
Unchained 26212dec1c
Some checks failed
Build and Deploy / build (push) Has been cancelled
fix: Apollo Client cache merge causing product duplication
The merge function was concatenating products on each query, causing
4 products to become 8, then 12, etc. Changed to replace incoming
data instead of merging.
2026-03-21 18:04:11 +02:00

46 lines
1.0 KiB
TypeScript

import { ApolloClient, InMemoryCache, createHttpLink } from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
const httpLink = createHttpLink({
uri: process.env.NEXT_PUBLIC_SALEOR_API_URL || "http://localhost:8000/graphql/",
});
const authLink = setContext((_, { headers }) => {
// Saleor doesn't require auth for public queries
// Add auth token here if needed for admin operations
return {
headers: {
...headers,
"Content-Type": "application/json",
},
};
});
export const saleorClient = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
products: {
keyArgs: ["channel", "filter"],
merge(_existing, incoming) {
return incoming;
},
},
},
},
},
}),
defaultOptions: {
watchQuery: {
fetchPolicy: "cache-first",
},
query: {
fetchPolicy: "cache-first",
},
},
});
export default saleorClient;