Skip to content

Commit

Permalink
Fix Magento store code not getting set in context.headers.store
Browse files Browse the repository at this point in the history
  • Loading branch information
bramvanderholst committed Dec 19, 2024
1 parent d708e06 commit 9b33eac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-hotels-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/magento-store': patch
---

Fix Magento store code not getting set in context.headers.store
4 changes: 0 additions & 4 deletions packages/magento-customer/link/customerLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions packages/magento-store/plugins/magentoStoreGraphqlConfig.ts
Original file line number Diff line number Diff line change
@@ -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<typeof graphqlConfigType> = (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
}),
],
}
}

0 comments on commit 9b33eac

Please sign in to comment.