@graphcommerce/graphql 8.1.0-canary.9 → 9.0.0-canary.55

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,117 @@
1
1
  # Change Log
2
2
 
3
+ ## 9.0.0-canary.55
4
+
5
+ ## 9.0.0-canary.54
6
+
7
+ ## 8.1.0-canary.53
8
+
9
+ ## 8.1.0-canary.52
10
+
11
+ ## 8.1.0-canary.51
12
+
13
+ ## 8.1.0-canary.50
14
+
15
+ ## 8.1.0-canary.49
16
+
17
+ ## 8.1.0-canary.48
18
+
19
+ ## 8.1.0-canary.47
20
+
21
+ ### Minor Changes
22
+
23
+ - [#2315](https://github.com/graphcommerce-org/graphcommerce/pull/2315) [`fb9118e`](https://github.com/graphcommerce-org/graphcommerce/commit/fb9118ec819d1b507d4fbde04f2ca34ec01ecf49) - Add guestOrder functionality, remove `size: 'max'` from getStaticProps
24
+ ([@Giovanni-Schroevers](https://github.com/Giovanni-Schroevers))
25
+
26
+ ## 8.1.0-canary.46
27
+
28
+ ### Patch Changes
29
+
30
+ - [#2314](https://github.com/graphcommerce-org/graphcommerce/pull/2314) [`fe8fce7`](https://github.com/graphcommerce-org/graphcommerce/commit/fe8fce7f5a82e227ed4a1992415af73fa7d0bb79) - Importing \*.interceptor is NOT allowed
31
+ ([@paales](https://github.com/paales))
32
+
33
+ ## 8.1.0-canary.45
34
+
35
+ ### Patch Changes
36
+
37
+ - [#2216](https://github.com/graphcommerce-org/graphcommerce/pull/2216) [`d066e6f`](https://github.com/graphcommerce-org/graphcommerce/commit/d066e6f904ca68a8771ab73ce30aa06d11fc71b6) - When loading a page, always create a new graphql client, so that cache isn't shared for each request, causing an unnecessary large page size.
38
+ ([@paales](https://github.com/paales))
39
+
40
+ ## 8.1.0-canary.44
41
+
42
+ ## 8.1.0-canary.43
43
+
44
+ ## 8.1.0-canary.42
45
+
46
+ ### Patch Changes
47
+
48
+ - [#2308](https://github.com/graphcommerce-org/graphcommerce/pull/2308) [`8faa5ac`](https://github.com/graphcommerce-org/graphcommerce/commit/8faa5ac618ecfdacd6d5eb751b4110c423aef97f) - Added Draft Mode support. When enabled it will be shown.
49
+ ([@paales](https://github.com/paales))
50
+
51
+ ## 8.1.0-canary.41
52
+
53
+ ## 8.1.0-canary.40
54
+
55
+ ## 8.1.0-canary.39
56
+
57
+ ## 8.1.0-canary.38
58
+
59
+ ## 8.1.0-canary.37
60
+
61
+ ## 8.1.0-canary.36
62
+
63
+ ## 8.1.0-canary.35
64
+
65
+ ## 8.1.0-canary.34
66
+
67
+ ## 8.1.0-canary.33
68
+
69
+ ## 8.1.0-canary.32
70
+
71
+ ## 8.1.0-canary.31
72
+
73
+ ## 8.1.0-canary.30
74
+
75
+ ## 8.1.0-canary.29
76
+
77
+ ## 8.1.0-canary.28
78
+
79
+ ## 8.1.0-canary.27
80
+
81
+ ## 8.1.0-canary.26
82
+
83
+ ## 8.1.0-canary.25
84
+
85
+ ## 8.1.0-canary.24
86
+
87
+ ## 8.1.0-canary.23
88
+
89
+ ## 8.1.0-canary.22
90
+
91
+ ## 8.1.0-canary.21
92
+
93
+ ## 8.1.0-canary.20
94
+
95
+ ## 8.1.0-canary.19
96
+
97
+ ## 8.1.0-canary.18
98
+
99
+ ## 8.1.0-canary.17
100
+
101
+ ## 8.1.0-canary.16
102
+
103
+ ## 8.1.0-canary.15
104
+
105
+ ## 8.1.0-canary.14
106
+
107
+ ## 8.1.0-canary.13
108
+
109
+ ## 8.1.0-canary.12
110
+
111
+ ## 8.1.0-canary.11
112
+
113
+ ## 8.1.0-canary.10
114
+
3
115
  ## 8.1.0-canary.9
4
116
 
5
117
  ## 8.1.0-canary.8
@@ -5,12 +5,18 @@ import {
5
5
  InMemoryCache,
6
6
  ApolloProvider,
7
7
  HttpLink,
8
+ DefaultOptions,
8
9
  } from '@apollo/client'
9
10
  // eslint-disable-next-line import/no-extraneous-dependencies
10
11
  import { useStorefrontConfig } from '@graphcommerce/next-ui/hooks/useStorefrontConfig'
11
12
  import type { AppProps } from 'next/app'
12
13
  import { useCallback, useEffect, useRef, useState } from 'react'
13
- import { ApolloClientConfig, graphqlConfig, ApolloClientConfigInput } from '../../config'
14
+ import {
15
+ ApolloClientConfig,
16
+ graphqlConfig,
17
+ ApolloClientConfigInput,
18
+ PreviewConfig,
19
+ } from '../../config'
14
20
  import fragments from '../../generated/fragments.json'
15
21
  import { createCacheReviver } from './createCacheReviver'
16
22
  import { errorLink } from './errorLink'
@@ -31,13 +37,19 @@ export type GraphQLProviderProps = AppProps &
31
37
  * Take a look at the props to see possible customization options.
32
38
  */
33
39
  export function GraphQLProvider(props: GraphQLProviderProps) {
34
- const { children, links, migrations, policies, pageProps } = props
40
+ const { children, links, migrations, policies, pageProps, router } = props
35
41
  const state = (pageProps as { apolloState?: NormalizedCacheObject }).apolloState
36
42
 
37
43
  const stateRef = useRef(state)
38
44
 
39
45
  const storefront = useStorefrontConfig()
40
- const conf = graphqlConfig({ links, migrations, policies, storefront })
46
+ const conf = graphqlConfig({
47
+ links,
48
+ migrations,
49
+ policies,
50
+ storefront,
51
+ preview: router.isPreview,
52
+ })
41
53
  const config = useRef<ApolloClientConfig>(conf)
42
54
  config.current = conf
43
55
 
@@ -62,7 +74,17 @@ export function GraphQLProvider(props: GraphQLProviderProps) {
62
74
  if (stateRef.current) cache.restore(stateRef.current)
63
75
 
64
76
  const ssrMode = typeof window === 'undefined'
65
- return new ApolloClient({ link, cache, name: 'web', ssrMode })
77
+ return new ApolloClient({
78
+ link,
79
+ cache,
80
+ name: 'web',
81
+ ssrMode,
82
+ defaultOptions: {
83
+ preview: {
84
+ preview: router.isPreview,
85
+ } as PreviewConfig,
86
+ } as DefaultOptions,
87
+ })
66
88
  })
67
89
 
68
90
  useEffect(() => {
@@ -30,6 +30,7 @@ export const persistenceMapper = (data: string): Promise<string> => {
30
30
  'ROOT_QUERY.countries',
31
31
  'ROOT_QUERY.checkoutAgreements',
32
32
  'ROOT_QUERY.storeConfig',
33
+ 'ROOT_QUERY.guestOrder',
33
34
  'ROOT_QUERY.__type*',
34
35
  '*Product:{"uid":"*"}.crosssell_products',
35
36
  ])
@@ -0,0 +1,85 @@
1
+ import { cssFlag, cssNotFlag } from '@graphcommerce/next-ui'
2
+ import { Box, Skeleton, SkeletonOwnProps, SkeletonProps, SxProps, Theme } from '@mui/material'
3
+ import type { OverrideProps } from '@mui/material/OverridableComponent'
4
+ import React, { createContext, useContext, useMemo } from 'react'
5
+
6
+ type MaskProp = { skeleton?: SkeletonProps }
7
+
8
+ interface InContextMaskTypeMap<
9
+ AdditionalProps = MaskProp,
10
+ RootComponent extends React.ElementType = 'div',
11
+ > {
12
+ props: AdditionalProps & SkeletonOwnProps
13
+ defaultComponent: RootComponent
14
+ }
15
+
16
+ export type InContextMaskProps<
17
+ RootComponent extends React.ElementType = InContextMaskTypeMap['defaultComponent'],
18
+ AdditionalProps = MaskProp,
19
+ > = OverrideProps<InContextMaskTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
20
+ component?: React.ElementType
21
+ }
22
+
23
+ type InContextMaskContextType = { mask: boolean }
24
+
25
+ const InContextMaskContext = createContext<InContextMaskContextType | null>(null)
26
+
27
+ export function useInContextInputMask() {
28
+ const context = useContext(InContextMaskContext)
29
+ if (!context) {
30
+ console.warn(
31
+ "useInContextInputMask was used without a InContextMaskProvider, this means that customer specific pricing probably isn't working.",
32
+ )
33
+ return { mask: false }
34
+ }
35
+ return context
36
+ }
37
+
38
+ export function InContextMaskProvider(props: { mask: boolean; children: React.ReactNode }) {
39
+ const { mask = false, children } = props
40
+ return (
41
+ <InContextMaskContext.Provider value={useMemo(() => ({ mask }), [mask])}>
42
+ {children}
43
+ </InContextMaskContext.Provider>
44
+ )
45
+ }
46
+
47
+ export function useInContextInputMaskSx(props: { sx?: SxProps<Theme>; skeleton?: SkeletonProps }) {
48
+ const { sx = [], skeleton } = props
49
+ const { mask } = useInContextInputMask()
50
+
51
+ return {
52
+ mask,
53
+ componentSx: [
54
+ mask && {
55
+ [cssFlag('in-context')]: { display: 'none' },
56
+ },
57
+ ...(Array.isArray(sx) ? sx : [sx]),
58
+ ],
59
+ maskSx: [
60
+ {
61
+ display: 'inline-block',
62
+ [cssNotFlag('in-context')]: { display: 'none' },
63
+ },
64
+ ...(Array.isArray(sx) ? sx : [sx]),
65
+ ...(Array.isArray(skeleton?.sx) ? skeleton.sx : [skeleton?.sx]),
66
+ ],
67
+ }
68
+ }
69
+
70
+ /**
71
+ * A component that renders a skeleton mask when the user is signed in.
72
+ */
73
+ export function InContextMask(props: InContextMaskProps) {
74
+ const { skeleton, children, ...rest } = props
75
+ const { mask, componentSx, maskSx } = useInContextInputMaskSx(props)
76
+
77
+ return (
78
+ <>
79
+ <Box {...rest} sx={componentSx}>
80
+ {children}
81
+ </Box>
82
+ {mask && <Skeleton {...rest} {...skeleton} sx={maskSx} />}
83
+ </>
84
+ )
85
+ }
package/config.ts CHANGED
@@ -1,7 +1,15 @@
1
1
  import { ApolloLink, TypePolicies } from '@apollo/client'
2
2
  import type { GraphCommerceStorefrontConfig } from '@graphcommerce/next-config'
3
+ import type { SetRequired } from 'type-fest'
3
4
  import { MigrateCache } from './components/GraphQLProvider/migrateCache'
4
5
 
6
+ export interface PreviewData {}
7
+
8
+ export type PreviewConfig = {
9
+ preview?: boolean
10
+ previewData?: PreviewData & Record<string, unknown>
11
+ }
12
+
5
13
  export type ApolloClientConfigInput = {
6
14
  storefront: GraphCommerceStorefrontConfig
7
15
 
@@ -17,11 +25,14 @@ export type ApolloClientConfigInput = {
17
25
  * yet, we run these migrations.
18
26
  */
19
27
  migrations?: MigrateCache[]
20
- }
28
+ } & PreviewConfig
21
29
 
22
- export type ApolloClientConfig = Required<ApolloClientConfigInput>
30
+ export type ApolloClientConfig = SetRequired<
31
+ ApolloClientConfigInput,
32
+ 'links' | 'policies' | 'migrations'
33
+ >
23
34
 
24
35
  export function graphqlConfig(config: ApolloClientConfigInput): ApolloClientConfig {
25
- const { storefront, links = [], policies = [], migrations = [] } = config
26
- return { storefront, links, policies, migrations }
36
+ const { storefront, links = [], policies = [], migrations = [], ...rest } = config
37
+ return { storefront, links, policies, migrations, ...rest }
27
38
  }
@@ -0,0 +1,17 @@
1
+ import { ApolloClient } from '@apollo/client'
2
+ import type { InContextInput } from '@graphcommerce/graphql-mesh'
3
+
4
+ export function getInContextInput(client: ApolloClient<any>): InContextInput | undefined {
5
+ return {}
6
+ }
7
+
8
+ /**
9
+ * Defines a method to handle the current context for the query.
10
+ *
11
+ * Other plugins should be able to define their own scopes and create a plugin on this method to augment the specific scope.
12
+ *
13
+ * @see @graphcommerce/magento-customer/plugins/magentoCustomerGetInContext.ts
14
+ *
15
+ * Note: ONLY return a value if the frontend should use the inContext directive.
16
+ */
17
+ export const useInContextInput = (): InContextInput | undefined => undefined
@@ -0,0 +1,57 @@
1
+ import type { InputMaybe, InContextInput } from '@graphcommerce/graphql-mesh'
2
+ // eslint-disable-next-line import/no-extraneous-dependencies
3
+ import { useIsSSR } from '@graphcommerce/next-ui/hooks/useIsSsr'
4
+ // eslint-disable-next-line import/no-extraneous-dependencies
5
+ import { getCssFlag, removeCssFlag, setCssFlag } from '@graphcommerce/next-ui/utils/cssFlags'
6
+ import { useEffect } from 'react'
7
+ import { QueryHookOptions, QueryResult, TypedDocumentNode, useQuery } from '../apollo'
8
+ import { useInContextInput } from './useInContextInput'
9
+
10
+ /**
11
+ * Creates a query that allows fetching data for logged in customers, but have
12
+ * a fallback for guest users.
13
+ *
14
+ * - Shows a global pageload indicator when loading customer specific information.
15
+ *
16
+ * When not to use this?
17
+ * - When a query is always scoped. This method specifically targets queries that can resolve unscoped (guest) and both scoped (customer) data.
18
+ *
19
+ * Usage:
20
+ * - Define a `@inContext(context: $context)` directive in your query
21
+ * - Use the useInContextQuery
22
+ */
23
+ export function useInContextQuery<
24
+ Q,
25
+ V extends { context?: InputMaybe<InContextInput>; [index: string]: unknown },
26
+ >(
27
+ document: TypedDocumentNode<Q, V>,
28
+ options: QueryHookOptions<Q, V>,
29
+ unscopedResult: Q,
30
+ ): Omit<QueryResult<Q, V>, 'data'> & { data: Q; mask: boolean } {
31
+ const { skip = true } = options
32
+ const context = useInContextInput()
33
+ const isSsr = useIsSSR()
34
+
35
+ useEffect(() => {
36
+ if (isSsr) return
37
+ if (context && !getCssFlag('in-context')) setCssFlag('in-context', true)
38
+ else if (!context && getCssFlag('in-context')) removeCssFlag('in-context')
39
+ }, [context, isSsr])
40
+
41
+ const clientQuery = useQuery<Q, V>(document, {
42
+ ...options,
43
+ variables: { ...options.variables, context } as V,
44
+ skip: skip && !context,
45
+ })
46
+
47
+ let { data } = clientQuery
48
+ if (!skip) data ??= clientQuery.previousData
49
+
50
+ // If the user is logged in we might need to show a skeleton:
51
+ let mask = isSsr
52
+ if (!isSsr && context) {
53
+ mask = !skip ? !clientQuery.data && !clientQuery.previousData : !clientQuery.data
54
+ }
55
+
56
+ return { ...clientQuery, data: data ?? unscopedResult, mask }
57
+ }
package/index.ts CHANGED
@@ -3,3 +3,8 @@ export * from './components/GraphQLProvider'
3
3
  export { default as fragments } from './generated/fragments.json'
4
4
  export * from './generated/types'
5
5
  export * from './config'
6
+ export * from './utils/getPreviewData'
7
+ export * from './utils/cachePolicy'
8
+ export * from './components/InContextMask/InContextMask'
9
+ export * from './hooks/useInContextInput'
10
+ export * from './hooks/useInContextQuery'
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/graphql",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "8.1.0-canary.9",
5
+ "version": "9.0.0-canary.55",
6
6
  "sideEffects": false,
7
7
  "main": "index.ts",
8
8
  "prettier": "@graphcommerce/prettier-config-pwa",
@@ -13,8 +13,8 @@
13
13
  }
14
14
  },
15
15
  "dependencies": {
16
- "@graphcommerce/graphql-codegen-near-operation-file": "8.1.0-canary.9",
17
- "@graphcommerce/graphql-codegen-relay-optimizer-plugin": "8.1.0-canary.9",
16
+ "@graphcommerce/graphql-codegen-near-operation-file": "9.0.0-canary.55",
17
+ "@graphcommerce/graphql-codegen-relay-optimizer-plugin": "9.0.0-canary.55",
18
18
  "@graphql-codegen/add": "5.0.1",
19
19
  "@graphql-codegen/fragment-matcher": "5.0.1",
20
20
  "@graphql-codegen/introspection": "4.0.1",
@@ -28,9 +28,9 @@
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@apollo/client": "^3",
31
- "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.9",
32
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.9",
33
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.9",
31
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.55",
32
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.55",
33
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.55",
34
34
  "graphql": "^16.7.1",
35
35
  "react": "^18.2.0",
36
36
  "react-dom": "^18.2.0"
@@ -0,0 +1,16 @@
1
+ """
2
+ By providing these values the Apollo Client cache will automatically scope the cache to these specific values.
3
+
4
+ If a InContext is provided
5
+ """
6
+ input InContextInput {
7
+ loggedIn: Boolean
8
+
9
+ # storeCode: String
10
+ # contentCurrency: String
11
+ }
12
+
13
+ """
14
+ Certain values are only available in the context of a session and need to have the cache scoped to these values.
15
+ """
16
+ directive @inContext(context: InContextInput) on FIELD
@@ -0,0 +1,15 @@
1
+ import { ApolloClient, FetchPolicy } from '@apollo/client'
2
+ import { getPreviewData } from './getPreviewData'
3
+
4
+ export function cachePolicy(
5
+ client: ApolloClient<object>,
6
+ requestedCachePolicy: FetchPolicy,
7
+ ): FetchPolicy {
8
+ return process.env.NODE_ENV !== 'development' && !getPreviewData(client)?.preview
9
+ ? requestedCachePolicy
10
+ : 'network-only'
11
+ }
12
+
13
+ export function cacheFirst(client: ApolloClient<object>) {
14
+ return cachePolicy(client, 'cache-first')
15
+ }
@@ -0,0 +1,7 @@
1
+ import { ApolloClient } from '@apollo/client'
2
+ import { PreviewConfig } from '../config'
3
+
4
+ export function getPreviewData(client: ApolloClient<object>): PreviewConfig | undefined {
5
+ if ('preview' in client.defaultOptions) return client.defaultOptions.preview as PreviewConfig
6
+ return undefined
7
+ }