@apollo/client 3.6.2 → 3.6.5

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.
Files changed (84) hide show
  1. package/apollo-client.cjs +164 -105
  2. package/apollo-client.cjs.map +1 -1
  3. package/apollo-client.min.cjs +1 -1
  4. package/cache/cache.cjs +9 -10
  5. package/cache/cache.cjs.map +1 -1
  6. package/cache/cache.cjs.native.js +2288 -0
  7. package/cache/inmemory/readFromStore.d.ts.map +1 -1
  8. package/cache/inmemory/readFromStore.js +10 -11
  9. package/cache/inmemory/readFromStore.js.map +1 -1
  10. package/core/ApolloClient.d.ts.map +1 -1
  11. package/core/ApolloClient.js.map +1 -1
  12. package/core/ObservableQuery.d.ts.map +1 -1
  13. package/core/ObservableQuery.js +5 -3
  14. package/core/ObservableQuery.js.map +1 -1
  15. package/core/core.cjs +5 -3
  16. package/core/core.cjs.map +1 -1
  17. package/core/core.cjs.native.js +2141 -0
  18. package/errors/errors.cjs.native.js +48 -0
  19. package/invariantErrorCodes.js +1 -1
  20. package/link/batch/batch.cjs.native.js +161 -0
  21. package/link/batch-http/batch-http.cjs.native.js +127 -0
  22. package/link/context/context.cjs.native.js +38 -0
  23. package/link/core/core.cjs.native.js +121 -0
  24. package/link/error/error.cjs.native.js +90 -0
  25. package/link/http/http.cjs.native.js +320 -0
  26. package/link/http/selectHttpOptionsAndBody.d.ts.map +1 -1
  27. package/link/http/selectHttpOptionsAndBody.js.map +1 -1
  28. package/link/persisted-queries/persisted-queries.cjs.native.js +174 -0
  29. package/link/retry/retry.cjs.native.js +170 -0
  30. package/link/schema/schema.cjs.native.js +56 -0
  31. package/link/subscriptions/subscriptions.cjs.native.js +45 -0
  32. package/link/utils/utils.cjs.native.js +115 -0
  33. package/link/ws/ws.cjs.native.js +28 -0
  34. package/main.cjs.native.js +16 -0
  35. package/package.json +16 -17
  36. package/react/components/components.cjs.native.js +79 -0
  37. package/react/context/context.cjs.native.js +67 -0
  38. package/react/hoc/hoc.cjs.native.js +325 -0
  39. package/react/hooks/hooks.cjs +168 -99
  40. package/react/hooks/hooks.cjs.map +1 -1
  41. package/react/hooks/hooks.cjs.native.js +612 -0
  42. package/react/hooks/useLazyQuery.d.ts.map +1 -1
  43. package/react/hooks/useLazyQuery.js +12 -23
  44. package/react/hooks/useLazyQuery.js.map +1 -1
  45. package/react/hooks/useMutation.js +7 -7
  46. package/react/hooks/useMutation.js.map +1 -1
  47. package/react/hooks/useQuery.d.ts +6 -2
  48. package/react/hooks/useQuery.d.ts.map +1 -1
  49. package/react/hooks/useQuery.js +52 -40
  50. package/react/hooks/useQuery.js.map +1 -1
  51. package/react/hooks/useSubscription.d.ts.map +1 -1
  52. package/react/hooks/useSubscription.js +17 -7
  53. package/react/hooks/useSubscription.js.map +1 -1
  54. package/react/hooks/useSyncExternalStore.d.ts +4 -0
  55. package/react/hooks/useSyncExternalStore.d.ts.map +1 -0
  56. package/react/hooks/useSyncExternalStore.js +48 -0
  57. package/react/hooks/useSyncExternalStore.js.map +1 -0
  58. package/react/parser/parser.cjs.native.js +103 -0
  59. package/react/react.cjs.native.js +22 -0
  60. package/react/ssr/ssr.cjs.native.js +150 -0
  61. package/testing/core/core.cjs.native.js +288 -0
  62. package/testing/testing.cjs.native.js +58 -0
  63. package/utilities/common/canUse.d.ts +2 -0
  64. package/utilities/common/canUse.d.ts.map +1 -1
  65. package/utilities/common/canUse.js +6 -2
  66. package/utilities/common/canUse.js.map +1 -1
  67. package/utilities/common/mergeDeep.d.ts.map +1 -1
  68. package/utilities/common/mergeDeep.js +8 -11
  69. package/utilities/common/mergeDeep.js.map +1 -1
  70. package/utilities/common/mergeOptions.d.ts +1 -1
  71. package/utilities/common/mergeOptions.d.ts.map +1 -1
  72. package/utilities/common/mergeOptions.js +1 -1
  73. package/utilities/common/mergeOptions.js.map +1 -1
  74. package/utilities/globals/globals.cjs.native.js +56 -0
  75. package/utilities/observables/Concast.d.ts.map +1 -1
  76. package/utilities/observables/Concast.js +5 -2
  77. package/utilities/observables/Concast.js.map +1 -1
  78. package/utilities/policies/pagination.d.ts.map +1 -1
  79. package/utilities/policies/pagination.js +9 -7
  80. package/utilities/policies/pagination.js.map +1 -1
  81. package/utilities/utilities.cjs +30 -23
  82. package/utilities/utilities.cjs.map +1 -1
  83. package/utilities/utilities.cjs.native.js +1281 -0
  84. package/version.js +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"useLazyQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useLazyQuery.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAGrD,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAM3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAIlD,IAAM,aAAa,GAAG;IACpB,SAAS;IACT,WAAW;IACX,WAAW;IACX,aAAa;IACb,cAAc;IACd,iBAAiB;CACT,CAAC;AAEX,MAAM,UAAU,YAAY,CAC1B,KAA0D,EAC1D,OAAiD;IAEjD,IAAM,aAAa,GAAG,gBAAgB,CACpC,eAAe,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,EAC1C,KAAK,CACN,CAAC;IAEF,IAAM,cAAc,GAAG,MAAM,EAAoD,CAAC;IAClF,IAAM,cAAc,GAAG,aAAa,CAAC,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC;IACtE,IAAM,kBAAkB,GACtB,CAAC,OAAO,IAAI,OAAO,CAAC,WAAW,CAAC;QAChC,CAAC,cAAc,CAAC,OAAO,IAAI,cAAc,CAAC,OAAO,CAAC,WAAW,CAAC;QAC9D,CAAC,cAAc,IAAI,cAAc,CAAC,WAAW,CAAC;QAC9C,aAAa,CAAC;IAEhB,IAAM,cAAc,GAAG,aAAa,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CAAC,EAAE,CAAC;YAE7E,IACE,iBAAiB,CAAC,KAAK;gBACvB,eAAe,CAAC,iBAAiB,CAAC,MAAM,CAAC,EACzC;gBAEE,IAAA,KACE,MAAM,CAAC,UAAU,CAAC,OAAO,YADP,EAApB,WAAW,mBAAG,MAAM,KAAA,CACQ;gBAE9B,IAAI,WAAW,KAAK,MAAM,EAAE;oBAC1B,MAAM,iBAAiB,CAAC,KAAK,IAAI,IAAI,WAAW,CAAC;wBAC/C,aAAa,EAAE,iBAAiB,CAAC,MAAM;qBACxC,CAAC,CAAC;iBACJ;aACF;YAED,OAAO,aAAa,CAAC,aAAa,CAAC,iBAAiB,CAAC,CAAC;QAExD,CAAC,CAAC,CAAC,IAAI,CAAC,UAAA,WAAW,IAAI,OAAA,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,YAAY,CAAC,EAAxC,CAAwC,CAAC,CAAC;QAGjE,aAAa,CAAC,WAAW,EAAE,CAAC;QAI5B,OAAO,CAAC,KAAK,CAAC,cAAO,CAAC,CAAC,CAAC;QAExB,OAAO,OAAO,CAAC;IACjB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport { useCallback, useMemo, useRef } from 'react';\n\nimport { OperationVariables } from '../../core';\nimport { ApolloError } from '../../errors';\nimport {\n LazyQueryHookOptions,\n LazyQueryResultTuple,\n QueryResult,\n} from '../types/types';\nimport { useInternalState } from './useQuery';\nimport { useApolloClient } from './useApolloClient';\nimport { isNonEmptyArray } from '../../utilities';\n\n// The following methods, when called will execute the query, regardless of\n// whether the useLazyQuery execute function was called before.\nconst EAGER_METHODS = [\n 'refetch',\n 'reobserve',\n 'fetchMore',\n 'updateQuery',\n 'startPolling',\n 'subscribeToMore',\n] as const;\n\nexport function useLazyQuery<TData = any, TVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LazyQueryHookOptions<TData, TVariables>\n): LazyQueryResultTuple<TData, TVariables> {\n const internalState = useInternalState(\n useApolloClient(options && options.client),\n query,\n );\n\n const execOptionsRef = useRef<Partial<LazyQueryHookOptions<TData, TVariables>>>();\n const defaultOptions = internalState.client.defaultOptions.watchQuery;\n const initialFetchPolicy =\n (options && options.fetchPolicy) ||\n (execOptionsRef.current && execOptionsRef.current.fetchPolicy) ||\n (defaultOptions && defaultOptions.fetchPolicy) ||\n \"cache-first\";\n\n const useQueryResult = internalState.useQuery({\n ...options,\n ...execOptionsRef.current,\n skip: !execOptionsRef.current,\n });\n\n const result: QueryResult<TData, TVariables> =\n Object.assign(useQueryResult, {\n called: !!execOptionsRef.current,\n });\n\n // We use useMemo here to make sure the eager methods have a stable identity.\n const eagerMethods = useMemo(() => {\n const eagerMethods: Record<string, any> = {};\n for (const key of EAGER_METHODS) {\n const method = result[key];\n eagerMethods[key] = function () {\n if (!execOptionsRef.current) {\n execOptionsRef.current = Object.create(null);\n // Only the first time populating execOptionsRef.current matters here.\n internalState.forceUpdate();\n }\n return method.apply(this, arguments);\n };\n }\n\n return eagerMethods;\n }, []);\n\n Object.assign(result, eagerMethods);\n\n const execute = useCallback<\n LazyQueryResultTuple<TData, TVariables>[0]\n >(executeOptions => {\n const promise = result.reobserve(\n execOptionsRef.current = executeOptions ? {\n ...executeOptions,\n fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy,\n } : {\n fetchPolicy: initialFetchPolicy,\n },\n ).then(apolloQueryResult => {\n // If this.observable.options.fetchPolicy is \"standby\", the\n // apolloQueryResult we receive here can be undefined, so we call\n // getCurrentResult to obtain a stub result.\n // TODO Investigate whether standby queries could return this stub result\n // in the first place.\n apolloQueryResult = apolloQueryResult || internalState[\"getCurrentResult\"]();\n\n if (\n apolloQueryResult.error ||\n isNonEmptyArray(apolloQueryResult.errors)\n ) {\n const {\n errorPolicy = \"none\",\n } = result.observable.options;\n\n if (errorPolicy === \"none\") {\n throw apolloQueryResult.error || new ApolloError({\n graphQLErrors: apolloQueryResult.errors,\n });\n }\n }\n\n return internalState.toQueryResult(apolloQueryResult);\n\n }).then(queryResult => Object.assign(queryResult, eagerMethods));\n\n // Deliver the loading state for this reobservation immediately.\n internalState.forceUpdate();\n\n // Because the return value of `useLazyQuery` is usually floated, we need\n // to catch the promise to prevent unhandled rejections.\n promise.catch(() => {});\n\n return promise;\n }, []);\n\n return [execute, result];\n}\n"]}
1
+ {"version":3,"file":"useLazyQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useLazyQuery.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAGrD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAM/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAIpD,IAAM,aAAa,GAAG;IACpB,SAAS;IACT,WAAW;IACX,WAAW;IACX,aAAa;IACb,cAAc;IACd,iBAAiB;CACT,CAAC;AAEX,MAAM,UAAU,YAAY,CAC1B,KAA0D,EAC1D,OAAiD;IAEjD,IAAM,aAAa,GAAG,gBAAgB,CACpC,eAAe,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,EAC1C,KAAK,CACN,CAAC;IAEF,IAAM,cAAc,GAAG,MAAM,EAAoD,CAAC;IAClF,IAAM,MAAM,GAAG,cAAc,CAAC,OAAO;QACnC,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC;QAC/C,CAAC,CAAC,OAAO,CAAC;IAEZ,IAAM,cAAc,GAAG,aAAa,CAAC,QAAQ,uBACxC,MAAM,KACT,IAAI,EAAE,CAAC,cAAc,CAAC,OAAO,IAC7B,CAAC;IAEH,IAAM,kBAAkB,GACtB,cAAc,CAAC,UAAU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E,OAAO,CAAC,KAAK,CAAC,cAAO,CAAC,CAAC,CAAC;QAExB,OAAO,OAAO,CAAC;IACjB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport { useCallback, useMemo, useRef } from 'react';\n\nimport { OperationVariables } from '../../core';\nimport { mergeOptions } from '../../utilities';\nimport {\n LazyQueryHookOptions,\n LazyQueryResultTuple,\n QueryResult,\n} from '../types/types';\nimport { useInternalState } from './useQuery';\nimport { useApolloClient } from './useApolloClient';\n\n// The following methods, when called will execute the query, regardless of\n// whether the useLazyQuery execute function was called before.\nconst EAGER_METHODS = [\n 'refetch',\n 'reobserve',\n 'fetchMore',\n 'updateQuery',\n 'startPolling',\n 'subscribeToMore',\n] as const;\n\nexport function useLazyQuery<TData = any, TVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LazyQueryHookOptions<TData, TVariables>\n): LazyQueryResultTuple<TData, TVariables> {\n const internalState = useInternalState(\n useApolloClient(options && options.client),\n query,\n );\n\n const execOptionsRef = useRef<Partial<LazyQueryHookOptions<TData, TVariables>>>();\n const merged = execOptionsRef.current\n ? mergeOptions(options, execOptionsRef.current)\n : options;\n\n const useQueryResult = internalState.useQuery({\n ...merged,\n skip: !execOptionsRef.current,\n });\n\n const initialFetchPolicy =\n useQueryResult.observable.options.initialFetchPolicy ||\n internalState.getDefaultFetchPolicy();\n\n const result: QueryResult<TData, TVariables> =\n Object.assign(useQueryResult, {\n called: !!execOptionsRef.current,\n });\n\n // We use useMemo here to make sure the eager methods have a stable identity.\n const eagerMethods = useMemo(() => {\n const eagerMethods: Record<string, any> = {};\n for (const key of EAGER_METHODS) {\n const method = result[key];\n eagerMethods[key] = function () {\n if (!execOptionsRef.current) {\n execOptionsRef.current = Object.create(null);\n // Only the first time populating execOptionsRef.current matters here.\n internalState.forceUpdate();\n }\n return method.apply(this, arguments);\n };\n }\n\n return eagerMethods;\n }, []);\n\n Object.assign(result, eagerMethods);\n\n const execute = useCallback<\n LazyQueryResultTuple<TData, TVariables>[0]\n >(executeOptions => {\n execOptionsRef.current = executeOptions ? {\n ...executeOptions,\n fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy,\n } : {\n fetchPolicy: initialFetchPolicy,\n };\n\n const promise = internalState\n .asyncUpdate() // Like internalState.forceUpdate, but returns a Promise.\n .then(queryResult => Object.assign(queryResult, eagerMethods));\n\n // Because the return value of `useLazyQuery` is usually floated, we need\n // to catch the promise to prevent unhandled rejections.\n promise.catch(() => {});\n\n return promise;\n }, []);\n\n return [execute, result];\n}\n"]}
@@ -40,7 +40,7 @@ export function useMutation(mutation, options) {
40
40
  var mutationId = ++ref.current.mutationId;
41
41
  var clientOptions = mergeOptions(baseOptions, executeOptions);
42
42
  return client.mutate(clientOptions).then(function (response) {
43
- var _a, _b;
43
+ var _a, _b, _c;
44
44
  var data = response.data, errors = response.errors;
45
45
  var error = errors && errors.length > 0
46
46
  ? new ApolloError({ graphQLErrors: errors })
@@ -58,11 +58,11 @@ export function useMutation(mutation, options) {
58
58
  setResult(ref.current.result = result_1);
59
59
  }
60
60
  }
61
- (_a = baseOptions.onCompleted) === null || _a === void 0 ? void 0 : _a.call(baseOptions, response.data);
62
- (_b = executeOptions.onCompleted) === null || _b === void 0 ? void 0 : _b.call(executeOptions, response.data);
61
+ (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onCompleted) === null || _b === void 0 ? void 0 : _b.call(_a, response.data);
62
+ (_c = executeOptions.onCompleted) === null || _c === void 0 ? void 0 : _c.call(executeOptions, response.data);
63
63
  return response;
64
64
  }).catch(function (error) {
65
- var _a, _b;
65
+ var _a, _b, _c, _d;
66
66
  if (mutationId === ref.current.mutationId &&
67
67
  ref.current.isMounted) {
68
68
  var result_2 = {
@@ -76,9 +76,9 @@ export function useMutation(mutation, options) {
76
76
  setResult(ref.current.result = result_2);
77
77
  }
78
78
  }
79
- if (baseOptions.onError || clientOptions.onError) {
80
- (_a = baseOptions.onError) === null || _a === void 0 ? void 0 : _a.call(baseOptions, error);
81
- (_b = executeOptions.onError) === null || _b === void 0 ? void 0 : _b.call(executeOptions, error);
79
+ if (((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError) || clientOptions.onError) {
80
+ (_c = (_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.onError) === null || _c === void 0 ? void 0 : _c.call(_b, error);
81
+ (_d = executeOptions.onError) === null || _d === void 0 ? void 0 : _d.call(executeOptions, error);
82
82
  return { data: void 0, errors: error };
83
83
  }
84
84
  throw error;
@@ -1 +1 @@
1
- {"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAUjE,OAAO,EAGL,YAAY,GAEb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,MAAM,UAAU,WAAW,CAMzB,QAA6D,EAC7D,OAA0D;IAE1D,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC,CAAC;IAChD,kBAAkB,CAAC,QAAQ,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IAC9C,IAAA,KAAsB,QAAQ,CAAgC;QAClE,MAAM,EAAE,KAAK;QACb,OAAO,EAAE,KAAK;QACd,MAAM,QAAA;KACP,CAAC,EAJK,MAAM,QAAA,EAAE,S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gBAChD,MAAA,WAAW,CAAC,OAAO,4DAAG,KAAK,CAAC,CAAC;gBAC7B,MAAA,cAAc,CAAC,OAAO,+DAAG,KAAK,CAAC,CAAC;gBAEhC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;aACxC;YAED,MAAM,KAAK,CAAC;QACd,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAM,KAAK,GAAG,WAAW,CAAC;QACxB,SAAS,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,QAAA,EAAE,CAAC,CAAC;IACvD,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,SAAS,CAAC;QACR,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;QAE7B,OAAO;YACL,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC;QAChC,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,aAAI,KAAK,OAAA,IAAK,MAAM,EAAG,CAAC;AACzC,CAAC","sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\nimport { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport {\n MutationFunctionOptions,\n MutationHookOptions,\n MutationResult,\n MutationTuple,\n} from '../types/types';\n\nimport {\n ApolloCache,\n DefaultContext,\n mergeOptions,\n OperationVariables,\n} from '../../core';\nimport { equal } from '@wry/equality';\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport { ApolloError } from '../../errors';\nimport { useApolloClient } from './useApolloClient';\n\nexport function useMutation<\n TData = any,\n TVariables = OperationVariables,\n TContext = DefaultContext,\n TCache extends ApolloCache<any> = ApolloCache<any>,\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: MutationHookOptions<TData, TVariables, TContext>,\n): MutationTuple<TData, TVariables, TContext, TCache> {\n const client = useApolloClient(options?.client);\n verifyDocumentType(mutation, DocumentType.Mutation);\n const [result, setResult] = useState<Omit<MutationResult, 'reset'>>({\n called: false,\n loading: false,\n client,\n });\n\n const ref = useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n // TODO: Trying to assign these in a useEffect or useLayoutEffect breaks\n // higher-order components.\n {\n Object.assign(ref.current, { client, options, mutation });\n }\n\n const execute = useCallback((\n executeOptions: MutationFunctionOptions<\n TData,\n TVariables,\n TContext,\n TCache\n > = {}\n ) => {\n const {client, options, mutation} = ref.current;\n const baseOptions = { ...options, mutation };\n if (!ref.current.result.loading && !baseOptions.ignoreResults) {\n setResult(ref.current.result = {\n loading: true,\n error: void 0,\n data: void 0,\n called: true,\n client,\n });\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(\n baseOptions,\n executeOptions as any,\n );\n\n return client.mutate(clientOptions).then((response) => {\n const { data, errors } = response;\n const error =\n errors && errors.length > 0\n ? new ApolloError({ graphQLErrors: errors })\n : void 0;\n\n if (\n mutationId === ref.current.mutationId &&\n !clientOptions.ignoreResults\n ) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (ref.current.isMounted && !equal(ref.current.result, result)) {\n setResult(ref.current.result = result);\n }\n }\n\n baseOptions.onCompleted?.(response.data!);\n executeOptions.onCompleted?.(response.data!);\n return response;\n }).catch((error) => {\n if (\n mutationId === ref.current.mutationId &&\n ref.current.isMounted\n ) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult(ref.current.result = result);\n }\n }\n\n if (baseOptions.onError || clientOptions.onError) {\n baseOptions.onError?.(error);\n executeOptions.onError?.(error);\n // TODO(brian): why are we returning this here???\n return { data: void 0, errors: error };\n }\n\n throw error;\n });\n }, []);\n\n const reset = useCallback(() => {\n setResult({ called: false, loading: false, client });\n }, []);\n\n useEffect(() => {\n ref.current.isMounted = true;\n\n return () => {\n ref.current.isMounted = false;\n };\n }, []);\n\n return [execute, { reset, ...result }];\n}\n"]}
1
+ {"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAUjE,OAAO,EAGL,YAAY,GAEb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,MAAM,UAAU,WAAW,CAMzB,QAA6D,EAC7D,OAA0D;IAE1D,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC,CAAC;IAChD,kBAAkB,CAAC,QAAQ,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IAC9C,IAAA,KAAsB,QAAQ,CAAgC;QAClE,MAAM,EAAE,KAAK;QACb,OAAO,EAAE,KAAK;QACd,MAAM,QAAA;KACP,CAAC,EAJK,MAAM,QAAA,EAAE,SAAS,QAItB,CAAC;IAEH,IAAM,GAAG,GAAG,MAAM,CAAC;QACjB,MAAM,QAAA;QACN,UAAU,EAAE,CAAC;QACb,SAAS,EAAE,IAAI;QACf,MAAM,QAAA;QACN,QAAQ,UAAA;QACR,OAAO,SAAA;KACR,CAAC,CAAC;IAIH;QACE,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,MAAM,QAAA,EAAE,OAAO,SAAA,EAAE,QAAQ,UAAA,EAAE,CAAC,CAAC;KAC3D;IAED,IAAM,OAAO,GAAG,WAAW,CAAC,UAC1B,cAK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C,CAAC;IAEP,IAAM,KAAK,GAAG,WAAW,CAAC;QACxB,SAAS,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,QAAA,EAAE,CAAC,CAAC;IACvD,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,SAAS,CAAC;QACR,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;QAE7B,OAAO;YACL,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC;QAChC,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,aAAI,KAAK,OAAA,IAAK,MAAM,EAAG,CAAC;AACzC,CAAC","sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\nimport { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport {\n MutationFunctionOptions,\n MutationHookOptions,\n MutationResult,\n MutationTuple,\n} from '../types/types';\n\nimport {\n ApolloCache,\n DefaultContext,\n mergeOptions,\n OperationVariables,\n} from '../../core';\nimport { equal } from '@wry/equality';\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport { ApolloError } from '../../errors';\nimport { useApolloClient } from './useApolloClient';\n\nexport function useMutation<\n TData = any,\n TVariables = OperationVariables,\n TContext = DefaultContext,\n TCache extends ApolloCache<any> = ApolloCache<any>,\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: MutationHookOptions<TData, TVariables, TContext>,\n): MutationTuple<TData, TVariables, TContext, TCache> {\n const client = useApolloClient(options?.client);\n verifyDocumentType(mutation, DocumentType.Mutation);\n const [result, setResult] = useState<Omit<MutationResult, 'reset'>>({\n called: false,\n loading: false,\n client,\n });\n\n const ref = useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n // TODO: Trying to assign these in a useEffect or useLayoutEffect breaks\n // higher-order components.\n {\n Object.assign(ref.current, { client, options, mutation });\n }\n\n const execute = useCallback((\n executeOptions: MutationFunctionOptions<\n TData,\n TVariables,\n TContext,\n TCache\n > = {}\n ) => {\n const {client, options, mutation} = ref.current;\n const baseOptions = { ...options, mutation };\n if (!ref.current.result.loading && !baseOptions.ignoreResults) {\n setResult(ref.current.result = {\n loading: true,\n error: void 0,\n data: void 0,\n called: true,\n client,\n });\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(\n baseOptions,\n executeOptions as any,\n );\n\n return client.mutate(clientOptions).then((response) => {\n const { data, errors } = response;\n const error =\n errors && errors.length > 0\n ? new ApolloError({ graphQLErrors: errors })\n : void 0;\n\n if (\n mutationId === ref.current.mutationId &&\n !clientOptions.ignoreResults\n ) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (ref.current.isMounted && !equal(ref.current.result, result)) {\n setResult(ref.current.result = result);\n }\n }\n\n ref.current.options?.onCompleted?.(response.data!);\n executeOptions.onCompleted?.(response.data!);\n return response;\n }).catch((error) => {\n if (\n mutationId === ref.current.mutationId &&\n ref.current.isMounted\n ) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult(ref.current.result = result);\n }\n }\n\n if (ref.current.options?.onError || clientOptions.onError) {\n ref.current.options?.onError?.(error);\n executeOptions.onError?.(error);\n // TODO(brian): why are we returning this here???\n return { data: void 0, errors: error };\n }\n\n throw error;\n });\n }, []);\n\n const reset = useCallback(() => {\n setResult({ called: false, loading: false, client });\n }, []);\n\n useEffect(() => {\n ref.current.isMounted = true;\n\n return () => {\n ref.current.isMounted = false;\n };\n }, []);\n\n return [execute, { reset, ...result }];\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { OperationVariables } from '../../core';
1
+ import { OperationVariables, WatchQueryFetchPolicy } from '../../core';
2
2
  import { ApolloClient, ApolloQueryResult, DocumentNode, TypedDocumentNode } from '../../core';
3
3
  import { QueryHookOptions, QueryResult } from '../types/types';
4
4
  import { useApolloClient } from './useApolloClient';
@@ -7,8 +7,11 @@ export declare function useInternalState<TData, TVariables>(client: ApolloClient
7
7
  declare class InternalState<TData, TVariables> {
8
8
  readonly client: ReturnType<typeof useApolloClient>;
9
9
  readonly query: DocumentNode | TypedDocumentNode<TData, TVariables>;
10
- constructor(client: ReturnType<typeof useApolloClient>, query: DocumentNode | TypedDocumentNode<TData, TVariables>);
10
+ constructor(client: ReturnType<typeof useApolloClient>, query: DocumentNode | TypedDocumentNode<TData, TVariables>, previous?: InternalState<TData, TVariables>);
11
11
  forceUpdate(): void;
12
+ asyncUpdate(): Promise<QueryResult<TData, TVariables>>;
13
+ private asyncResolveFns;
14
+ private optionsToIgnoreOnce;
12
15
  useQuery(options: QueryHookOptions<TData, TVariables>): QueryResult<TData, TVariables>;
13
16
  private renderPromises;
14
17
  private queryHookOptions;
@@ -17,6 +20,7 @@ declare class InternalState<TData, TVariables> {
17
20
  private ssrDisabledResult;
18
21
  private skipStandbyResult;
19
22
  private createWatchQueryOptions;
23
+ getDefaultFetchPolicy(): WatchQueryFetchPolicy;
20
24
  private onCompleted;
21
25
  private onError;
22
26
  private observable;
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":"AAUA,OAAO,EAAgB,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAG9D,OAAO,EACL,YAAY,EACZ,iBAAiB,EAGjB,YAAY,EACZ,iBAAiB,EAElB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,gBAAgB,EAChB,WAAW,EAEZ,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AASpD,wBAAgB,QAAQ,CACtB,KAAK,GAAG,GAAG,EACX,UAAU,GAAG,kBAAkB,EAE/B,KAAK,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EAC1D,OAAO,GAAE,gB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AC,KAAK,EAAE,UAAU,CAAC;IAa7C,WAAW;IAKX,WAAW;IAQX,OAAO,CAAC,eAAe,CAEnB;IAEJ,OAAO,CAAC,mBAAmB,CAEvB;IAKJ,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC,KAAK,EAAE,UAAU,CAAC;IAgHrD,OAAO,CAAC,cAAc,CAAuC;IAC7D,OAAO,CAAC,gBAAgB,CAAsC;IAC9D,OAAO,CAAC,iBAAiB,CAAuC;IAEhE,OAAO,CAAC,UAAU;IA0FlB,OAAO,CAAC,iBAAiB,CAKtB;IAEH,OAAO,CAAC,iBAAiB,CAKtB;IAGH,OAAO,CAAC,uBAAuB;IAuD/B,qBAAqB,IAAI,qBAAqB;IAW9C,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,OAAO;IAEf,OAAO,CAAC,UAAU,CAAqC;IACvD,OAAO,CAAC,cAAc,CAGpB;IAEF,OAAO,CAAC,kBAAkB;IA6C1B,OAAO,CAAC,MAAM,CAAuC;IACrD,OAAO,CAAC,YAAY,CAAoB;IAExC,OAAO,CAAC,SAAS;IAYjB,OAAO,CAAC,sBAAsB;IAU9B,OAAO,CAAC,gBAAgB;IAexB,OAAO,CAAC,kBAAkB,CAGtB;IAEJ,aAAa,CACX,MAAM,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAC/B,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC;IA2BjC,OAAO,CAAC,0BAA0B;CAoBnC"}
@@ -1,6 +1,7 @@
1
1
  import { __assign, __rest } from "tslib";
2
+ import { invariant } from "../../utilities/globals/index.js";
2
3
  import { useCallback, useContext, useMemo, useRef, useState, } from 'react';
3
- import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
4
+ import { useSyncExternalStore } from "./useSyncExternalStore.js";
4
5
  import { equal } from '@wry/equality';
5
6
  import { mergeOptions } from "../../core/index.js";
6
7
  import { getApolloContext } from "../context/index.js";
@@ -8,7 +9,7 @@ import { ApolloError } from "../../errors/index.js";
8
9
  import { NetworkStatus, } from "../../core/index.js";
9
10
  import { DocumentType, verifyDocumentType } from "../parser/index.js";
10
11
  import { useApolloClient } from "./useApolloClient.js";
11
- import { canUseWeakMap, isNonEmptyArray, maybeDeepFreeze } from "../../utilities/index.js";
12
+ import { canUseWeakMap, canUseWeakSet, isNonEmptyArray, maybeDeepFreeze } from "../../utilities/index.js";
12
13
  var hasOwnProperty = Object.prototype.hasOwnProperty;
13
14
  export function useQuery(query, options) {
14
15
  if (options === void 0) { options = Object.create(null); }
@@ -19,7 +20,7 @@ export function useInternalState(client, query) {
19
20
  if (!stateRef.current ||
20
21
  client !== stateRef.current.client ||
21
22
  query !== stateRef.current.query) {
22
- stateRef.current = new InternalState(client, query);
23
+ stateRef.current = new InternalState(client, query, stateRef.current);
23
24
  }
24
25
  var state = stateRef.current;
25
26
  var _a = useState(0), _tick = _a[0], setTick = _a[1];
@@ -29,9 +30,11 @@ export function useInternalState(client, query) {
29
30
  return state;
30
31
  }
31
32
  var InternalState = (function () {
32
- function InternalState(client, query) {
33
+ function InternalState(client, query, previous) {
33
34
  this.client = client;
34
35
  this.query = query;
36
+ this.asyncResolveFns = new Set();
37
+ this.optionsToIgnoreOnce = new (canUseWeakSet ? WeakSet : Set)();
35
38
  this.ssrDisabledResult = maybeDeepFreeze({
36
39
  loading: true,
37
40
  data: void 0,
@@ -46,8 +49,22 @@ var InternalState = (function () {
46
49
  });
47
50
  this.toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)();
48
51
  verifyDocumentType(query, DocumentType.Query);
52
+ var previousResult = previous && previous.result;
53
+ var previousData = previousResult && previousResult.data;
54
+ if (previousData) {
55
+ this.previousData = previousData;
56
+ }
49
57
  }
50
58
  InternalState.prototype.forceUpdate = function () {
59
+ __DEV__ && invariant.warn("Calling default no-op implementation of InternalState#forceUpdate");
60
+ };
61
+ InternalState.prototype.asyncUpdate = function () {
62
+ var _this = this;
63
+ return new Promise(function (resolve) {
64
+ _this.asyncResolveFns.add(resolve);
65
+ _this.optionsToIgnoreOnce.add(_this.watchQueryOptions);
66
+ _this.forceUpdate();
67
+ });
51
68
  };
52
69
  InternalState.prototype.useQuery = function (options) {
53
70
  var _this = this;
@@ -102,15 +119,22 @@ var InternalState = (function () {
102
119
  this.client.disableNetworkFetches,
103
120
  ]), function () { return _this.getCurrentResult(); }, function () { return _this.getCurrentResult(); });
104
121
  this.unsafeHandlePartialRefetch(result);
105
- return this.toQueryResult(result);
122
+ var queryResult = this.toQueryResult(result);
123
+ if (!queryResult.loading && this.asyncResolveFns.size) {
124
+ this.asyncResolveFns.forEach(function (resolve) { return resolve(queryResult); });
125
+ this.asyncResolveFns.clear();
126
+ }
127
+ return queryResult;
106
128
  };
107
129
  InternalState.prototype.useOptions = function (options) {
108
130
  var _a;
109
131
  var watchQueryOptions = this.createWatchQueryOptions(this.queryHookOptions = options);
110
132
  var currentWatchQueryOptions = this.watchQueryOptions;
111
- if (!equal(watchQueryOptions, currentWatchQueryOptions)) {
133
+ if (this.optionsToIgnoreOnce.has(currentWatchQueryOptions) ||
134
+ !equal(watchQueryOptions, currentWatchQueryOptions)) {
112
135
  this.watchQueryOptions = watchQueryOptions;
113
136
  if (currentWatchQueryOptions && this.observable) {
137
+ this.optionsToIgnoreOnce.delete(currentWatchQueryOptions);
114
138
  this.observable.reobserve(watchQueryOptions);
115
139
  this.previousData = ((_a = this.result) === null || _a === void 0 ? void 0 : _a.data) || this.previousData;
116
140
  this.result = void 0;
@@ -119,7 +143,8 @@ var InternalState = (function () {
119
143
  this.onCompleted = options.onCompleted || InternalState.prototype.onCompleted;
120
144
  this.onError = options.onError || InternalState.prototype.onError;
121
145
  if ((this.renderPromises || this.client.disableNetworkFetches) &&
122
- this.queryHookOptions.ssr === false) {
146
+ this.queryHookOptions.ssr === false &&
147
+ !this.queryHookOptions.skip) {
123
148
  this.result = this.ssrDisabledResult;
124
149
  }
125
150
  else if (this.queryHookOptions.skip ||
@@ -132,51 +157,38 @@ var InternalState = (function () {
132
157
  }
133
158
  };
134
159
  InternalState.prototype.createWatchQueryOptions = function (_a) {
160
+ var _b;
135
161
  if (_a === void 0) { _a = {}; }
136
162
  var skip = _a.skip, ssr = _a.ssr, onCompleted = _a.onCompleted, onError = _a.onError, displayName = _a.displayName, defaultOptions = _a.defaultOptions, otherOptions = __rest(_a, ["skip", "ssr", "onCompleted", "onError", "displayName", "defaultOptions"]);
137
- var toMerge = [];
138
- var globalDefaults = this.client.defaultOptions.watchQuery;
139
- if (globalDefaults)
140
- toMerge.push(globalDefaults);
141
- if (defaultOptions)
142
- toMerge.push(defaultOptions);
143
- var latestOptions = this.observable && this.observable.options;
144
- if (latestOptions && toMerge.length) {
145
- var defaults_1 = toMerge.reduce(mergeOptions, Object.create(null));
146
- toMerge.length = 1;
147
- toMerge[0] = defaults_1;
148
- Object.keys(defaults_1).forEach(function (defaultOptionName) {
149
- var currentOptionValue = latestOptions[defaultOptionName];
150
- if (hasOwnProperty.call(latestOptions, defaultOptionName) &&
151
- !equal(defaults_1[defaultOptionName], currentOptionValue)) {
152
- defaults_1[defaultOptionName] = defaultOptionName === "variables"
153
- ? __assign(__assign({}, defaults_1.variables), currentOptionValue) : currentOptionValue;
154
- }
155
- });
156
- }
157
- toMerge.push(otherOptions);
158
- var merged = toMerge.reduce(mergeOptions, Object.create(null));
159
- var watchQueryOptions = Object.assign(merged, { query: this.query });
163
+ var watchQueryOptions = Object.assign(otherOptions, { query: this.query });
160
164
  if (this.renderPromises &&
161
165
  (watchQueryOptions.fetchPolicy === 'network-only' ||
162
166
  watchQueryOptions.fetchPolicy === 'cache-and-network')) {
163
167
  watchQueryOptions.fetchPolicy = 'cache-first';
164
168
  }
165
- else if (!watchQueryOptions.fetchPolicy) {
166
- watchQueryOptions.fetchPolicy = 'cache-first';
169
+ if (!watchQueryOptions.variables) {
170
+ watchQueryOptions.variables = {};
167
171
  }
168
172
  if (skip) {
169
- var _b = watchQueryOptions.initialFetchPolicy, initialFetchPolicy = _b === void 0 ? watchQueryOptions.fetchPolicy : _b;
173
+ var _c = watchQueryOptions.fetchPolicy, fetchPolicy = _c === void 0 ? this.getDefaultFetchPolicy() : _c, _d = watchQueryOptions.initialFetchPolicy, initialFetchPolicy = _d === void 0 ? fetchPolicy : _d;
170
174
  Object.assign(watchQueryOptions, {
171
175
  initialFetchPolicy: initialFetchPolicy,
172
176
  fetchPolicy: 'standby',
173
177
  });
174
178
  }
175
- if (!watchQueryOptions.variables) {
176
- watchQueryOptions.variables = {};
179
+ else if (!watchQueryOptions.fetchPolicy) {
180
+ watchQueryOptions.fetchPolicy =
181
+ ((_b = this.observable) === null || _b === void 0 ? void 0 : _b.options.initialFetchPolicy) ||
182
+ this.getDefaultFetchPolicy();
177
183
  }
178
184
  return watchQueryOptions;
179
185
  };
186
+ InternalState.prototype.getDefaultFetchPolicy = function () {
187
+ var _a, _b;
188
+ return (((_a = this.queryHookOptions.defaultOptions) === null || _a === void 0 ? void 0 : _a.fetchPolicy) ||
189
+ ((_b = this.client.defaultOptions.watchQuery) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
190
+ "cache-first");
191
+ };
180
192
  InternalState.prototype.onCompleted = function (data) { };
181
193
  InternalState.prototype.onError = function (error) { };
182
194
  InternalState.prototype.useObservableQuery = function () {
@@ -184,7 +196,7 @@ var InternalState = (function () {
184
196
  this.renderPromises
185
197
  && this.renderPromises.getSSRObservable(this.watchQueryOptions)
186
198
  || this.observable
187
- || this.client.watchQuery(__assign({}, this.watchQueryOptions));
199
+ || this.client.watchQuery(mergeOptions(this.queryHookOptions.defaultOptions, this.watchQueryOptions));
188
200
  this.obsQueryFields = useMemo(function () { return ({
189
201
  refetch: obsQuery.refetch.bind(obsQuery),
190
202
  reobserve: obsQuery.reobserve.bind(obsQuery),
@@ -194,11 +206,11 @@ var InternalState = (function () {
194
206
  stopPolling: obsQuery.stopPolling.bind(obsQuery),
195
207
  subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
196
208
  }); }, [obsQuery]);
197
- if (this.renderPromises) {
209
+ var ssrAllowed = !(this.queryHookOptions.ssr === false ||
210
+ this.queryHookOptions.skip);
211
+ if (this.renderPromises && ssrAllowed) {
198
212
  this.renderPromises.registerSSRObservable(obsQuery);
199
- var ssrAllowed = !(this.queryHookOptions.ssr === false ||
200
- this.queryHookOptions.skip);
201
- if (ssrAllowed && obsQuery.getCurrentResult().loading) {
213
+ if (obsQuery.getCurrentResult().loading) {
202
214
  this.renderPromises.addObservableQueryPromise(obsQuery);
203
215
  }
204
216
  }
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,WAAW,EACX,UAAU,EACV,OAAO,EACP,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAC7E,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAE,YAAY,EAAsB,MAAM,YAAY,CAAC;AAC9D,OAAO,EAAsB,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAGL,aAAa,GAKd,MAAM,YAAY,CAAC;AAOpB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAI9E,IAAA,cAAc,GAEd,MAAM,yBAFQ,CAEP;AAEX,MAAM,UAAU,QAAQ,CAItB,KAA0D,EAC1D,OAAkE;IAAlE,wBAAA,EAAA,UAA+C,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;IAElE,OAAO,gBAAgB,CACrB,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,EAC/B,KAAK,CACN,CAAC,QAA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KxD,WAAW,CAAC,KAAK,GAAG,IAAI,WAAW,CAAC,EAAE,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;SACvE;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;IAEO,kDAA0B,GAAlC,UAAmC,MAAgC;QAMjE,IACE,MAAM,CAAC,OAAO;YACd,IAAI,CAAC,gBAAgB,CAAC,cAAc;YACpC,CAAC,MAAM,CAAC,OAAO;YACf,CAAC,CAAC,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;YACvD,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,WAAW,KAAK,YAAY,EACpD;YACA,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE;gBACpB,OAAO,EAAE,IAAI;gBACb,aAAa,EAAE,aAAa,CAAC,OAAO;aACrC,CAAC,CAAC;YACH,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;SAC3B;IACH,CAAC;IACH,oBAAC;AAAD,CAAC,AAndD,IAmdC","sourcesContent":["import {\n useCallback,\n useContext,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';\nimport { equal } from '@wry/equality';\n\nimport { mergeOptions, OperationVariables } from '../../core';\nimport { ApolloContextValue, getApolloContext } from '../context';\nimport { ApolloError } from '../../errors';\nimport {\n ApolloClient,\n ApolloQueryResult,\n NetworkStatus,\n ObservableQuery,\n DocumentNode,\n TypedDocumentNode,\n WatchQueryOptions,\n} from '../../core';\nimport {\n QueryHookOptions,\n QueryResult,\n ObservableQueryFields,\n} from '../types/types';\n\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport { useApolloClient } from './useApolloClient';\nimport { canUseWeakMap, isNonEmptyArray, maybeDeepFreeze } from '../../utilities';\n\nconst {\n prototype: {\n hasOwnProperty,\n },\n} = Object;\n\nexport function useQuery<\n TData = any,\n TVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: QueryHookOptions<TData, TVariables> = Object.create(null),\n): QueryResult<TData, TVariables> {\n return useInternalState(\n useApolloClient(options.client),\n query,\n ).useQuery(options);\n}\n\nexport function useInternalState<TData, TVariables>(\n client: ApolloClient<any>,\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n): InternalState<TData, TVariables> {\n const stateRef = useRef<InternalState<TData, TVariables>>();\n if (\n !stateRef.current ||\n client !== stateRef.current.client ||\n query !== stateRef.current.query\n ) {\n stateRef.current = new InternalState(client, query);\n }\n const state = stateRef.current;\n\n // By default, InternalState.prototype.forceUpdate is an empty function, but\n // we replace it here (before anyone has had a chance to see this state yet)\n // with a function that unconditionally forces an update, using the latest\n // setTick function. Updating this state by calling state.forceUpdate is the\n // only way we trigger React component updates (no other useState calls within\n // the InternalState class).\n const [_tick, setTick] = useState(0);\n state.forceUpdate = () => {\n setTick(tick => tick + 1);\n };\n\n return state;\n}\n\nclass InternalState<TData, TVariables> {\n constructor(\n public readonly client: ReturnType<typeof useApolloClient>,\n public readonly query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n ) {\n verifyDocumentType(query, DocumentType.Query);\n }\n\n forceUpdate() {\n // Replaced (in useInternalState) with a method that triggers an update.\n }\n\n // Methods beginning with use- should be called according to the standard\n // rules of React hooks: only at the top level of the calling function, and\n // without any dynamic conditional logic.\n useQuery(options: QueryHookOptions<TData, TVariables>) {\n // The renderPromises field gets initialized here in the useQuery method, at\n // the beginning of everything (for a given component rendering, at least),\n // so we can safely use this.renderPromises in other/later InternalState\n // methods without worrying it might be uninitialized. Even after\n // initialization, this.renderPromises is usually undefined (unless SSR is\n // happening), but that's fine as long as it has been initialized that way,\n // rather than left uninitialized.\n this.renderPromises = useContext(getApolloContext()).renderPromises;\n\n this.useOptions(options);\n\n const obsQuery = this.useObservableQuery();\n\n const result = useSyncExternalStore(\n useCallback(() => {\n if (this.renderPromises) {\n return () => {};\n }\n\n const onNext = () => {\n const previousResult = this.result;\n // We use `getCurrentResult()` instead of the onNext argument because\n // the values differ slightly. Specifically, loading results will have\n // an empty object for data instead of `undefined` for some reason.\n const result = obsQuery.getCurrentResult();\n // Make sure we're not attempting to re-render similar results\n if (\n previousResult &&\n previousResult.loading === result.loading &&\n previousResult.networkStatus === result.networkStatus &&\n equal(previousResult.data, result.data)\n ) {\n return;\n }\n\n this.setResult(result);\n };\n\n const onError = (error: Error) => {\n const last = obsQuery[\"last\"];\n subscription.unsubscribe();\n // Unfortunately, if `lastError` is set in the current\n // `observableQuery` when the subscription is re-created,\n // the subscription will immediately receive the error, which will\n // cause it to terminate again. To avoid this, we first clear\n // the last error/result from the `observableQuery` before re-starting\n // the subscription, and restore it afterwards (so the subscription\n // has a chance to stay open).\n try {\n obsQuery.resetLastResults();\n subscription = obsQuery.subscribe(onNext, onError);\n } finally {\n obsQuery[\"last\"] = last;\n }\n\n if (!hasOwnProperty.call(error, 'graphQLErrors')) {\n // The error is not a GraphQL error\n throw error;\n }\n\n const previousResult = this.result;\n if (\n !previousResult ||\n (previousResult && previousResult.loading) ||\n !equal(error, previousResult.error)\n ) {\n this.setResult({\n data: (previousResult && previousResult.data) as TData,\n error: error as ApolloError,\n loading: false,\n networkStatus: NetworkStatus.error,\n });\n }\n };\n\n let subscription = obsQuery.subscribe(onNext, onError);\n\n return () => subscription.unsubscribe();\n }, [\n // We memoize the subscribe function using useCallback and the following\n // dependency keys, because the subscribe function reference is all that\n // useSyncExternalStore uses internally as a dependency key for the\n // useEffect ultimately responsible for the subscription, so we are\n // effectively passing this dependency array to that useEffect buried\n // inside useSyncExternalStore, as desired.\n obsQuery,\n this.renderPromises,\n this.client.disableNetworkFetches,\n ]),\n\n () => this.getCurrentResult(),\n () => this.getCurrentResult(),\n );\n\n // TODO Remove this method when we remove support for options.partialRefetch.\n this.unsafeHandlePartialRefetch(result);\n\n return this.toQueryResult(result);\n }\n\n // These members (except for renderPromises) are all populated by the\n // useOptions method, which is called unconditionally at the beginning of the\n // useQuery method, so we can safely use these members in other/later methods\n // without worrying they might be uninitialized.\n private renderPromises: ApolloContextValue[\"renderPromises\"];\n private queryHookOptions: QueryHookOptions<TData, TVariables>;\n private watchQueryOptions: WatchQueryOptions<TVariables, TData>;\n\n private useOptions(\n options: QueryHookOptions<TData, TVariables>,\n ) {\n const watchQueryOptions = this.createWatchQueryOptions(\n this.queryHookOptions = options,\n );\n\n // Update this.watchQueryOptions, but only when they have changed, which\n // allows us to depend on the referential stability of\n // this.watchQueryOptions elsewhere.\n const currentWatchQueryOptions = this.watchQueryOptions;\n if (!equal(watchQueryOptions, currentWatchQueryOptions)) {\n this.watchQueryOptions = watchQueryOptions;\n if (currentWatchQueryOptions && this.observable) {\n // Though it might be tempting to postpone this reobserve call to the\n // useEffect block, we need getCurrentResult to return an appropriate\n // loading:true result synchronously (later within the same call to\n // useQuery). Since we already have this.observable here (not true for\n // the very first call to useQuery), we are not initiating any new\n // subscriptions, though it does feel less than ideal that reobserve\n // (potentially) kicks off a network request (for example, when the\n // variables have changed), which is technically a side-effect.\n this.observable.reobserve(watchQueryOptions);\n this.previousData = this.result?.data || this.previousData;\n this.result = void 0;\n }\n }\n\n // Make sure state.onCompleted and state.onError always reflect the latest\n // options.onCompleted and options.onError callbacks provided to useQuery,\n // since those functions are often recreated every time useQuery is called.\n // Like the forceUpdate method, the versions of these methods inherited from\n // InternalState.prototype are empty no-ops, but we can override them on the\n // base state object (without modifying the prototype).\n this.onCompleted = options.onCompleted || InternalState.prototype.onCompleted;\n this.onError = options.onError || InternalState.prototype.onError;\n\n if (\n (this.renderPromises || this.client.disableNetworkFetches) &&\n this.queryHookOptions.ssr === false\n ) {\n // If SSR has been explicitly disabled, and this function has been called\n // on the server side, return the default loading state.\n this.result = this.ssrDisabledResult;\n } else if (\n this.queryHookOptions.skip ||\n this.watchQueryOptions.fetchPolicy === 'standby'\n ) {\n // When skipping a query (ie. we're not querying for data but still want to\n // render children), make sure the `data` is cleared out and `loading` is\n // set to `false` (since we aren't loading anything).\n //\n // NOTE: We no longer think this is the correct behavior. Skipping should\n // not automatically set `data` to `undefined`, but instead leave the\n // previous data in place. In other words, skipping should not mandate that\n // previously received data is all of a sudden removed. Unfortunately,\n // changing this is breaking, so we'll have to wait until Apollo Client 4.0\n // to address this.\n this.result = this.skipStandbyResult;\n } else if (\n this.result === this.ssrDisabledResult ||\n this.result === this.skipStandbyResult\n ) {\n this.result = void 0;\n }\n }\n\n private ssrDisabledResult = maybeDeepFreeze({\n loading: true,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.loading,\n });\n\n private skipStandbyResult = maybeDeepFreeze({\n loading: false,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.ready,\n });\n\n // A function to massage options before passing them to ObservableQuery.\n private createWatchQueryOptions({\n skip,\n ssr,\n onCompleted,\n onError,\n displayName,\n defaultOptions,\n // The above options are useQuery-specific, so this ...otherOptions spread\n // makes otherOptions almost a WatchQueryOptions object, except for the\n // query property that we add below.\n ...otherOptions\n }: QueryHookOptions<TData, TVariables> = {}): WatchQueryOptions<TVariables, TData> {\n // We use the mergeOptions helper function (which uses compact(...) and\n // shallow-merges variables) to combine globalDefaults with any local\n // defaultOptions provided to useQuery.\n const toMerge: Partial<WatchQueryOptions<TVariables, TData>>[] = [];\n\n // Merge global client.watchQuery default options with the lowest priority.\n const globalDefaults = this.client.defaultOptions.watchQuery;\n if (globalDefaults) toMerge.push(globalDefaults);\n\n // Next, merge any defaultOptions passed directly to useQuery.\n if (defaultOptions) toMerge.push(defaultOptions);\n\n const latestOptions = this.observable && this.observable.options;\n if (latestOptions && toMerge.length) {\n // If we already have this.watchQueryOptions, those options should take\n // precedence over default options of the same name. It might be simpler\n // to do toMerge.push(this.watchQueryOptions), but that potentially\n // (re)injects unrelated/unwanted options. Passing Object.create(null) as\n // the second argument to toMerge.reduce ensures the result is a newly\n // created object, so we can safely modify it in the forEach loop below.\n const defaults = toMerge.reduce(mergeOptions, Object.create(null));\n\n // Compact the toMerge array to hold only the merged defaults. This is\n // equivalent to toMerge.splice(0, toMerge.length, defaults).\n toMerge.length = 1;\n toMerge[0] = defaults;\n\n Object.keys(defaults).forEach(\n (defaultOptionName: keyof WatchQueryOptions<TVariables, TData>) => {\n const currentOptionValue = latestOptions[defaultOptionName];\n if (\n hasOwnProperty.call(latestOptions, defaultOptionName) &&\n !equal(defaults[defaultOptionName], currentOptionValue)\n ) {\n // If you keep passing useQuery({ defaultOptions: { variables }}),\n // those default variables continue to provide their default values\n // every time, though in most cases this.watchQueryOptions.variables\n // will have a current value for every default variable name, so the\n // defaults don't matter. However, if a variable has been removed\n // from this.watchQueryOptions.variables, future useQuery calls can\n // restore its default value from defaultOptions.variables.\n defaults[defaultOptionName] = defaultOptionName === \"variables\"\n ? { ...defaults.variables, ...currentOptionValue }\n : currentOptionValue;\n }\n },\n );\n }\n\n // Give highest precedence to any non-default WatchQueryOptions passed\n // directly to useQuery.\n toMerge.push(otherOptions);\n\n const merged = toMerge.reduce(mergeOptions, Object.create(null));\n\n // This Object.assign is safe because merged is the fresh object created by\n // the Object.create(null) argument to toMerge.reduce.\n const watchQueryOptions: WatchQueryOptions<TVariables, TData> =\n Object.assign(merged, { query: this.query });\n\n if (\n this.renderPromises &&\n (\n watchQueryOptions.fetchPolicy === 'network-only' ||\n watchQueryOptions.fetchPolicy === 'cache-and-network'\n )\n ) {\n // this behavior was added to react-apollo without explanation in this PR\n // https://github.com/apollographql/react-apollo/pull/1579\n watchQueryOptions.fetchPolicy = 'cache-first';\n } else if (!watchQueryOptions.fetchPolicy) {\n // We applied all available fetchPolicy default values above (from\n // globalDefaults and defaultOptions), so, if fetchPolicy is still\n // undefined, fall back to the default default (no typo), cache-first.\n watchQueryOptions.fetchPolicy = 'cache-first';\n }\n\n if (skip) {\n const {\n // The watchQueryOptions.initialFetchPolicy field usually defaults to\n // watchQueryOptions.fetchPolicy, which has now been properly\n // defaulted/initialized. However, watchQueryOptions.initialFetchPolicy\n // can be provided explicitly instead, if more control is desired.\n initialFetchPolicy = watchQueryOptions.fetchPolicy,\n } = watchQueryOptions;\n\n // When skipping, we set watchQueryOptions.fetchPolicy initially to\n // \"standby\", but we also need/want to preserve the initial non-standby\n // fetchPolicy that would have been used if not skipping.\n Object.assign(watchQueryOptions, {\n initialFetchPolicy,\n fetchPolicy: 'standby',\n });\n }\n\n if (!watchQueryOptions.variables) {\n watchQueryOptions.variables = {} as TVariables;\n }\n\n return watchQueryOptions;\n }\n\n // Defining these methods as no-ops on the prototype allows us to call\n // state.onCompleted and/or state.onError without worrying about whether a\n // callback was provided.\n private onCompleted(data: TData) {}\n private onError(error: ApolloError) {}\n\n private observable: ObservableQuery<TData, TVariables>;\n private obsQueryFields: Omit<\n ObservableQueryFields<TData, TVariables>,\n \"variables\"\n >;\n\n private useObservableQuery() {\n // See if there is an existing observable that was used to fetch the same\n // data and if so, use it instead since it will contain the proper queryId\n // to fetch the result set. This is used during SSR.\n const obsQuery = this.observable =\n this.renderPromises\n && this.renderPromises.getSSRObservable(this.watchQueryOptions)\n || this.observable // Reuse this.observable if possible (and not SSR)\n || this.client.watchQuery({\n ...this.watchQueryOptions,\n });\n\n this.obsQueryFields = useMemo(() => ({\n refetch: obsQuery.refetch.bind(obsQuery),\n reobserve: obsQuery.reobserve.bind(obsQuery),\n fetchMore: obsQuery.fetchMore.bind(obsQuery),\n updateQuery: obsQuery.updateQuery.bind(obsQuery),\n startPolling: obsQuery.startPolling.bind(obsQuery),\n stopPolling: obsQuery.stopPolling.bind(obsQuery),\n subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),\n }), [obsQuery]);\n\n if (this.renderPromises) {\n this.renderPromises.registerSSRObservable(obsQuery);\n\n const ssrAllowed = !(\n this.queryHookOptions.ssr === false ||\n this.queryHookOptions.skip\n );\n\n if (ssrAllowed && obsQuery.getCurrentResult().loading) {\n // TODO: This is a legacy API which could probably be cleaned up\n this.renderPromises.addObservableQueryPromise(obsQuery);\n }\n }\n\n return obsQuery;\n }\n\n // These members are populated by getCurrentResult and setResult, and it's\n // okay/normal for them to be initially undefined.\n private result: undefined | ApolloQueryResult<TData>;\n private previousData: undefined | TData;\n\n private setResult(nextResult: ApolloQueryResult<TData>) {\n const previousResult = this.result;\n if (previousResult && previousResult.data) {\n this.previousData = previousResult.data;\n }\n this.result = nextResult;\n // Calling state.setResult always triggers an update, though some call sites\n // perform additional equality checks before committing to an update.\n this.forceUpdate();\n this.handleErrorOrCompleted(nextResult);\n }\n\n private handleErrorOrCompleted(result: ApolloQueryResult<TData>) {\n if (!result.loading) {\n if (result.error) {\n this.onError(result.error);\n } else if (result.data) {\n this.onCompleted(result.data);\n }\n }\n }\n\n private getCurrentResult(): ApolloQueryResult<TData> {\n // Using this.result as a cache ensures getCurrentResult continues returning\n // the same (===) result object, unless state.setResult has been called, or\n // we're doing server rendering and therefore override the result below.\n if (!this.result) {\n this.handleErrorOrCompleted(\n this.result = this.observable.getCurrentResult()\n );\n }\n return this.result;\n }\n\n // This cache allows the referential stability of this.result (as returned by\n // getCurrentResult) to translate into referential stability of the resulting\n // QueryResult object returned by toQueryResult.\n private toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)<\n ApolloQueryResult<TData>,\n QueryResult<TData, TVariables>\n >();\n\n toQueryResult(\n result: ApolloQueryResult<TData>,\n ): QueryResult<TData, TVariables> {\n let queryResult = this.toQueryResultCache.get(result);\n if (queryResult) return queryResult;\n\n const { data, partial, ...resultWithoutPartial } = result;\n this.toQueryResultCache.set(result, queryResult = {\n data, // Ensure always defined, even if result.data is missing.\n ...resultWithoutPartial,\n ...this.obsQueryFields,\n client: this.client,\n observable: this.observable,\n variables: this.observable.variables,\n called: true,\n previousData: this.previousData,\n });\n\n if (!queryResult.error && isNonEmptyArray(result.errors)) {\n // Until a set naming convention for networkError and graphQLErrors is\n // decided upon, we map errors (graphQLErrors) to the error options.\n // TODO: Is it possible for both result.error and result.errors to be\n // defined here?\n queryResult.error = new ApolloError({ graphQLErrors: result.errors });\n }\n\n return queryResult;\n }\n\n private unsafeHandlePartialRefetch(result: ApolloQueryResult<TData>) {\n // WARNING: SIDE-EFFECTS IN THE RENDER FUNCTION\n //\n // TODO: This code should be removed when the partialRefetch option is\n // removed. I was unable to get this hook to behave reasonably in certain\n // edge cases when this block was put in an effect.\n if (\n result.partial &&\n this.queryHookOptions.partialRefetch &&\n !result.loading &&\n (!result.data || Object.keys(result.data).length === 0) &&\n this.observable.options.fetchPolicy !== 'cache-only'\n ) {\n Object.assign(result, {\n loading: true,\n networkStatus: NetworkStatus.refetch,\n });\n this.observable.refetch();\n }\n }\n}\n"]}
1
+ {"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,EACL,WAAW,EACX,UAAU,EACV,OAAO,EACP,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAE,YAAY,EAA6C,MAAM,YAAY,CAAC;AACrF,OAAO,EAAsB,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAGL,aAAa,GAKd,MAAM,YAAY,CAAC;AAOpB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAI7F,IAAA,cAAc,GAEd,MAAM,yBAFQ,CAEP;AAEX,MAAM,UAAU,QAAQ,CAItB,KAA0D,EAC1D,OAAkE;IAAlE,wBAAA,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globals';\n\nimport {\n useCallback,\n useContext,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport { useSyncExternalStore } from './useSyncExternalStore';\nimport { equal } from '@wry/equality';\n\nimport { mergeOptions, OperationVariables, WatchQueryFetchPolicy } from '../../core';\nimport { ApolloContextValue, getApolloContext } from '../context';\nimport { ApolloError } from '../../errors';\nimport {\n ApolloClient,\n ApolloQueryResult,\n NetworkStatus,\n ObservableQuery,\n DocumentNode,\n TypedDocumentNode,\n WatchQueryOptions,\n} from '../../core';\nimport {\n QueryHookOptions,\n QueryResult,\n ObservableQueryFields,\n} from '../types/types';\n\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport { useApolloClient } from './useApolloClient';\nimport { canUseWeakMap, canUseWeakSet, isNonEmptyArray, maybeDeepFreeze } from '../../utilities';\n\nconst {\n prototype: {\n hasOwnProperty,\n },\n} = Object;\n\nexport function useQuery<\n TData = any,\n TVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: QueryHookOptions<TData, TVariables> = Object.create(null),\n): QueryResult<TData, TVariables> {\n return useInternalState(\n useApolloClient(options.client),\n query,\n ).useQuery(options);\n}\n\nexport function useInternalState<TData, TVariables>(\n client: ApolloClient<any>,\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n): InternalState<TData, TVariables> {\n const stateRef = useRef<InternalState<TData, TVariables>>();\n if (\n !stateRef.current ||\n client !== stateRef.current.client ||\n query !== stateRef.current.query\n ) {\n stateRef.current = new InternalState(client, query, stateRef.current);\n }\n const state = stateRef.current;\n\n // By default, InternalState.prototype.forceUpdate is an empty function, but\n // we replace it here (before anyone has had a chance to see this state yet)\n // with a function that unconditionally forces an update, using the latest\n // setTick function. Updating this state by calling state.forceUpdate is the\n // only way we trigger React component updates (no other useState calls within\n // the InternalState class).\n const [_tick, setTick] = useState(0);\n state.forceUpdate = () => {\n setTick(tick => tick + 1);\n };\n\n return state;\n}\n\nclass InternalState<TData, TVariables> {\n constructor(\n public readonly client: ReturnType<typeof useApolloClient>,\n public readonly query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n previous?: InternalState<TData, TVariables>,\n ) {\n verifyDocumentType(query, DocumentType.Query);\n\n // Reuse previousData from previous InternalState (if any) to provide\n // continuity of previousData even if/when the query or client changes.\n const previousResult = previous && previous.result;\n const previousData = previousResult && previousResult.data;\n if (previousData) {\n this.previousData = previousData;\n }\n }\n\n forceUpdate() {\n // Replaced (in useInternalState) with a method that triggers an update.\n invariant.warn(\"Calling default no-op implementation of InternalState#forceUpdate\");\n }\n\n asyncUpdate() {\n return new Promise<QueryResult<TData, TVariables>>(resolve => {\n this.asyncResolveFns.add(resolve);\n this.optionsToIgnoreOnce.add(this.watchQueryOptions);\n this.forceUpdate();\n });\n }\n\n private asyncResolveFns = new Set<\n (result: QueryResult<TData, TVariables>) => void\n >();\n\n private optionsToIgnoreOnce = new (canUseWeakSet ? WeakSet : Set)<\n WatchQueryOptions<TVariables, TData>\n >();\n\n // Methods beginning with use- should be called according to the standard\n // rules of React hooks: only at the top level of the calling function, and\n // without any dynamic conditional logic.\n useQuery(options: QueryHookOptions<TData, TVariables>) {\n // The renderPromises field gets initialized here in the useQuery method, at\n // the beginning of everything (for a given component rendering, at least),\n // so we can safely use this.renderPromises in other/later InternalState\n // methods without worrying it might be uninitialized. Even after\n // initialization, this.renderPromises is usually undefined (unless SSR is\n // happening), but that's fine as long as it has been initialized that way,\n // rather than left uninitialized.\n this.renderPromises = useContext(getApolloContext()).renderPromises;\n\n this.useOptions(options);\n\n const obsQuery = this.useObservableQuery();\n\n const result = useSyncExternalStore(\n useCallback(() => {\n if (this.renderPromises) {\n return () => {};\n }\n\n const onNext = () => {\n const previousResult = this.result;\n // We use `getCurrentResult()` instead of the onNext argument because\n // the values differ slightly. Specifically, loading results will have\n // an empty object for data instead of `undefined` for some reason.\n const result = obsQuery.getCurrentResult();\n // Make sure we're not attempting to re-render similar results\n if (\n previousResult &&\n previousResult.loading === result.loading &&\n previousResult.networkStatus === result.networkStatus &&\n equal(previousResult.data, result.data)\n ) {\n return;\n }\n\n this.setResult(result);\n };\n\n const onError = (error: Error) => {\n const last = obsQuery[\"last\"];\n subscription.unsubscribe();\n // Unfortunately, if `lastError` is set in the current\n // `observableQuery` when the subscription is re-created,\n // the subscription will immediately receive the error, which will\n // cause it to terminate again. To avoid this, we first clear\n // the last error/result from the `observableQuery` before re-starting\n // the subscription, and restore it afterwards (so the subscription\n // has a chance to stay open).\n try {\n obsQuery.resetLastResults();\n subscription = obsQuery.subscribe(onNext, onError);\n } finally {\n obsQuery[\"last\"] = last;\n }\n\n if (!hasOwnProperty.call(error, 'graphQLErrors')) {\n // The error is not a GraphQL error\n throw error;\n }\n\n const previousResult = this.result;\n if (\n !previousResult ||\n (previousResult && previousResult.loading) ||\n !equal(error, previousResult.error)\n ) {\n this.setResult({\n data: (previousResult && previousResult.data) as TData,\n error: error as ApolloError,\n loading: false,\n networkStatus: NetworkStatus.error,\n });\n }\n };\n\n let subscription = obsQuery.subscribe(onNext, onError);\n\n return () => subscription.unsubscribe();\n }, [\n // We memoize the subscribe function using useCallback and the following\n // dependency keys, because the subscribe function reference is all that\n // useSyncExternalStore uses internally as a dependency key for the\n // useEffect ultimately responsible for the subscription, so we are\n // effectively passing this dependency array to that useEffect buried\n // inside useSyncExternalStore, as desired.\n obsQuery,\n this.renderPromises,\n this.client.disableNetworkFetches,\n ]),\n\n () => this.getCurrentResult(),\n () => this.getCurrentResult(),\n );\n\n // TODO Remove this method when we remove support for options.partialRefetch.\n this.unsafeHandlePartialRefetch(result);\n\n const queryResult = this.toQueryResult(result);\n\n if (!queryResult.loading && this.asyncResolveFns.size) {\n this.asyncResolveFns.forEach(resolve => resolve(queryResult));\n this.asyncResolveFns.clear();\n }\n\n return queryResult;\n }\n\n // These members (except for renderPromises) are all populated by the\n // useOptions method, which is called unconditionally at the beginning of the\n // useQuery method, so we can safely use these members in other/later methods\n // without worrying they might be uninitialized.\n private renderPromises: ApolloContextValue[\"renderPromises\"];\n private queryHookOptions: QueryHookOptions<TData, TVariables>;\n private watchQueryOptions: WatchQueryOptions<TVariables, TData>;\n\n private useOptions(\n options: QueryHookOptions<TData, TVariables>,\n ) {\n const watchQueryOptions = this.createWatchQueryOptions(\n this.queryHookOptions = options,\n );\n\n // Update this.watchQueryOptions, but only when they have changed, which\n // allows us to depend on the referential stability of\n // this.watchQueryOptions elsewhere.\n const currentWatchQueryOptions = this.watchQueryOptions;\n\n // To force this equality test to \"fail,\" thereby reliably triggering\n // observable.reobserve, add any current WatchQueryOptions object(s) you\n // want to be ignored to this.optionsToIgnoreOnce. A similar effect could be\n // achieved by nullifying this.watchQueryOptions so the equality test\n // immediately fails because currentWatchQueryOptions is null, but this way\n // we can promise a truthy this.watchQueryOptions at all times.\n if (\n this.optionsToIgnoreOnce.has(currentWatchQueryOptions) ||\n !equal(watchQueryOptions, currentWatchQueryOptions)\n ) {\n this.watchQueryOptions = watchQueryOptions;\n\n if (currentWatchQueryOptions && this.observable) {\n // As advertised in the -Once of this.optionsToIgnoreOnce, this trick is\n // only good for one forced execution of observable.reobserve per\n // ignored WatchQueryOptions object, though it is unlikely we will ever\n // see this exact currentWatchQueryOptions object again here, since we\n // just replaced this.watchQueryOptions with watchQueryOptions.\n this.optionsToIgnoreOnce.delete(currentWatchQueryOptions);\n\n // Though it might be tempting to postpone this reobserve call to the\n // useEffect block, we need getCurrentResult to return an appropriate\n // loading:true result synchronously (later within the same call to\n // useQuery). Since we already have this.observable here (not true for\n // the very first call to useQuery), we are not initiating any new\n // subscriptions, though it does feel less than ideal that reobserve\n // (potentially) kicks off a network request (for example, when the\n // variables have changed), which is technically a side-effect.\n this.observable.reobserve(watchQueryOptions);\n\n // Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,\n // but save the current data as this.previousData, just like setResult\n // usually does.\n this.previousData = this.result?.data || this.previousData;\n this.result = void 0;\n }\n }\n\n // Make sure state.onCompleted and state.onError always reflect the latest\n // options.onCompleted and options.onError callbacks provided to useQuery,\n // since those functions are often recreated every time useQuery is called.\n // Like the forceUpdate method, the versions of these methods inherited from\n // InternalState.prototype are empty no-ops, but we can override them on the\n // base state object (without modifying the prototype).\n this.onCompleted = options.onCompleted || InternalState.prototype.onCompleted;\n this.onError = options.onError || InternalState.prototype.onError;\n\n if (\n (this.renderPromises || this.client.disableNetworkFetches) &&\n this.queryHookOptions.ssr === false &&\n !this.queryHookOptions.skip\n ) {\n // If SSR has been explicitly disabled, and this function has been called\n // on the server side, return the default loading state.\n this.result = this.ssrDisabledResult;\n } else if (\n this.queryHookOptions.skip ||\n this.watchQueryOptions.fetchPolicy === 'standby'\n ) {\n // When skipping a query (ie. we're not querying for data but still want to\n // render children), make sure the `data` is cleared out and `loading` is\n // set to `false` (since we aren't loading anything).\n //\n // NOTE: We no longer think this is the correct behavior. Skipping should\n // not automatically set `data` to `undefined`, but instead leave the\n // previous data in place. In other words, skipping should not mandate that\n // previously received data is all of a sudden removed. Unfortunately,\n // changing this is breaking, so we'll have to wait until Apollo Client 4.0\n // to address this.\n this.result = this.skipStandbyResult;\n } else if (\n this.result === this.ssrDisabledResult ||\n this.result === this.skipStandbyResult\n ) {\n this.result = void 0;\n }\n }\n\n private ssrDisabledResult = maybeDeepFreeze({\n loading: true,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.loading,\n });\n\n private skipStandbyResult = maybeDeepFreeze({\n loading: false,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.ready,\n });\n\n // A function to massage options before passing them to ObservableQuery.\n private createWatchQueryOptions({\n skip,\n ssr,\n onCompleted,\n onError,\n displayName,\n defaultOptions,\n // The above options are useQuery-specific, so this ...otherOptions spread\n // makes otherOptions almost a WatchQueryOptions object, except for the\n // query property that we add below.\n ...otherOptions\n }: QueryHookOptions<TData, TVariables> = {}): WatchQueryOptions<TVariables, TData> {\n // This Object.assign is safe because otherOptions is a fresh ...rest object\n // that did not exist until just now, so modifications are still allowed.\n const watchQueryOptions: WatchQueryOptions<TVariables, TData> =\n Object.assign(otherOptions, { query: this.query });\n\n if (\n this.renderPromises &&\n (\n watchQueryOptions.fetchPolicy === 'network-only' ||\n watchQueryOptions.fetchPolicy === 'cache-and-network'\n )\n ) {\n // this behavior was added to react-apollo without explanation in this PR\n // https://github.com/apollographql/react-apollo/pull/1579\n watchQueryOptions.fetchPolicy = 'cache-first';\n }\n\n if (!watchQueryOptions.variables) {\n watchQueryOptions.variables = {} as TVariables;\n }\n\n if (skip) {\n const {\n fetchPolicy = this.getDefaultFetchPolicy(),\n initialFetchPolicy = fetchPolicy,\n } = watchQueryOptions;\n\n // When skipping, we set watchQueryOptions.fetchPolicy initially to\n // \"standby\", but we also need/want to preserve the initial non-standby\n // fetchPolicy that would have been used if not skipping.\n Object.assign(watchQueryOptions, {\n initialFetchPolicy,\n fetchPolicy: 'standby',\n });\n } else if (!watchQueryOptions.fetchPolicy) {\n watchQueryOptions.fetchPolicy =\n this.observable?.options.initialFetchPolicy ||\n this.getDefaultFetchPolicy();\n }\n\n return watchQueryOptions;\n }\n\n getDefaultFetchPolicy(): WatchQueryFetchPolicy {\n return (\n this.queryHookOptions.defaultOptions?.fetchPolicy ||\n this.client.defaultOptions.watchQuery?.fetchPolicy ||\n \"cache-first\"\n );\n }\n\n // Defining these methods as no-ops on the prototype allows us to call\n // state.onCompleted and/or state.onError without worrying about whether a\n // callback was provided.\n private onCompleted(data: TData) {}\n private onError(error: ApolloError) {}\n\n private observable: ObservableQuery<TData, TVariables>;\n private obsQueryFields: Omit<\n ObservableQueryFields<TData, TVariables>,\n \"variables\"\n >;\n\n private useObservableQuery() {\n // See if there is an existing observable that was used to fetch the same\n // data and if so, use it instead since it will contain the proper queryId\n // to fetch the result set. This is used during SSR.\n const obsQuery = this.observable =\n this.renderPromises\n && this.renderPromises.getSSRObservable(this.watchQueryOptions)\n || this.observable // Reuse this.observable if possible (and not SSR)\n || this.client.watchQuery(mergeOptions(\n // Any options.defaultOptions passed to useQuery serve as default\n // options because we use them only here, when first creating the\n // ObservableQuery by calling client.watchQuery.\n this.queryHookOptions.defaultOptions,\n this.watchQueryOptions,\n ));\n\n this.obsQueryFields = useMemo(() => ({\n refetch: obsQuery.refetch.bind(obsQuery),\n reobserve: obsQuery.reobserve.bind(obsQuery),\n fetchMore: obsQuery.fetchMore.bind(obsQuery),\n updateQuery: obsQuery.updateQuery.bind(obsQuery),\n startPolling: obsQuery.startPolling.bind(obsQuery),\n stopPolling: obsQuery.stopPolling.bind(obsQuery),\n subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),\n }), [obsQuery]);\n\n const ssrAllowed = !(\n this.queryHookOptions.ssr === false ||\n this.queryHookOptions.skip\n );\n\n if (this.renderPromises && ssrAllowed) {\n this.renderPromises.registerSSRObservable(obsQuery);\n\n if (obsQuery.getCurrentResult().loading) {\n // TODO: This is a legacy API which could probably be cleaned up\n this.renderPromises.addObservableQueryPromise(obsQuery);\n }\n }\n\n return obsQuery;\n }\n\n // These members are populated by getCurrentResult and setResult, and it's\n // okay/normal for them to be initially undefined.\n private result: undefined | ApolloQueryResult<TData>;\n private previousData: undefined | TData;\n\n private setResult(nextResult: ApolloQueryResult<TData>) {\n const previousResult = this.result;\n if (previousResult && previousResult.data) {\n this.previousData = previousResult.data;\n }\n this.result = nextResult;\n // Calling state.setResult always triggers an update, though some call sites\n // perform additional equality checks before committing to an update.\n this.forceUpdate();\n this.handleErrorOrCompleted(nextResult);\n }\n\n private handleErrorOrCompleted(result: ApolloQueryResult<TData>) {\n if (!result.loading) {\n if (result.error) {\n this.onError(result.error);\n } else if (result.data) {\n this.onCompleted(result.data);\n }\n }\n }\n\n private getCurrentResult(): ApolloQueryResult<TData> {\n // Using this.result as a cache ensures getCurrentResult continues returning\n // the same (===) result object, unless state.setResult has been called, or\n // we're doing server rendering and therefore override the result below.\n if (!this.result) {\n this.handleErrorOrCompleted(\n this.result = this.observable.getCurrentResult()\n );\n }\n return this.result;\n }\n\n // This cache allows the referential stability of this.result (as returned by\n // getCurrentResult) to translate into referential stability of the resulting\n // QueryResult object returned by toQueryResult.\n private toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)<\n ApolloQueryResult<TData>,\n QueryResult<TData, TVariables>\n >();\n\n toQueryResult(\n result: ApolloQueryResult<TData>,\n ): QueryResult<TData, TVariables> {\n let queryResult = this.toQueryResultCache.get(result);\n if (queryResult) return queryResult;\n\n const { data, partial, ...resultWithoutPartial } = result;\n this.toQueryResultCache.set(result, queryResult = {\n data, // Ensure always defined, even if result.data is missing.\n ...resultWithoutPartial,\n ...this.obsQueryFields,\n client: this.client,\n observable: this.observable,\n variables: this.observable.variables,\n called: true,\n previousData: this.previousData,\n });\n\n if (!queryResult.error && isNonEmptyArray(result.errors)) {\n // Until a set naming convention for networkError and graphQLErrors is\n // decided upon, we map errors (graphQLErrors) to the error options.\n // TODO: Is it possible for both result.error and result.errors to be\n // defined here?\n queryResult.error = new ApolloError({ graphQLErrors: result.errors });\n }\n\n return queryResult;\n }\n\n private unsafeHandlePartialRefetch(result: ApolloQueryResult<TData>) {\n // WARNING: SIDE-EFFECTS IN THE RENDER FUNCTION\n //\n // TODO: This code should be removed when the partialRefetch option is\n // removed. I was unable to get this hook to behave reasonably in certain\n // edge cases when this block was put in an effect.\n if (\n result.partial &&\n this.queryHookOptions.partialRefetch &&\n !result.loading &&\n (!result.data || Object.keys(result.data).length === 0) &&\n this.observable.options.fetchPolicy !== 'cache-only'\n ) {\n Object.assign(result, {\n loading: true,\n networkStatus: NetworkStatus.refetch,\n });\n this.observable.refetch();\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"useSubscription.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAItE,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGhD,wBAAgB,eAAe,CAAC,KAAK,GAAG,GAAG,EAAE,UAAU,GAAG,kBAAkB,EAC1E,YAAY,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EACjE,OAAO,CAAC,EAAE,uBAAuB,CAAC,KAAK,EAAE,UAAU,CAAC,kCA4GrD"}
1
+ {"version":3,"file":"useSubscription.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAItE,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGhD,wBAAgB,eAAe,CAAC,KAAK,GAAG,GAAG,EAAE,UAAU,GAAG,kBAAkB,EAC1E,YAAY,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EACjE,OAAO,CAAC,EAAE,uBAAuB,CAAC,KAAK,EAAE,UAAU,CAAC,kCAqHrD"}
@@ -23,6 +23,12 @@ export function useSubscription(subscription, options) {
23
23
  context: options === null || options === void 0 ? void 0 : options.context,
24
24
  });
25
25
  }), observable = _b[0], setObservable = _b[1];
26
+ var canResetObservableRef = useRef(false);
27
+ useEffect(function () {
28
+ return function () {
29
+ canResetObservableRef.current = true;
30
+ };
31
+ }, []);
26
32
  var ref = useRef({ client: client, subscription: subscription, options: options });
27
33
  useEffect(function () {
28
34
  var _a, _b, _c, _d;
@@ -31,7 +37,7 @@ export function useSubscription(subscription, options) {
31
37
  shouldResubscribe = !!shouldResubscribe(options);
32
38
  }
33
39
  if (options === null || options === void 0 ? void 0 : options.skip) {
34
- if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip)) {
40
+ if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip) || canResetObservableRef.current) {
35
41
  setResult({
36
42
  loading: false,
37
43
  data: void 0,
@@ -39,13 +45,16 @@ export function useSubscription(subscription, options) {
39
45
  variables: options === null || options === void 0 ? void 0 : options.variables,
40
46
  });
41
47
  setObservable(null);
48
+ canResetObservableRef.current = false;
42
49
  }
43
50
  }
44
- else if (shouldResubscribe !== false && (client !== ref.current.client ||
45
- subscription !== ref.current.subscription ||
46
- (options === null || options === void 0 ? void 0 : options.fetchPolicy) !== ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
47
- !(options === null || options === void 0 ? void 0 : options.skip) !== !((_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.skip) ||
48
- !equal(options === null || options === void 0 ? void 0 : options.variables, (_d = ref.current.options) === null || _d === void 0 ? void 0 : _d.variables))) {
51
+ else if ((shouldResubscribe !== false &&
52
+ (client !== ref.current.client ||
53
+ subscription !== ref.current.subscription ||
54
+ (options === null || options === void 0 ? void 0 : options.fetchPolicy) !== ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
55
+ !(options === null || options === void 0 ? void 0 : options.skip) !== !((_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.skip) ||
56
+ !equal(options === null || options === void 0 ? void 0 : options.variables, (_d = ref.current.options) === null || _d === void 0 ? void 0 : _d.variables))) ||
57
+ canResetObservableRef.current) {
49
58
  setResult({
50
59
  loading: true,
51
60
  data: void 0,
@@ -58,9 +67,10 @@ export function useSubscription(subscription, options) {
58
67
  fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
59
68
  context: options === null || options === void 0 ? void 0 : options.context,
60
69
  }));
70
+ canResetObservableRef.current = false;
61
71
  }
62
72
  Object.assign(ref.current, { client: client, subscription: subscription, options: options });
63
- }, [client, subscription, options]);
73
+ }, [client, subscription, options, canResetObservableRef.current]);
64
74
  useEffect(function () {
65
75
  if (!observable) {
66
76
  return;
@@ -1 +1 @@
1
- {"version":3,"file":"useSubscription.js","sourceRoot":"","sources":["../../../src/react/hooks/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGpD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAM7D,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,MAAM,UAAU,eAAe,CAC7B,YAAiE,EACjE,OAAoD;IAEpD,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC,CAAC;IAChD,kBAAkB,CAAC,YAAY,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;IACtD,IAAA,KAAsB,QAAQ,CAA4B;QAC9D,OAAO,EAAE,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,CAAA;QACvB,KAAK,EAAE,KAAK,CAAC;QACb,IAAI,EAAE,KAAK,CAAC;QACZ,SAAS,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS;KAC9B,CAAC,EALK,MAAM,QAAA,EAAE,SAAS,QAKtB,CAAC;IAEG,IAAA,KAA8B,QAAQ,CAAC;QAC3C,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,EAAE;YACjB,OAAO,IAAI,CAAC;SACb;QAED,OAAO,MAAM,CAAC,SAAS,CAAC;YACtB,KAAK,EAAE,YAAY;YACnB,SAAS,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAA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O,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS;iBAC9B,CAAC;gBACF,SAAS,CAAC,MAAM,CAAC,CAAC;gBAElB,MAAA,MAAA,GAAG,CAAC,OAAO,CAAC,OAAO,0CAAE,kBAAkB,mDAAG;oBACxC,MAAM,QAAA;oBACN,gBAAgB,EAAE,MAAM;iBACzB,CAAC,CAAC;YACL,CAAC;YACD,KAAK,YAAC,KAAK;gBACT,SAAS,CAAC;oBACR,OAAO,EAAE,KAAK;oBACd,IAAI,EAAE,KAAK,CAAC;oBACZ,KAAK,OAAA;oBACL,SAAS,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS;iBAC9B,CAAC,CAAC;YACL,CAAC;YACD,QAAQ;;gBACN,MAAA,MAAA,GAAG,CAAC,OAAO,CAAC,OAAO,0CAAE,sBAAsB,kDAAI,CAAC;YAClD,CAAC;SACF,CAAC,CAAC;QAEH,OAAO;YACL,YAAY,CAAC,WAAW,EAAE,CAAC;QAC7B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;IAEjB,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import '../../utilities/globals';\nimport { useState, useRef, useEffect } from 'react';\nimport { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport { equal } from '@wry/equality';\n\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport {\n SubscriptionHookOptions,\n SubscriptionResult\n} from '../types/types';\nimport { OperationVariables } from '../../core';\nimport { useApolloClient } from './useApolloClient';\n\nexport function useSubscription<TData = any, TVariables = OperationVariables>(\n subscription: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: SubscriptionHookOptions<TData, TVariables>,\n) {\n const client = useApolloClient(options?.client);\n verifyDocumentType(subscription, DocumentType.Subscription);\n const [result, setResult] = useState<SubscriptionResult<TData>>({\n loading: !options?.skip,\n error: void 0,\n data: void 0,\n variables: options?.variables,\n });\n\n const [observable, setObservable] = useState(() => {\n if (options?.skip) {\n return null;\n }\n\n return client.subscribe({\n query: subscription,\n variables: options?.variables,\n fetchPolicy: options?.fetchPolicy,\n context: options?.context,\n });\n });\n\n const ref = useRef({ client, subscription, options });\n useEffect(() => {\n let shouldResubscribe = options?.shouldResubscribe;\n if (typeof shouldResubscribe === 'function') {\n shouldResubscribe = !!shouldResubscribe(options!);\n }\n\n if (options?.skip) {\n if (!options?.skip !== !ref.current.options?.skip) {\n setResult({\n loading: false,\n data: void 0,\n error: void 0,\n variables: options?.variables,\n });\n setObservable(null);\n }\n } else if (\n shouldResubscribe !== false && (\n client !== ref.current.client ||\n subscription !== ref.current.subscription ||\n options?.fetchPolicy !== ref.current.options?.fetchPolicy ||\n !options?.skip !== !ref.current.options?.skip ||\n !equal(options?.variables, ref.current.options?.variables)\n )\n ) {\n setResult({\n loading: true,\n data: void 0,\n error: void 0,\n variables: options?.variables,\n });\n setObservable(client.subscribe({\n query: subscription,\n variables: options?.variables,\n fetchPolicy: options?.fetchPolicy,\n context: options?.context,\n }));\n }\n\n Object.assign(ref.current, { client, subscription, options });\n }, [client, subscription, options]);\n\n useEffect(() => {\n if (!observable) {\n return;\n }\n\n const subscription = observable.subscribe({\n next(fetchResult) {\n const result = {\n loading: false,\n // TODO: fetchResult.data can be null but SubscriptionResult.data\n // expects TData | undefined only\n data: fetchResult.data!,\n error: void 0,\n variables: options?.variables,\n };\n setResult(result);\n\n ref.current.options?.onSubscriptionData?.({\n client,\n subscriptionData: result\n });\n },\n error(error) {\n setResult({\n loading: false,\n data: void 0,\n error,\n variables: options?.variables,\n });\n },\n complete() {\n ref.current.options?.onSubscriptionComplete?.();\n },\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [observable]);\n\n return result;\n}\n"]}
1
+ {"version":3,"file":"useSubscription.js","sourceRoot":"","sources":["../../../src/react/hooks/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE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IAAI,EAAE,WAAW,CAAC,IAAK;oBACvB,KAAK,EAAE,KAAK,CAAC;oBACb,SAAS,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS;iBAC9B,CAAC;gBACF,SAAS,CAAC,MAAM,CAAC,CAAC;gBAElB,MAAA,MAAA,GAAG,CAAC,OAAO,CAAC,OAAO,0CAAE,kBAAkB,mDAAG;oBACxC,MAAM,QAAA;oBACN,gBAAgB,EAAE,MAAM;iBACzB,CAAC,CAAC;YACL,CAAC;YACD,KAAK,YAAC,KAAK;gBACT,SAAS,CAAC;oBACR,OAAO,EAAE,KAAK;oBACd,IAAI,EAAE,KAAK,CAAC;oBACZ,KAAK,OAAA;oBACL,SAAS,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS;iBAC9B,CAAC,CAAC;YACL,CAAC;YACD,QAAQ;;gBACN,MAAA,MAAA,GAAG,CAAC,OAAO,CAAC,OAAO,0CAAE,sBAAsB,kDAAI,CAAC;YAClD,CAAC;SACF,CAAC,CAAC;QAEH,OAAO;YACL,YAAY,CAAC,WAAW,EAAE,CAAC;QAC7B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;IAEjB,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import '../../utilities/globals';\nimport { useState, useRef, useEffect } from 'react';\nimport { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport { equal } from '@wry/equality';\n\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport {\n SubscriptionHookOptions,\n SubscriptionResult\n} from '../types/types';\nimport { OperationVariables } from '../../core';\nimport { useApolloClient } from './useApolloClient';\n\nexport function useSubscription<TData = any, TVariables = OperationVariables>(\n subscription: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: SubscriptionHookOptions<TData, TVariables>,\n) {\n const client = useApolloClient(options?.client);\n verifyDocumentType(subscription, DocumentType.Subscription);\n const [result, setResult] = useState<SubscriptionResult<TData>>({\n loading: !options?.skip,\n error: void 0,\n data: void 0,\n variables: options?.variables,\n });\n\n const [observable, setObservable] = useState(() => {\n if (options?.skip) {\n return null;\n }\n\n return client.subscribe({\n query: subscription,\n variables: options?.variables,\n fetchPolicy: options?.fetchPolicy,\n context: options?.context,\n });\n });\n\n const canResetObservableRef = useRef(false);\n useEffect(() => {\n return () => {\n canResetObservableRef.current = true;\n };\n }, []);\n\n const ref = useRef({ client, subscription, options });\n useEffect(() => {\n let shouldResubscribe = options?.shouldResubscribe;\n if (typeof shouldResubscribe === 'function') {\n shouldResubscribe = !!shouldResubscribe(options!);\n }\n\n if (options?.skip) {\n if (!options?.skip !== !ref.current.options?.skip || canResetObservableRef.current) {\n setResult({\n loading: false,\n data: void 0,\n error: void 0,\n variables: options?.variables,\n });\n setObservable(null);\n canResetObservableRef.current = false;\n }\n } else if (\n (shouldResubscribe !== false &&\n (client !== ref.current.client ||\n subscription !== ref.current.subscription ||\n options?.fetchPolicy !== ref.current.options?.fetchPolicy ||\n !options?.skip !== !ref.current.options?.skip ||\n !equal(options?.variables, ref.current.options?.variables))) ||\n canResetObservableRef.current\n ) {\n setResult({\n loading: true,\n data: void 0,\n error: void 0,\n variables: options?.variables,\n });\n setObservable(client.subscribe({\n query: subscription,\n variables: options?.variables,\n fetchPolicy: options?.fetchPolicy,\n context: options?.context,\n }));\n canResetObservableRef.current = false;\n }\n\n Object.assign(ref.current, { client, subscription, options });\n }, [client, subscription, options, canResetObservableRef.current]);\n\n useEffect(() => {\n if (!observable) {\n return;\n }\n\n const subscription = observable.subscribe({\n next(fetchResult) {\n const result = {\n loading: false,\n // TODO: fetchResult.data can be null but SubscriptionResult.data\n // expects TData | undefined only\n data: fetchResult.data!,\n error: void 0,\n variables: options?.variables,\n };\n setResult(result);\n\n ref.current.options?.onSubscriptionData?.({\n client,\n subscriptionData: result\n });\n },\n error(error) {\n setResult({\n loading: false,\n data: void 0,\n error,\n variables: options?.variables,\n });\n },\n complete() {\n ref.current.options?.onSubscriptionComplete?.();\n },\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [observable]);\n\n return result;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ declare type RealUseSESHookType = typeof import("use-sync-external-store").useSyncExternalStore;
2
+ export declare const useSyncExternalStore: RealUseSESHookType;
3
+ export {};
4
+ //# sourceMappingURL=useSyncExternalStore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSyncExternalStore.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useSyncExternalStore.ts"],"names":[],"mappings":"AAOA,aAAK,kBAAkB,GAKrB,cAAc,yBAAyB,EAAE,oBAAoB,CAAC;AAYhE,eAAO,MAAM,oBAAoB,EAAE,kBAyFjC,CAAC"}