@graphcommerce/magento-customer 9.0.0-canary.57 → 9.0.0-canary.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/Config.graphqls +7 -0
- package/hooks/CustomerToken.graphql +2 -0
- package/link/customerLink.ts +16 -3
- package/link/xMagentoCacheIdHeader.ts +35 -0
- package/package.json +14 -14
- package/plugins/XMagentoCacheIdGraphQLProvider.tsx +14 -0
- package/schema/CustomerToken.graphqls +1 -0
- package/typePolicies.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 9.0.0-canary.59
|
|
4
|
+
|
|
5
|
+
## 9.0.0-canary.58
|
|
6
|
+
|
|
7
|
+
### Minor Changes
|
|
8
|
+
|
|
9
|
+
- [#2330](https://github.com/graphcommerce-org/graphcommerce/pull/2330) [`8de9c13`](https://github.com/graphcommerce-org/graphcommerce/commit/8de9c13b977633104ab81ce04def1dd6d1d4411b) - Added support X-Magento-Cache-Id to allow Varnish to cache requests that are made in the browser while users are logged in. For example the products query can now be cached for logged in users. Functionality can be disabled by setting `customerXMagentoCacheIdDisable: true` in your configuration. ([@paales](https://github.com/paales))
|
|
10
|
+
|
|
3
11
|
## 9.0.0-canary.57
|
|
4
12
|
|
|
5
13
|
## 9.0.0-canary.56
|
package/Config.graphqls
CHANGED
|
@@ -14,6 +14,13 @@ extend input GraphCommerceConfig {
|
|
|
14
14
|
Enable customer account deletion through the account section
|
|
15
15
|
"""
|
|
16
16
|
customerDeleteEnabled: Boolean
|
|
17
|
+
|
|
18
|
+
"""
|
|
19
|
+
X-Magento-Cache-Id allows Varnish to cache requests that are made in the browser while users are logged in. For example the products query can now be cached for logged in users.
|
|
20
|
+
|
|
21
|
+
This can be disabled when Varnish is running out of available memory.
|
|
22
|
+
"""
|
|
23
|
+
customerXMagentoCacheIdDisable: Boolean
|
|
17
24
|
}
|
|
18
25
|
|
|
19
26
|
extend input GraphCommerceDebugConfig {
|
package/link/customerLink.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { globalApolloClient } from '@graphcommerce/graphql'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
ApolloCache,
|
|
4
|
+
ApolloLink,
|
|
5
|
+
fromPromise,
|
|
6
|
+
onError,
|
|
7
|
+
setContext,
|
|
8
|
+
} from '@graphcommerce/graphql/apollo'
|
|
3
9
|
import { ErrorCategory } from '@graphcommerce/magento-graphql'
|
|
4
10
|
import type { GraphQLError } from 'graphql'
|
|
5
11
|
import { NextRouter } from 'next/router'
|
|
@@ -8,6 +14,13 @@ import { CustomerTokenDocument } from '../hooks'
|
|
|
8
14
|
|
|
9
15
|
export type PushRouter = Pick<NextRouter, 'push' | 'events'>
|
|
10
16
|
|
|
17
|
+
declare module '@apollo/client' {
|
|
18
|
+
interface DefaultContext {
|
|
19
|
+
cache?: ApolloCache<unknown>
|
|
20
|
+
headers?: Record<string, string>
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
11
24
|
async function pushWithPromise(router: Pick<NextRouter, 'push' | 'events'>, url: string) {
|
|
12
25
|
try {
|
|
13
26
|
await router.push(url)
|
|
@@ -45,7 +58,7 @@ const addTokenHeader = setContext((_, context) => {
|
|
|
45
58
|
if (!context.headers) context.headers = {}
|
|
46
59
|
|
|
47
60
|
try {
|
|
48
|
-
const query = context.cache
|
|
61
|
+
const query = context.cache?.readQuery({ query: CustomerTokenDocument })
|
|
49
62
|
|
|
50
63
|
if (query?.customerToken?.token) {
|
|
51
64
|
context.headers.authorization = `Bearer ${query?.customerToken?.token}`
|
|
@@ -73,7 +86,7 @@ const customerErrorLink = (router: PushRouter) =>
|
|
|
73
86
|
/** If the error we're dealing with is not an authorization error, we're done. */
|
|
74
87
|
if (!authError) return undefined
|
|
75
88
|
|
|
76
|
-
if (!oldHeaders
|
|
89
|
+
if (!oldHeaders?.authorization) {
|
|
77
90
|
// console.error(
|
|
78
91
|
// 'No authorization header found in request, but an authorization error was returned, this is a bug. This is the operation:',
|
|
79
92
|
// operation,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ApolloLink, DefaultContext } from '@graphcommerce/graphql'
|
|
2
|
+
import { CustomerTokenDocument } from '../hooks'
|
|
3
|
+
|
|
4
|
+
export const xMagentoCacheIdHeader = new ApolloLink((operation, forward) => {
|
|
5
|
+
operation.setContext((context: DefaultContext) => {
|
|
6
|
+
const xMagentoCacheId = context.cache?.readQuery({ query: CustomerTokenDocument })
|
|
7
|
+
?.customerToken?.xMagentoCacheId
|
|
8
|
+
if (!xMagentoCacheId) return context
|
|
9
|
+
|
|
10
|
+
return { ...context, headers: { ...context.headers, 'x-magento-cache-id': xMagentoCacheId } }
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
return forward(operation).map((data) => {
|
|
14
|
+
const cache = operation.getContext().cache
|
|
15
|
+
if (!cache) return data
|
|
16
|
+
|
|
17
|
+
const xMagentoCacheId = (data.extensions as { forwardedHeaders: Record<string, string> })
|
|
18
|
+
.forwardedHeaders['x-magento-cache-id']
|
|
19
|
+
if (!xMagentoCacheId) return data
|
|
20
|
+
|
|
21
|
+
const tokenResult = cache.readQuery({ query: CustomerTokenDocument })
|
|
22
|
+
|
|
23
|
+
if (
|
|
24
|
+
!tokenResult?.customerToken ||
|
|
25
|
+
tokenResult.customerToken?.xMagentoCacheId === xMagentoCacheId
|
|
26
|
+
)
|
|
27
|
+
return data
|
|
28
|
+
|
|
29
|
+
cache.writeQuery({
|
|
30
|
+
query: CustomerTokenDocument,
|
|
31
|
+
data: { customerToken: { ...tokenResult.customerToken, xMagentoCacheId } },
|
|
32
|
+
})
|
|
33
|
+
return data
|
|
34
|
+
})
|
|
35
|
+
})
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graphcommerce/magento-customer",
|
|
3
3
|
"homepage": "https://www.graphcommerce.org/",
|
|
4
4
|
"repository": "github:graphcommerce-org/graphcommerce",
|
|
5
|
-
"version": "9.0.0-canary.
|
|
5
|
+
"version": "9.0.0-canary.59",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
8
8
|
"eslintConfig": {
|
|
@@ -12,19 +12,19 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"peerDependencies": {
|
|
15
|
-
"@graphcommerce/ecommerce-ui": "^9.0.0-canary.
|
|
16
|
-
"@graphcommerce/eslint-config-pwa": "^9.0.0-canary.
|
|
17
|
-
"@graphcommerce/framer-next-pages": "^9.0.0-canary.
|
|
18
|
-
"@graphcommerce/framer-utils": "^9.0.0-canary.
|
|
19
|
-
"@graphcommerce/graphql": "^9.0.0-canary.
|
|
20
|
-
"@graphcommerce/graphql-mesh": "^9.0.0-canary.
|
|
21
|
-
"@graphcommerce/image": "^9.0.0-canary.
|
|
22
|
-
"@graphcommerce/magento-graphql": "^9.0.0-canary.
|
|
23
|
-
"@graphcommerce/magento-store": "^9.0.0-canary.
|
|
24
|
-
"@graphcommerce/next-ui": "^9.0.0-canary.
|
|
25
|
-
"@graphcommerce/prettier-config-pwa": "^9.0.0-canary.
|
|
26
|
-
"@graphcommerce/react-hook-form": "^9.0.0-canary.
|
|
27
|
-
"@graphcommerce/typescript-config-pwa": "^9.0.0-canary.
|
|
15
|
+
"@graphcommerce/ecommerce-ui": "^9.0.0-canary.59",
|
|
16
|
+
"@graphcommerce/eslint-config-pwa": "^9.0.0-canary.59",
|
|
17
|
+
"@graphcommerce/framer-next-pages": "^9.0.0-canary.59",
|
|
18
|
+
"@graphcommerce/framer-utils": "^9.0.0-canary.59",
|
|
19
|
+
"@graphcommerce/graphql": "^9.0.0-canary.59",
|
|
20
|
+
"@graphcommerce/graphql-mesh": "^9.0.0-canary.59",
|
|
21
|
+
"@graphcommerce/image": "^9.0.0-canary.59",
|
|
22
|
+
"@graphcommerce/magento-graphql": "^9.0.0-canary.59",
|
|
23
|
+
"@graphcommerce/magento-store": "^9.0.0-canary.59",
|
|
24
|
+
"@graphcommerce/next-ui": "^9.0.0-canary.59",
|
|
25
|
+
"@graphcommerce/prettier-config-pwa": "^9.0.0-canary.59",
|
|
26
|
+
"@graphcommerce/react-hook-form": "^9.0.0-canary.59",
|
|
27
|
+
"@graphcommerce/typescript-config-pwa": "^9.0.0-canary.59",
|
|
28
28
|
"@lingui/core": "^4.2.1",
|
|
29
29
|
"@lingui/macro": "^4.2.1",
|
|
30
30
|
"@lingui/react": "^4.2.1",
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PluginConfig, PluginProps } from '@graphcommerce/next-config'
|
|
2
|
+
import { xMagentoCacheIdHeader } from '../link/xMagentoCacheIdHeader'
|
|
3
|
+
import { GraphQLProviderProps } from '@graphcommerce/graphql'
|
|
4
|
+
|
|
5
|
+
export const config: PluginConfig = {
|
|
6
|
+
type: 'component',
|
|
7
|
+
module: '@graphcommerce/graphql',
|
|
8
|
+
ifConfig: ['customerXMagentoCacheIdDisable', false],
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function GraphQLProvider(props: PluginProps<GraphQLProviderProps>) {
|
|
12
|
+
const { Prev, links = [], ...rest } = props
|
|
13
|
+
return <Prev {...rest} links={[...links, xMagentoCacheIdHeader]} />
|
|
14
|
+
}
|
package/typePolicies.ts
CHANGED