@graphcommerce/magento-customer 9.0.0-canary.57 → 9.0.0-canary.58

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 9.0.0-canary.58
4
+
5
+ ### Minor Changes
6
+
7
+ - [#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))
8
+
3
9
  ## 9.0.0-canary.57
4
10
 
5
11
  ## 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 {
@@ -1,7 +1,9 @@
1
1
  query CustomerToken {
2
2
  customerToken @client {
3
+ __typename
3
4
  ...CustomerTokenFragment
4
5
  createdAt
5
6
  valid
7
+ xMagentoCacheId
6
8
  }
7
9
  }
@@ -1,5 +1,11 @@
1
1
  import { globalApolloClient } from '@graphcommerce/graphql'
2
- import { ApolloLink, fromPromise, onError, setContext } from '@graphcommerce/graphql/apollo'
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.readQuery({ query: CustomerTokenDocument })
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.authorization) {
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.57",
5
+ "version": "9.0.0-canary.58",
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.57",
16
- "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.57",
17
- "@graphcommerce/framer-next-pages": "^9.0.0-canary.57",
18
- "@graphcommerce/framer-utils": "^9.0.0-canary.57",
19
- "@graphcommerce/graphql": "^9.0.0-canary.57",
20
- "@graphcommerce/graphql-mesh": "^9.0.0-canary.57",
21
- "@graphcommerce/image": "^9.0.0-canary.57",
22
- "@graphcommerce/magento-graphql": "^9.0.0-canary.57",
23
- "@graphcommerce/magento-store": "^9.0.0-canary.57",
24
- "@graphcommerce/next-ui": "^9.0.0-canary.57",
25
- "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.57",
26
- "@graphcommerce/react-hook-form": "^9.0.0-canary.57",
27
- "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.57",
15
+ "@graphcommerce/ecommerce-ui": "^9.0.0-canary.58",
16
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.58",
17
+ "@graphcommerce/framer-next-pages": "^9.0.0-canary.58",
18
+ "@graphcommerce/framer-utils": "^9.0.0-canary.58",
19
+ "@graphcommerce/graphql": "^9.0.0-canary.58",
20
+ "@graphcommerce/graphql-mesh": "^9.0.0-canary.58",
21
+ "@graphcommerce/image": "^9.0.0-canary.58",
22
+ "@graphcommerce/magento-graphql": "^9.0.0-canary.58",
23
+ "@graphcommerce/magento-store": "^9.0.0-canary.58",
24
+ "@graphcommerce/next-ui": "^9.0.0-canary.58",
25
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.58",
26
+ "@graphcommerce/react-hook-form": "^9.0.0-canary.58",
27
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.58",
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
+ }
@@ -5,4 +5,5 @@ extend type Query {
5
5
  extend type CustomerToken {
6
6
  createdAt: String @deprecated(reason: "Value is not used in GraphCommerce, but still filled in.")
7
7
  valid: Boolean
8
+ xMagentoCacheId: String
8
9
  }
package/typePolicies.ts CHANGED
@@ -16,6 +16,7 @@ const generateCustomerToken: FieldPolicy<Mutation['generateCustomerToken']> = {
16
16
  token: options.readField('token', incoming) as string,
17
17
  createdAt: new Date().toUTCString(),
18
18
  valid: true,
19
+ xMagentoCacheId: null,
19
20
  },
20
21
  },
21
22
  })