From 9b33eac3fc8cdd7ac8ba160e598dd878c956e7c3 Mon Sep 17 00:00:00 2001 From: Bram van der Holst Date: Thu, 19 Dec 2024 17:12:01 +0100 Subject: [PATCH 1/2] Fix Magento store code not getting set in context.headers.store --- .changeset/cold-hotels-yawn.md | 5 ++++ .../magento-customer/link/customerLink.ts | 4 --- .../plugins/magentoStoreGraphqlConfig.ts | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 .changeset/cold-hotels-yawn.md create mode 100644 packages/magento-store/plugins/magentoStoreGraphqlConfig.ts 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 + }), + ], + } +} From 3711e2ba7d0c6bcf93a3c19060463bab32930ec8 Mon Sep 17 00:00:00 2001 From: Paul Hachmang Date: Thu, 19 Dec 2024 17:25:34 +0100 Subject: [PATCH 2/2] Fix tests --- .../__tests__/interceptors/findPlugins.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packagesDev/next-config/__tests__/interceptors/findPlugins.ts b/packagesDev/next-config/__tests__/interceptors/findPlugins.ts index e115fe4d26..54c8afedec 100644 --- a/packagesDev/next-config/__tests__/interceptors/findPlugins.ts +++ b/packagesDev/next-config/__tests__/interceptors/findPlugins.ts @@ -1,6 +1,5 @@ import type { GraphCommerceConfig } from '../../src/generated/config' import { findPlugins } from '../../src/interceptors/findPlugins' - const projectRoot = `${process.cwd()}/examples/magento-graphcms` it('finds plugins', () => { const fakeconfig = { @@ -13,8 +12,7 @@ it('finds plugins', () => { const disabled = plugins.filter((p) => !p.enabled) const enabled = plugins.filter((p) => p.enabled) expect(errors).toMatchInlineSnapshot('[]') - expect(enabled).toMatchInlineSnapshot( - ` + expect(enabled).toMatchInlineSnapshot(` [ { "enabled": true, @@ -518,6 +516,14 @@ it('finds plugins', () => { "targetModule": "@graphcommerce/graphql", "type": "component", }, + { + "enabled": true, + "sourceExport": "graphqlConfig", + "sourceModule": "@graphcommerce/magento-store/plugins/magentoStoreGraphqlConfig", + "targetExport": "graphqlConfig", + "targetModule": "@graphcommerce/graphql", + "type": "function", + }, { "enabled": true, "sourceExport": "meshConfig", @@ -543,9 +549,9 @@ it('finds plugins', () => { "type": "function", }, ] - `, - ) - expect(disabled).toMatchInlineSnapshot(` + `) + expect(disabled).toMatchInlineSnapshot( + ` [ { "enabled": false, @@ -746,5 +752,6 @@ it('finds plugins', () => { "type": "component", }, ] - `) + `, + ) })