@graphcommerce/graphql 8.1.0-canary.43 → 8.1.0-canary.45

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,14 @@
1
1
  # Change Log
2
2
 
3
+ ## 8.1.0-canary.45
4
+
5
+ ### Patch Changes
6
+
7
+ - [#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.
8
+ ([@paales](https://github.com/paales))
9
+
10
+ ## 8.1.0-canary.44
11
+
3
12
  ## 8.1.0-canary.43
4
13
 
5
14
  ## 8.1.0-canary.42
@@ -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
+ }
@@ -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
@@ -5,3 +5,6 @@ export * from './generated/types'
5
5
  export * from './config'
6
6
  export * from './utils/getPreviewData'
7
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.43",
5
+ "version": "8.1.0-canary.45",
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.43",
17
- "@graphcommerce/graphql-codegen-relay-optimizer-plugin": "8.1.0-canary.43",
16
+ "@graphcommerce/graphql-codegen-near-operation-file": "8.1.0-canary.45",
17
+ "@graphcommerce/graphql-codegen-relay-optimizer-plugin": "8.1.0-canary.45",
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.43",
32
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.43",
33
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.43",
31
+ "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.45",
32
+ "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.45",
33
+ "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.45",
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