diff --git a/.changeset/cold-hotels-yawn.md b/.changeset/cold-hotels-yawn.md new file mode 100644 index 0000000000..4b576030ff --- /dev/null +++ b/.changeset/cold-hotels-yawn.md @@ -0,0 +1,5 @@ +--- +'@graphcommerce/magento-store': patch +--- + +Fix Magento store code not getting set in context.headers.store diff --git a/packages/magento-customer/link/customerLink.ts b/packages/magento-customer/link/customerLink.ts index c1889e6d29..b4dc5b483a 100644 --- a/packages/magento-customer/link/customerLink.ts +++ b/packages/magento-customer/link/customerLink.ts @@ -57,10 +57,6 @@ const addTokenHeader = setContext((_, context) => { if (query?.customerToken?.token) { context.headers.authorization = `Bearer ${query?.customerToken?.token}` - - // todo implement x-magento-cache-id - // But to be able to do this, we need to forward the header from the original request to the client. - // GraphQL Mesh currently does not support forwarding headers. return context } return context diff --git a/packages/magento-store/plugins/magentoStoreGraphqlConfig.ts b/packages/magento-store/plugins/magentoStoreGraphqlConfig.ts new file mode 100644 index 0000000000..271b7cc2bf --- /dev/null +++ b/packages/magento-store/plugins/magentoStoreGraphqlConfig.ts @@ -0,0 +1,29 @@ +import { setContext, type graphqlConfig as graphqlConfigType } from '@graphcommerce/graphql' +import type { FunctionPlugin, PluginConfig } from '@graphcommerce/next-config' + +export const config: PluginConfig = { + type: 'function', + module: '@graphcommerce/graphql', +} + +export const graphqlConfig: FunctionPlugin = (prev, conf) => { + const results = prev(conf) + + return { + ...results, + links: [ + ...results.links, + setContext((_, context) => { + if (!context.headers) context.headers = {} + context.headers.store = conf.storefront.magentoStoreCode + + if (conf.preview) { + // To disable caching from the backend, we provide a bogus cache ID. + context.headers['x-magento-cache-id'] = + `random-cache-id${Math.random().toString(36).slice(2)}` + } + return context + }), + ], + } +}