@graphcommerce/graphql 8.1.0-canary.40 → 8.1.0-canary.42

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.42
4
+
5
+ ### Patch Changes
6
+
7
+ - [#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.
8
+ ([@paales](https://github.com/paales))
9
+
10
+ ## 8.1.0-canary.41
11
+
3
12
  ## 8.1.0-canary.40
4
13
 
5
14
  ## 8.1.0-canary.39
@@ -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(() => {
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
  }
package/index.ts CHANGED
@@ -3,3 +3,5 @@ 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'
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.40",
5
+ "version": "8.1.0-canary.42",
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.40",
17
- "@graphcommerce/graphql-codegen-relay-optimizer-plugin": "8.1.0-canary.40",
16
+ "@graphcommerce/graphql-codegen-near-operation-file": "8.1.0-canary.42",
17
+ "@graphcommerce/graphql-codegen-relay-optimizer-plugin": "8.1.0-canary.42",
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.40",
32
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.40",
33
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.40",
31
+ "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.42",
32
+ "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.42",
33
+ "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.42",
34
34
  "graphql": "^16.7.1",
35
35
  "react": "^18.2.0",
36
36
  "react-dom": "^18.2.0"
@@ -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.interceptor'
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
+ }