diff --git a/.changeset/attribute-metadata-cache-per-store.md b/.changeset/attribute-metadata-cache-per-store.md new file mode 100644 index 00000000000..627cf1b61ee --- /dev/null +++ b/.changeset/attribute-metadata-cache-per-store.md @@ -0,0 +1,5 @@ +--- +'@graphcommerce/magento-graphql': patch +--- + +The cache of `customAttributeMetadataV2` has one entry per store. The key had no store, so all stores got the labels of the store that requested the attribute first. diff --git a/packages/magento-graphql/mesh/customAttributeMetadataV2.test.ts b/packages/magento-graphql/mesh/customAttributeMetadataV2.test.ts new file mode 100644 index 00000000000..52b8b74907f --- /dev/null +++ b/packages/magento-graphql/mesh/customAttributeMetadataV2.test.ts @@ -0,0 +1,31 @@ +import type { MeshContext } from '@graphcommerce/graphql-mesh' +import { expect, it } from 'vitest' +import { customAttributeMetadataV2 } from './customAttributeMetadataV2' + +function createContext(store: string, cache: Map) { + return { + headers: { store }, + cache: { + get: async (key: string) => cache.get(key), + set: async (key: string, value: unknown) => { + cache.set(key, value) + }, + }, + m2: { + Query: { + customAttributeMetadataV2: async () => ({ code: 'color', label: `Color ${store}` }), + }, + }, + } as unknown as MeshContext +} + +it('caches the attribute metadata per store', async () => { + const cache = new Map() + const input = { entity_type: 'catalog_product', attribute_code: 'color' } as const + + const english = await customAttributeMetadataV2(input, createContext('en_US', cache)) + const dutch = await customAttributeMetadataV2(input, createContext('nl_NL', cache)) + + expect(english?.label).toBe('Color en_US') + expect(dutch?.label).toBe('Color nl_NL') +}) diff --git a/packages/magento-graphql/mesh/customAttributeMetadataV2.ts b/packages/magento-graphql/mesh/customAttributeMetadataV2.ts index b6d7f494178..25f43cefd80 100644 --- a/packages/magento-graphql/mesh/customAttributeMetadataV2.ts +++ b/packages/magento-graphql/mesh/customAttributeMetadataV2.ts @@ -6,7 +6,8 @@ export async function customAttributeMetadataV2( input: CustomAttributeInput, context: MeshContext, ): Promise { - const cacheKey = `customAttributeMetadata-${input.entity_type}-${input.attribute_code}` + const { headers } = context as MeshContext & { headers?: { store?: string } } + const cacheKey = `customAttributeMetadata-${headers?.store ?? 'default'}-${input.entity_type}-${input.attribute_code}` const cached = await context.cache.get(cacheKey) if (cached) return cached