@graphcommerce/graphql 6.1.1-canary.4 → 6.2.0-canary.10

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,21 @@
1
1
  # Change Log
2
2
 
3
+ ## 6.2.0-canary.10
4
+
5
+ ## 6.2.0-canary.9
6
+
7
+ ## 6.2.0-canary.8
8
+
9
+ ## 6.2.0-canary.7
10
+
11
+ ## 6.2.0-canary.6
12
+
13
+ ### Patch Changes
14
+
15
+ - [#1915](https://github.com/graphcommerce-org/graphcommerce/pull/1915) [`f4a8c3881`](https://github.com/graphcommerce-org/graphcommerce/commit/f4a8c388183e17c52e7f66536c5448749f494d7f) - Moved the injection of the links to plugins ([@paales](https://github.com/paales))
16
+
17
+ ## 6.1.1-canary.5
18
+
3
19
  ## 6.1.1-canary.4
4
20
 
5
21
  ## 6.1.1-canary.3
@@ -3,38 +3,25 @@ import {
3
3
  NormalizedCacheObject,
4
4
  ApolloLink,
5
5
  InMemoryCache,
6
- TypePolicies,
7
6
  ApolloProvider,
8
7
  HttpLink,
9
8
  } from '@apollo/client'
9
+ import { useStorefrontConfig } from '@graphcommerce/next-ui'
10
10
  import type { AppProps } from 'next/app'
11
- import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
12
- import { createCacheReviver } from '../createCacheReviver'
13
- import { errorLink } from '../errorLink'
14
- import fragments from '../generated/fragments.json'
15
- import { measurePerformanceLink } from '../measurePerformanceLink'
16
- import { MigrateCache } from '../migrateCache'
17
- import { mergeTypePolicies } from '../typePolicies'
11
+ import { useCallback, useEffect, useRef, useState } from 'react'
12
+ import { ApolloClientConfig, graphqlConfig, ApolloClientConfigInput } from '../../config'
13
+ import fragments from '../../generated/fragments.json'
14
+ import { createCacheReviver } from './createCacheReviver'
15
+ import { errorLink } from './errorLink'
16
+ import { measurePerformanceLink } from './measurePerformanceLink'
17
+ import { mergeTypePolicies } from './typePolicies'
18
18
 
19
19
  export const globalApolloClient: { current: ApolloClient<NormalizedCacheObject> | null } = {
20
20
  current: null,
21
21
  }
22
22
 
23
- export type GraphQLProviderProps = AppProps & {
24
- children: React.ReactNode
25
- /** Additional ApolloLink to add to the chain. */
26
- links?: ApolloLink[]
27
- /**
28
- * This is a list of type policies which are used to influence how cache is handled.
29
- * https://www.apollographql.com/docs/react/caching/cache-field-behavior/
30
- */
31
- policies?: TypePolicies[]
32
- /**
33
- * To upgrade the local storage to a new version when the app is updated, but the client isn't
34
- * yet, we run these migrations.
35
- */
36
- migrations?: MigrateCache[]
37
- }
23
+ export type GraphQLProviderProps = AppProps &
24
+ Omit<ApolloClientConfigInput, 'storefront'> & { children: React.ReactNode }
38
25
 
39
26
  /**
40
27
  * The GraphQLProvider allows us to configure the ApolloClient and provide it to the rest of the
@@ -43,19 +30,21 @@ export type GraphQLProviderProps = AppProps & {
43
30
  * Take a look at the props to see possible customization options.
44
31
  */
45
32
  export function GraphQLProvider(props: GraphQLProviderProps) {
46
- const { children, policies = [], migrations = [], links = [], pageProps } = props
33
+ const { children, links, migrations, policies, pageProps } = props
47
34
  const state = (pageProps as { apolloState?: NormalizedCacheObject }).apolloState
48
35
 
49
36
  const stateRef = useRef(state)
50
37
 
51
- const linksRef = useRef(links)
52
- const policiesRef = useRef(policies)
38
+ const storefront = useStorefrontConfig()
39
+ const conf = graphqlConfig({ links, migrations, policies, storefront })
40
+ const config = useRef<ApolloClientConfig>(conf)
41
+ config.current = conf
53
42
 
54
43
  const createCache = useCallback(
55
44
  () =>
56
45
  new InMemoryCache({
57
46
  possibleTypes: fragments.possibleTypes,
58
- typePolicies: mergeTypePolicies(policiesRef.current),
47
+ typePolicies: mergeTypePolicies(config.current.policies),
59
48
  }),
60
49
  [],
61
50
  )
@@ -63,7 +52,7 @@ export function GraphQLProvider(props: GraphQLProviderProps) {
63
52
  const [client] = useState(() => {
64
53
  const link = ApolloLink.from([
65
54
  ...(typeof window === 'undefined' ? [errorLink, measurePerformanceLink] : []),
66
- ...linksRef.current,
55
+ ...config.current.links,
67
56
  // The actual Http connection to the Mesh backend.
68
57
  new HttpLink({ uri: '/api/graphql', credentials: 'same-origin' }),
69
58
  ])
@@ -77,8 +66,8 @@ export function GraphQLProvider(props: GraphQLProviderProps) {
77
66
 
78
67
  useEffect(() => {
79
68
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
80
- createCacheReviver(client, createCache, policies, migrations, state)
81
- }, [client, createCache, migrations, policies, state])
69
+ createCacheReviver(client, createCache, config.current, state)
70
+ }, [client, createCache, state])
82
71
 
83
72
  globalApolloClient.current = client
84
73
 
@@ -1,7 +1,7 @@
1
1
  import { LocalStorageWrapper, CachePersistor } from 'apollo3-cache-persist'
2
- import { mergeDeep, ApolloCache, ApolloClient, NormalizedCacheObject } from './apollo'
3
- import type { StrictTypedTypePolicies } from './generated/types'
4
- import { MigrateCache, migrateCacheHandler } from './migrateCache'
2
+ import { mergeDeep, ApolloCache, ApolloClient, NormalizedCacheObject } from '../../apollo'
3
+ import { ApolloClientConfig } from '../../config'
4
+ import { migrateCacheHandler } from './migrateCache'
5
5
  import { getTypePoliciesVersion } from './typePolicies'
6
6
 
7
7
  const APOLLO_CACHE_PERSIST = 'apollo-cache-persist'
@@ -13,12 +13,11 @@ let persistor: CachePersistor<NormalizedCacheObject> | null = null
13
13
  export async function createCacheReviver(
14
14
  client: ApolloClient<NormalizedCacheObject>,
15
15
  createCache: () => ApolloCache<NormalizedCacheObject>,
16
- policies: StrictTypedTypePolicies[],
17
- migrations: MigrateCache[],
16
+ config: ApolloClientConfig,
18
17
  incomingState: NormalizedCacheObject = {},
19
18
  ) {
20
19
  let state = incomingState
21
- const typePoliciesVersion = getTypePoliciesVersion(policies)
20
+ const typePoliciesVersion = getTypePoliciesVersion(config.policies)
22
21
 
23
22
  if (typeof window !== 'undefined') {
24
23
  try {
@@ -57,7 +56,7 @@ export async function createCacheReviver(
57
56
  oldCache.restore(JSON.parse(storedState) as NormalizedCacheObject)
58
57
 
59
58
  // Run the migration
60
- migrateCacheHandler(oldCache, cache, migrations)
59
+ migrateCacheHandler(oldCache, cache, config.migrations)
61
60
 
62
61
  state = mergeDeep(cache.extract(), incomingState)
63
62
  console.info('migration complete')
@@ -0,0 +1,6 @@
1
+ export * from './createCacheReviver'
2
+ export * from './errorLink'
3
+ export * from './GraphQLProvider'
4
+ export * from './measurePerformanceLink'
5
+ export * from './migrateCache'
6
+ export * from './typePolicies'
@@ -2,7 +2,7 @@
2
2
  import { ApolloLink } from '@apollo/client'
3
3
  import type { MeshFetchHTTPInformation } from '@graphql-mesh/plugin-http-details-extensions'
4
4
  import { print } from 'graphql'
5
- import { cliHyperlink } from './lib/hyperlinker'
5
+ import { cliHyperlink } from '../../lib/hyperlinker'
6
6
 
7
7
  const running = new Map<
8
8
  string,
@@ -1,4 +1,4 @@
1
- import { ApolloCache, NormalizedCacheObject } from './apollo'
1
+ import { ApolloCache, NormalizedCacheObject } from '../../apollo'
2
2
 
3
3
  export type MigrateCache = (
4
4
  oldCache: ApolloCache<NormalizedCacheObject>,
@@ -1,5 +1,5 @@
1
1
  import type { StrictTypedTypePolicies } from '@graphcommerce/graphql'
2
- import { mergeDeep } from './apollo'
2
+ import { mergeDeep } from '../../apollo'
3
3
 
4
4
  export const mergeTypePolicies = (policies: StrictTypedTypePolicies[]): StrictTypedTypePolicies =>
5
5
  mergeDeep(...policies)
package/config.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { ApolloLink, TypePolicies } from '@apollo/client'
2
+ import type { GraphCommerceStorefrontConfig } from '@graphcommerce/next-config'
3
+ import { MigrateCache } from './components/GraphQLProvider/migrateCache'
4
+
5
+ export type ApolloClientConfigInput = {
6
+ storefront: GraphCommerceStorefrontConfig
7
+
8
+ /** Additional ApolloLink to add to the chain. */
9
+ links?: ApolloLink[]
10
+ /**
11
+ * This is a list of type policies which are used to influence how cache is handled.
12
+ * https://www.apollographql.com/docs/react/caching/cache-field-behavior/
13
+ */
14
+ policies?: TypePolicies[]
15
+ /**
16
+ * To upgrade the local storage to a new version when the app is updated, but the client isn't
17
+ * yet, we run these migrations.
18
+ */
19
+ migrations?: MigrateCache[]
20
+ }
21
+
22
+ export type ApolloClientConfig = Required<ApolloClientConfigInput>
23
+
24
+ export function graphqlConfig(config: ApolloClientConfigInput): ApolloClientConfig {
25
+ const { storefront, links = [], policies = [], migrations = [] } = config
26
+ return { storefront, links, policies, migrations }
27
+ }
package/index.ts CHANGED
@@ -1,13 +1,5 @@
1
- export * from './generated/types'
2
-
3
- export * from './measurePerformanceLink'
4
- export * from './errorLink'
5
-
6
- export { default as fragments } from './generated/fragments.json'
7
-
8
- export * from './migrateCache'
9
- export * from './typePolicies'
10
- export * from './createCacheReviver'
11
-
12
1
  export * from './apollo'
13
2
  export * from './components/GraphQLProvider'
3
+ export { default as fragments } from './generated/fragments.json'
4
+ export * from './generated/types'
5
+ export * from './config'
@@ -1,5 +1,3 @@
1
- 'use strict'
2
-
3
1
  /*
4
2
  A hyperlink is opened upon encountering an OSC 8 escape sequence with the target URI. The syntax is
5
3
  OSC 8 ; params ; URI BEL|ST
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": "6.1.1-canary.4",
5
+ "version": "6.2.0-canary.10",
6
6
  "sideEffects": false,
7
7
  "main": "index.ts",
8
8
  "prettier": "@graphcommerce/prettier-config-pwa",
@@ -14,8 +14,8 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@apollo/client": "^3.7.10",
17
- "@graphcommerce/graphql-codegen-near-operation-file": "6.1.1-canary.4",
18
- "@graphcommerce/graphql-codegen-relay-optimizer-plugin": "6.1.1-canary.4",
17
+ "@graphcommerce/graphql-codegen-near-operation-file": "6.2.0-canary.10",
18
+ "@graphcommerce/graphql-codegen-relay-optimizer-plugin": "6.2.0-canary.10",
19
19
  "@graphql-codegen/add": "4.0.1",
20
20
  "@graphql-codegen/fragment-matcher": "4.0.1",
21
21
  "@graphql-codegen/introspection": "3.0.1",
@@ -29,9 +29,9 @@
29
29
  "graphql": "16.6.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@graphcommerce/eslint-config-pwa": "6.1.1-canary.4",
33
- "@graphcommerce/prettier-config-pwa": "6.1.1-canary.4",
34
- "@graphcommerce/typescript-config-pwa": "6.1.1-canary.4"
32
+ "@graphcommerce/eslint-config-pwa": "6.2.0-canary.10",
33
+ "@graphcommerce/prettier-config-pwa": "6.2.0-canary.10",
34
+ "@graphcommerce/typescript-config-pwa": "6.2.0-canary.10"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "react": "^18.2.0",