Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/attribute-metadata-cache-per-store.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 31 additions & 0 deletions packages/magento-graphql/mesh/customAttributeMetadataV2.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>) {
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<string, unknown>()
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')
})
3 changes: 2 additions & 1 deletion packages/magento-graphql/mesh/customAttributeMetadataV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export async function customAttributeMetadataV2(
input: CustomAttributeInput,
context: MeshContext,
): Promise<CustomAttributeMetadataInterface | null> {
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

Expand Down
Loading