Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Magento store code not getting set in context.headers.store #2458

Merged
merged 2 commits into from
Dec 19, 2024
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/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
}),
],
}
}
21 changes: 14 additions & 7 deletions packagesDev/next-config/__tests__/interceptors/findPlugins.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -543,9 +549,9 @@ it('finds plugins', () => {
"type": "function",
},
]
`,
)
expect(disabled).toMatchInlineSnapshot(`
`)
expect(disabled).toMatchInlineSnapshot(
`
[
{
"enabled": false,
Expand Down Expand Up @@ -746,5 +752,6 @@ it('finds plugins', () => {
"type": "component",
},
]
`)
`,
)
})
Loading