@apollo/client 3.9.5 → 3.9.7

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 (64) hide show
  1. package/CHANGELOG.md +35 -19
  2. package/apollo-client.cjs +106 -78
  3. package/apollo-client.cjs.map +1 -1
  4. package/apollo-client.min.cjs +1 -1
  5. package/core/LocalState.js +13 -13
  6. package/core/LocalState.js.map +1 -1
  7. package/core/QueryManager.js +7 -7
  8. package/core/QueryManager.js.map +1 -1
  9. package/core/core.cjs +21 -21
  10. package/core/core.cjs.map +1 -1
  11. package/core/core.cjs.native.js +21 -21
  12. package/dev/dev.cjs +1 -1
  13. package/dev/dev.cjs.map +1 -1
  14. package/dev/dev.cjs.native.js +1 -1
  15. package/link/http/http.cjs +11 -8
  16. package/link/http/http.cjs.map +1 -1
  17. package/link/http/http.cjs.native.js +11 -8
  18. package/link/http/parseAndCheckHttpResponse.js +11 -8
  19. package/link/http/parseAndCheckHttpResponse.js.map +1 -1
  20. package/package.json +22 -22
  21. package/react/hooks/hooks.cjs +67 -44
  22. package/react/hooks/hooks.cjs.map +1 -1
  23. package/react/hooks/hooks.cjs.native.js +67 -44
  24. package/react/hooks/internal/index.d.ts +1 -0
  25. package/react/hooks/internal/index.js +1 -0
  26. package/react/hooks/internal/index.js.map +1 -1
  27. package/react/hooks/internal/useRenderGuard.js +1 -1
  28. package/react/hooks/internal/useRenderGuard.js.map +1 -1
  29. package/react/hooks/internal/wrapHook.d.ts +56 -0
  30. package/react/hooks/internal/wrapHook.js +43 -0
  31. package/react/hooks/internal/wrapHook.js.map +1 -0
  32. package/react/hooks/useBackgroundQuery.js +4 -0
  33. package/react/hooks/useBackgroundQuery.js.map +1 -1
  34. package/react/hooks/useFragment.js +4 -1
  35. package/react/hooks/useFragment.js.map +1 -1
  36. package/react/hooks/useQuery.js +4 -0
  37. package/react/hooks/useQuery.js.map +1 -1
  38. package/react/hooks/useReadQuery.js +4 -1
  39. package/react/hooks/useReadQuery.js.map +1 -1
  40. package/react/hooks/useSuspenseQuery.js +4 -1
  41. package/react/hooks/useSuspenseQuery.js.map +1 -1
  42. package/react/internal/cache/QueryReference.js +17 -5
  43. package/react/internal/cache/QueryReference.js.map +1 -1
  44. package/react/internal/index.d.ts +1 -0
  45. package/react/internal/index.js.map +1 -1
  46. package/react/internal/internal.cjs +7 -5
  47. package/react/internal/internal.cjs.map +1 -1
  48. package/react/internal/internal.cjs.native.js +7 -5
  49. package/testing/internal/ObservableStream.js +3 -3
  50. package/testing/internal/ObservableStream.js.map +1 -1
  51. package/testing/internal/profile/profile.js +6 -6
  52. package/testing/internal/profile/profile.js.map +1 -1
  53. package/testing/internal/scenarios/index.js +1 -1
  54. package/testing/internal/scenarios/index.js.map +1 -1
  55. package/utilities/globals/globals.cjs +1 -1
  56. package/utilities/globals/globals.cjs.map +1 -1
  57. package/utilities/globals/globals.cjs.native.js +1 -1
  58. package/utilities/subscriptions/relay/relay.cjs +11 -8
  59. package/utilities/subscriptions/relay/relay.cjs.map +1 -1
  60. package/utilities/subscriptions/relay/relay.cjs.native.js +11 -8
  61. package/utilities/subscriptions/urql/urql.cjs +11 -8
  62. package/utilities/subscriptions/urql/urql.cjs.map +1 -1
  63. package/utilities/subscriptions/urql/urql.cjs.native.js +11 -8
  64. package/version.js +1 -1
@@ -84,9 +84,64 @@ function checkIfSnapshotChanged(_a) {
84
84
  }
85
85
  }
86
86
 
87
+ function useDeepMemo(memoFn, deps) {
88
+ var ref = React__namespace.useRef();
89
+ if (!ref.current || !equality.equal(ref.current.deps, deps)) {
90
+ ref.current = { value: memoFn(), deps: deps };
91
+ }
92
+ return ref.current.value;
93
+ }
94
+
95
+ function getRenderDispatcher() {
96
+ var _a, _b;
97
+ return (_b = (_a = React__namespace.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _a === void 0 ? void 0 : _a.ReactCurrentDispatcher) === null || _b === void 0 ? void 0 : _b.current;
98
+ }
99
+ var RenderDispatcher = null;
100
+ function useRenderGuard() {
101
+ RenderDispatcher = getRenderDispatcher();
102
+ return React__namespace.useCallback(function () {
103
+ return (RenderDispatcher != null && RenderDispatcher === getRenderDispatcher());
104
+ }, []);
105
+ }
106
+
107
+ var INIT = {};
108
+ function useLazyRef(getInitialValue) {
109
+ var ref = React__namespace.useRef(INIT);
110
+ if (ref.current === INIT) {
111
+ ref.current = getInitialValue();
112
+ }
113
+ return ref;
114
+ }
115
+
116
+ var useKey = "use";
117
+ var realHook = React__namespace[useKey];
118
+ var __use = realHook ||
119
+ function __use(promise) {
120
+ var statefulPromise = utilities.wrapPromiseWithState(promise);
121
+ switch (statefulPromise.status) {
122
+ case "pending":
123
+ throw statefulPromise;
124
+ case "rejected":
125
+ throw statefulPromise.reason;
126
+ case "fulfilled":
127
+ return statefulPromise.value;
128
+ }
129
+ };
130
+
131
+ var wrapperSymbol = Symbol.for("apollo.hook.wrappers");
132
+ function wrapHook(hookName, useHook, clientOrObsQuery) {
133
+ var queryManager = clientOrObsQuery["queryManager"];
134
+ var wrappers = queryManager && queryManager[wrapperSymbol];
135
+ var wrapper = wrappers && wrappers[hookName];
136
+ return wrapper ? wrapper(useHook) : useHook;
137
+ }
138
+
87
139
  var hasOwnProperty = Object.prototype.hasOwnProperty;
88
140
  function useQuery(query, options) {
89
141
  if (options === void 0) { options = Object.create(null); }
142
+ return wrapHook("useQuery", _useQuery, useApolloClient(options && options.client))(query, options);
143
+ }
144
+ function _useQuery(query, options) {
90
145
  return useInternalState(useApolloClient(options.client), query).useQuery(options);
91
146
  }
92
147
  function useInternalState(client, query) {
@@ -691,51 +746,10 @@ function useReactiveVar(rv) {
691
746
  }, [rv]), rv, rv);
692
747
  }
693
748
 
694
- function useDeepMemo(memoFn, deps) {
695
- var ref = React__namespace.useRef();
696
- if (!ref.current || !equality.equal(ref.current.deps, deps)) {
697
- ref.current = { value: memoFn(), deps: deps };
698
- }
699
- return ref.current.value;
700
- }
701
-
702
- function getRenderDispatcher() {
703
- var _a, _b;
704
- return (_b = (_a = React__namespace.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _a === void 0 ? void 0 : _a.ReactCurrentDispatcher) === null || _b === void 0 ? void 0 : _b.current;
705
- }
706
- var RenderDispatcher = null;
707
- function useRenderGuard() {
708
- RenderDispatcher = getRenderDispatcher();
709
- return React__namespace.useCallback(function () {
710
- return (RenderDispatcher !== null && RenderDispatcher === getRenderDispatcher());
711
- }, []);
712
- }
713
-
714
- var INIT = {};
715
- function useLazyRef(getInitialValue) {
716
- var ref = React__namespace.useRef(INIT);
717
- if (ref.current === INIT) {
718
- ref.current = getInitialValue();
719
- }
720
- return ref;
721
- }
722
-
723
- var useKey = "use";
724
- var realHook = React__namespace[useKey];
725
- var __use = realHook ||
726
- function __use(promise) {
727
- var statefulPromise = utilities.wrapPromiseWithState(promise);
728
- switch (statefulPromise.status) {
729
- case "pending":
730
- throw statefulPromise;
731
- case "rejected":
732
- throw statefulPromise.reason;
733
- case "fulfilled":
734
- return statefulPromise.value;
735
- }
736
- };
737
-
738
749
  function useFragment(options) {
750
+ return wrapHook("useFragment", _useFragment, useApolloClient(options.client))(options);
751
+ }
752
+ function _useFragment(options) {
739
753
  var cache = useApolloClient(options.client).cache;
740
754
  var diffOptions = useDeepMemo(function () {
741
755
  var fragment = options.fragment, fragmentName = options.fragmentName, from = options.from, _a = options.optimistic, optimistic = _a === void 0 ? true : _a, rest = tslib.__rest(options, ["fragment", "fragmentName", "from", "optimistic"]);
@@ -775,6 +789,9 @@ var skipToken = Symbol.for("apollo.skipToken");
775
789
 
776
790
  function useSuspenseQuery(query, options) {
777
791
  if (options === void 0) { options = Object.create(null); }
792
+ return wrapHook("useSuspenseQuery", _useSuspenseQuery, useApolloClient(typeof options === "object" ? options.client : undefined))(query, options);
793
+ }
794
+ function _useSuspenseQuery(query, options) {
778
795
  var client = useApolloClient(options.client);
779
796
  var suspenseCache = internal.getSuspenseCache(client);
780
797
  var watchQueryOptions = useWatchQueryOptions({
@@ -892,6 +909,9 @@ function useWatchQueryOptions(_a) {
892
909
 
893
910
  function useBackgroundQuery(query, options) {
894
911
  if (options === void 0) { options = Object.create(null); }
912
+ return wrapHook("useBackgroundQuery", _useBackgroundQuery, useApolloClient(typeof options === "object" ? options.client : undefined))(query, options);
913
+ }
914
+ function _useBackgroundQuery(query, options) {
895
915
  var client = useApolloClient(options.client);
896
916
  var suspenseCache = internal.getSuspenseCache(client);
897
917
  var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
@@ -1006,6 +1026,9 @@ function useQueryRefHandlers(queryRef) {
1006
1026
  }
1007
1027
 
1008
1028
  function useReadQuery(queryRef) {
1029
+ return wrapHook("useReadQuery", _useReadQuery, internal.unwrapQueryRef(queryRef)["observable"])(queryRef);
1030
+ }
1031
+ function _useReadQuery(queryRef) {
1009
1032
  var internalQueryRef = React__namespace.useMemo(function () { return internal.unwrapQueryRef(queryRef); }, [queryRef]);
1010
1033
  var getPromise = React__namespace.useCallback(function () { return internal.getWrappedPromise(queryRef); }, [queryRef]);
1011
1034
  if (internalQueryRef.disposed) {
@@ -3,4 +3,5 @@ export { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect.js";
3
3
  export { useRenderGuard } from "./useRenderGuard.js";
4
4
  export { useLazyRef } from "./useLazyRef.js";
5
5
  export { __use } from "./__use.js";
6
+ export { wrapHook } from "./wrapHook.js";
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -4,4 +4,5 @@ export { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect.js";
4
4
  export { useRenderGuard } from "./useRenderGuard.js";
5
5
  export { useLazyRef } from "./useLazyRef.js";
6
6
  export { __use } from "./__use.js";
7
+ export { wrapHook } from "./wrapHook.js";
7
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/react/hooks/internal/index.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC","sourcesContent":["// These hooks are used internally and are not exported publicly by the library\nexport { useDeepMemo } from \"./useDeepMemo.js\";\nexport { useIsomorphicLayoutEffect } from \"./useIsomorphicLayoutEffect.js\";\nexport { useRenderGuard } from \"./useRenderGuard.js\";\nexport { useLazyRef } from \"./useLazyRef.js\";\nexport { __use } from \"./__use.js\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/react/hooks/internal/index.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC","sourcesContent":["// These hooks are used internally and are not exported publicly by the library\nexport { useDeepMemo } from \"./useDeepMemo.js\";\nexport { useIsomorphicLayoutEffect } from \"./useIsomorphicLayoutEffect.js\";\nexport { useRenderGuard } from \"./useRenderGuard.js\";\nexport { useLazyRef } from \"./useLazyRef.js\";\nexport { __use } from \"./__use.js\";\nexport { wrapHook } from \"./wrapHook.js\";\n"]}
@@ -11,7 +11,7 @@ https://github.com/facebook/relay/blob/8651fbca19adbfbb79af7a3bc40834d105fd7747/
11
11
  export function useRenderGuard() {
12
12
  RenderDispatcher = getRenderDispatcher();
13
13
  return React.useCallback(function () {
14
- return (RenderDispatcher !== null && RenderDispatcher === getRenderDispatcher());
14
+ return (RenderDispatcher != null && RenderDispatcher === getRenderDispatcher());
15
15
  }, []);
16
16
  }
17
17
  //# sourceMappingURL=useRenderGuard.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useRenderGuard.js","sourceRoot":"","sources":["../../../../src/react/hooks/internal/useRenderGuard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,SAAS,mBAAmB;;IAC1B,OAAO,MAAA,MAAC,KAAa,CAAC,kDAAkD,0CACpE,sBAAsB,0CAAE,OAAO,CAAC;AACtC,CAAC;AAED,IAAI,gBAAgB,GAAY,IAAI,CAAC;AAErC;;;EAGE;AACF,MAAM,UAAU,cAAc;IAC5B,gBAAgB,GAAG,mBAAmB,EAAE,CAAC;IAEzC,OAAO,KAAK,CAAC,WAAW,CAAC;QACvB,OAAO,CACL,gBAAgB,KAAK,IAAI,IAAI,gBAAgB,KAAK,mBAAmB,EAAE,CACxE,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC","sourcesContent":["import * as React from \"rehackt\";\n\nfunction getRenderDispatcher() {\n return (React as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED\n ?.ReactCurrentDispatcher?.current;\n}\n\nlet RenderDispatcher: unknown = null;\n\n/*\nRelay does this too, so we hope this is safe.\nhttps://github.com/facebook/relay/blob/8651fbca19adbfbb79af7a3bc40834d105fd7747/packages/react-relay/relay-hooks/loadQuery.js#L90-L98\n*/\nexport function useRenderGuard() {\n RenderDispatcher = getRenderDispatcher();\n\n return React.useCallback(() => {\n return (\n RenderDispatcher !== null && RenderDispatcher === getRenderDispatcher()\n );\n }, []);\n}\n"]}
1
+ {"version":3,"file":"useRenderGuard.js","sourceRoot":"","sources":["../../../../src/react/hooks/internal/useRenderGuard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,SAAS,mBAAmB;;IAC1B,OAAO,MAAA,MAAC,KAAa,CAAC,kDAAkD,0CACpE,sBAAsB,0CAAE,OAAO,CAAC;AACtC,CAAC;AAED,IAAI,gBAAgB,GAAY,IAAI,CAAC;AAErC;;;EAGE;AACF,MAAM,UAAU,cAAc;IAC5B,gBAAgB,GAAG,mBAAmB,EAAE,CAAC;IAEzC,OAAO,KAAK,CAAC,WAAW,CAAC;QACvB,OAAO,CACL,gBAAgB,IAAI,IAAI,IAAI,gBAAgB,KAAK,mBAAmB,EAAE,CACvE,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC","sourcesContent":["import * as React from \"rehackt\";\n\nfunction getRenderDispatcher() {\n return (React as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED\n ?.ReactCurrentDispatcher?.current;\n}\n\nlet RenderDispatcher: unknown = null;\n\n/*\nRelay does this too, so we hope this is safe.\nhttps://github.com/facebook/relay/blob/8651fbca19adbfbb79af7a3bc40834d105fd7747/packages/react-relay/relay-hooks/loadQuery.js#L90-L98\n*/\nexport function useRenderGuard() {\n RenderDispatcher = getRenderDispatcher();\n\n return React.useCallback(() => {\n return (\n RenderDispatcher != null && RenderDispatcher === getRenderDispatcher()\n );\n }, []);\n}\n"]}
@@ -0,0 +1,56 @@
1
+ import type { useQuery, useSuspenseQuery, useBackgroundQuery, useReadQuery, useFragment } from "../index.js";
2
+ import type { ApolloClient } from "../../../core/ApolloClient.js";
3
+ import type { ObservableQuery } from "../../../core/ObservableQuery.js";
4
+ interface WrappableHooks {
5
+ useQuery: typeof useQuery;
6
+ useSuspenseQuery: typeof useSuspenseQuery;
7
+ useBackgroundQuery: typeof useBackgroundQuery;
8
+ useReadQuery: typeof useReadQuery;
9
+ useFragment: typeof useFragment;
10
+ }
11
+ /**
12
+ * @internal
13
+ * Can be used to correctly type the [Symbol.for("apollo.hook.wrappers")] property of
14
+ * `QueryManager`, to override/wrap hook functionality.
15
+ */
16
+ export type HookWrappers = {
17
+ [K in keyof WrappableHooks]?: (originalHook: WrappableHooks[K]) => WrappableHooks[K];
18
+ };
19
+ /**
20
+ * @internal
21
+ *
22
+ * Makes an Apollo Client hook "wrappable".
23
+ * That means that the Apollo Client instance can expose a "wrapper" that will be
24
+ * used to wrap the original hook implementation with additional logic.
25
+ * @example
26
+ * ```tsx
27
+ * // this is already done in `@apollo/client` for all wrappable hooks (see `WrappableHooks`)
28
+ * // following this pattern
29
+ * function useQuery() {
30
+ * return wrapHook('useQuery', _useQuery, options.client)(query, options);
31
+ * }
32
+ * function _useQuery(query, options) {
33
+ * // original implementation
34
+ * }
35
+ *
36
+ * // this is what a library like `@apollo/client-react-streaming` would do
37
+ * class ApolloClientWithStreaming extends ApolloClient {
38
+ * constructor(options) {
39
+ * super(options);
40
+ * this.queryManager[Symbol.for("apollo.hook.wrappers")] = {
41
+ * useQuery: (original) => (query, options) => {
42
+ * console.log("useQuery was called with options", options);
43
+ * return original(query, options);
44
+ * }
45
+ * }
46
+ * }
47
+ * }
48
+ *
49
+ * // this will now log the options and then call the original `useQuery`
50
+ * const client = new ApolloClientWithStreaming({ ... });
51
+ * useQuery(query, { client });
52
+ * ```
53
+ */
54
+ export declare function wrapHook<Hook extends (...args: any[]) => any>(hookName: keyof WrappableHooks, useHook: Hook, clientOrObsQuery: ObservableQuery<any> | ApolloClient<any>): Hook;
55
+ export {};
56
+ //# sourceMappingURL=wrapHook.d.ts.map
@@ -0,0 +1,43 @@
1
+ var wrapperSymbol = Symbol.for("apollo.hook.wrappers");
2
+ /**
3
+ * @internal
4
+ *
5
+ * Makes an Apollo Client hook "wrappable".
6
+ * That means that the Apollo Client instance can expose a "wrapper" that will be
7
+ * used to wrap the original hook implementation with additional logic.
8
+ * @example
9
+ * ```tsx
10
+ * // this is already done in `@apollo/client` for all wrappable hooks (see `WrappableHooks`)
11
+ * // following this pattern
12
+ * function useQuery() {
13
+ * return wrapHook('useQuery', _useQuery, options.client)(query, options);
14
+ * }
15
+ * function _useQuery(query, options) {
16
+ * // original implementation
17
+ * }
18
+ *
19
+ * // this is what a library like `@apollo/client-react-streaming` would do
20
+ * class ApolloClientWithStreaming extends ApolloClient {
21
+ * constructor(options) {
22
+ * super(options);
23
+ * this.queryManager[Symbol.for("apollo.hook.wrappers")] = {
24
+ * useQuery: (original) => (query, options) => {
25
+ * console.log("useQuery was called with options", options);
26
+ * return original(query, options);
27
+ * }
28
+ * }
29
+ * }
30
+ * }
31
+ *
32
+ * // this will now log the options and then call the original `useQuery`
33
+ * const client = new ApolloClientWithStreaming({ ... });
34
+ * useQuery(query, { client });
35
+ * ```
36
+ */
37
+ export function wrapHook(hookName, useHook, clientOrObsQuery) {
38
+ var queryManager = clientOrObsQuery["queryManager"];
39
+ var wrappers = queryManager && queryManager[wrapperSymbol];
40
+ var wrapper = wrappers && wrappers[hookName];
41
+ return wrapper ? wrapper(useHook) : useHook;
42
+ }
43
+ //# sourceMappingURL=wrapHook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrapHook.js","sourceRoot":"","sources":["../../../../src/react/hooks/internal/wrapHook.ts"],"names":[],"mappings":"AAWA,IAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AAyBzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,QAAQ,CACtB,QAA8B,EAC9B,OAAa,EACb,gBAA0D;IAE1D,IAAM,YAAY,GAChB,gBAKD,CAAC,cAAc,CAAC,CAAC;IAClB,IAAM,QAAQ,GAAG,YAAY,IAAI,YAAY,CAAC,aAAa,CAAC,CAAC;IAC7D,IAAM,OAAO,GACX,QAAQ,IAAK,QAAQ,CAAC,QAAQ,CAAS,CAAC;IAC1C,OAAO,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;AAC9C,CAAC","sourcesContent":["import type {\n useQuery,\n useSuspenseQuery,\n useBackgroundQuery,\n useReadQuery,\n useFragment,\n} from \"../index.js\";\nimport type { QueryManager } from \"../../../core/QueryManager.js\";\nimport type { ApolloClient } from \"../../../core/ApolloClient.js\";\nimport type { ObservableQuery } from \"../../../core/ObservableQuery.js\";\n\nconst wrapperSymbol = Symbol.for(\"apollo.hook.wrappers\");\n\ninterface WrappableHooks {\n useQuery: typeof useQuery;\n useSuspenseQuery: typeof useSuspenseQuery;\n useBackgroundQuery: typeof useBackgroundQuery;\n useReadQuery: typeof useReadQuery;\n useFragment: typeof useFragment;\n}\n\n/**\n * @internal\n * Can be used to correctly type the [Symbol.for(\"apollo.hook.wrappers\")] property of\n * `QueryManager`, to override/wrap hook functionality.\n */\nexport type HookWrappers = {\n [K in keyof WrappableHooks]?: (\n originalHook: WrappableHooks[K]\n ) => WrappableHooks[K];\n};\n\ninterface QueryManagerWithWrappers<T> extends QueryManager<T> {\n [wrapperSymbol]?: HookWrappers;\n}\n\n/**\n * @internal\n *\n * Makes an Apollo Client hook \"wrappable\".\n * That means that the Apollo Client instance can expose a \"wrapper\" that will be\n * used to wrap the original hook implementation with additional logic.\n * @example\n * ```tsx\n * // this is already done in `@apollo/client` for all wrappable hooks (see `WrappableHooks`)\n * // following this pattern\n * function useQuery() {\n * return wrapHook('useQuery', _useQuery, options.client)(query, options);\n * }\n * function _useQuery(query, options) {\n * // original implementation\n * }\n *\n * // this is what a library like `@apollo/client-react-streaming` would do\n * class ApolloClientWithStreaming extends ApolloClient {\n * constructor(options) {\n * super(options);\n * this.queryManager[Symbol.for(\"apollo.hook.wrappers\")] = {\n * useQuery: (original) => (query, options) => {\n * console.log(\"useQuery was called with options\", options);\n * return original(query, options);\n * }\n * }\n * }\n * }\n *\n * // this will now log the options and then call the original `useQuery`\n * const client = new ApolloClientWithStreaming({ ... });\n * useQuery(query, { client });\n * ```\n */\nexport function wrapHook<Hook extends (...args: any[]) => any>(\n hookName: keyof WrappableHooks,\n useHook: Hook,\n clientOrObsQuery: ObservableQuery<any> | ApolloClient<any>\n): Hook {\n const queryManager = (\n clientOrObsQuery as unknown as {\n // both `ApolloClient` and `ObservableQuery` have a `queryManager` property\n // but they're both `private`, so we have to cast around for a bit here.\n queryManager: QueryManagerWithWrappers<any>;\n }\n )[\"queryManager\"];\n const wrappers = queryManager && queryManager[wrapperSymbol];\n const wrapper: undefined | ((wrap: Hook) => Hook) =\n wrappers && (wrappers[hookName] as any);\n return wrapper ? wrapper(useHook) : useHook;\n}\n"]}
@@ -2,10 +2,14 @@ import { __spreadArray } from "tslib";
2
2
  import * as React from "rehackt";
3
3
  import { useApolloClient } from "./useApolloClient.js";
4
4
  import { getSuspenseCache, unwrapQueryRef, updateWrappedQueryRef, wrapQueryRef, } from "../internal/index.js";
5
+ import { wrapHook } from "./internal/index.js";
5
6
  import { useWatchQueryOptions } from "./useSuspenseQuery.js";
6
7
  import { canonicalStringify } from "../../cache/index.js";
7
8
  export function useBackgroundQuery(query, options) {
8
9
  if (options === void 0) { options = Object.create(null); }
10
+ return wrapHook("useBackgroundQuery", _useBackgroundQuery, useApolloClient(typeof options === "object" ? options.client : undefined))(query, options);
11
+ }
12
+ function _useBackgroundQuery(query, options) {
9
13
  var client = useApolloClient(options.client);
10
14
  var suspenseCache = getSuspenseCache(client);
11
15
  var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
@@ -1 +1 @@
1
- {"version":3,"file":"useBackgroundQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useBackgroundQuery.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAQjC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,YAAY,GACb,MAAM,sBAAsB,CAAC;AAI9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAw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QAAQ,CAAC,CAAC,CAAC;IAC7C,CAAC;IACD,IAAI,QAAQ,CAAC,gBAAgB,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACjD,IAAM,OAAO,GAAG,QAAQ,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;QACzD,qBAAqB,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IAED,IAAM,SAAS,GAAyC,KAAK,CAAC,WAAW,CACvE,UAAC,OAAO;QACN,IAAM,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC,OAAqC,CAAC,CAAC;QAE1E,kBAAkB,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;QAE3C,OAAO,OAAO,CAAC;IACjB,CAAC,EACD,CAAC,QAAQ,CAAC,CACX,CAAC;IAEF,IAAM,OAAO,GAAuC,KAAK,CAAC,WAAW,CACnE,UAAC,SAAS;QACR,IAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAE5C,kBAAkB,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;QAE3C,OAAO,OAAO,CAAC;IACjB,CAAC,EACD,CAAC,QAAQ,CAAC,CACX,CAAC;IAEF,OAAO;QACL,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,KAAK,CAAC;QACjD,EAAE,SAAS,WAAA,EAAE,OAAO,SAAA,EAAE;KACvB,CAAC;AACJ,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport type {\n DocumentNode,\n FetchMoreQueryOptions,\n OperationVariables,\n TypedDocumentNode,\n WatchQueryOptions,\n} from \"../../core/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport {\n getSuspenseCache,\n unwrapQueryRef,\n updateWrappedQueryRef,\n wrapQueryRef,\n} from \"../internal/index.js\";\nimport type { CacheKey, QueryReference } from \"../internal/index.js\";\nimport type { BackgroundQueryHookOptions, NoInfer } from \"../types/types.js\";\nimport { __use } from \"./internal/index.js\";\nimport { useWatchQueryOptions } from \"./useSuspenseQuery.js\";\nimport type { FetchMoreFunction, RefetchFunction } from \"./useSuspenseQuery.js\";\nimport { canonicalStringify } from \"../../cache/index.js\";\nimport type { DeepPartial } from \"../../utilities/index.js\";\nimport type { SkipToken } from \"./constants.js\";\n\nexport type UseBackgroundQueryResult<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n> = {\n fetchMore: FetchMoreFunction<TData, TVariables>;\n refetch: RefetchFunction<TData, TVariables>;\n};\n\ntype BackgroundQueryHookOptionsNoInfer<\n TData,\n TVariables extends OperationVariables,\n> = BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>;\n\nexport function useBackgroundQuery<\n TData,\n TVariables extends OperationVariables,\n TOptions extends Omit<BackgroundQueryHookOptions<TData>, \"variables\">,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & TOptions\n): [\n (\n | QueryReference<\n TOptions[\"errorPolicy\"] extends \"ignore\" | \"all\" ?\n TOptions[\"returnPartialData\"] extends true ?\n DeepPartial<TData> | undefined\n : TData | undefined\n : TOptions[\"returnPartialData\"] extends true ? DeepPartial<TData>\n : TData,\n TVariables\n >\n | (TOptions[\"skip\"] extends boolean ? undefined : never)\n ),\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n): [\n QueryReference<DeepPartial<TData> | undefined, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n errorPolicy: \"ignore\" | \"all\";\n }\n): [\n QueryReference<TData | undefined, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n skip: boolean;\n returnPartialData: true;\n }\n): [\n QueryReference<DeepPartial<TData>, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n }\n): [\n QueryReference<DeepPartial<TData>, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n skip: boolean;\n }\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryReference<TData, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: SkipToken\n): [undefined, UseBackgroundQueryResult<TData, TVariables>];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | SkipToken\n | (BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n })\n): [\n QueryReference<DeepPartial<TData>, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: SkipToken | BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | (SkipToken &\n Partial<BackgroundQueryHookOptionsNoInfer<TData, TVariables>>)\n | BackgroundQueryHookOptionsNoInfer<TData, TVariables> = Object.create(null)\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n] {\n const client = useApolloClient(options.client);\n const suspenseCache = getSuspenseCache(client);\n const watchQueryOptions = useWatchQueryOptions({ client, query, options });\n const { fetchPolicy, variables } = watchQueryOptions;\n const { queryKey = [] } = options;\n\n // This ref tracks the first time query execution is enabled to determine\n // whether to return a query ref or `undefined`. When initialized\n // in a skipped state (either via `skip: true` or `skipToken`) we return\n // `undefined` for the `queryRef` until the query has been enabled. Once\n // enabled, a query ref is always returned regardless of whether the query is\n // skipped again later.\n const didFetchResult = React.useRef(fetchPolicy !== \"standby\");\n didFetchResult.current ||= fetchPolicy !== \"standby\";\n\n const cacheKey: CacheKey = [\n query,\n canonicalStringify(variables),\n ...([] as any[]).concat(queryKey),\n ];\n\n const queryRef = suspenseCache.getQueryRef(cacheKey, () =>\n client.watchQuery(watchQueryOptions as WatchQueryOptions<any, any>)\n );\n\n const [wrappedQueryRef, setWrappedQueryRef] = React.useState(\n wrapQueryRef(queryRef)\n );\n if (unwrapQueryRef(wrappedQueryRef) !== queryRef) {\n setWrappedQueryRef(wrapQueryRef(queryRef));\n }\n if (queryRef.didChangeOptions(watchQueryOptions)) {\n const promise = queryRef.applyOptions(watchQueryOptions);\n updateWrappedQueryRef(wrappedQueryRef, promise);\n }\n\n const fetchMore: FetchMoreFunction<TData, TVariables> = React.useCallback(\n (options) => {\n const promise = queryRef.fetchMore(options as FetchMoreQueryOptions<any>);\n\n setWrappedQueryRef(wrapQueryRef(queryRef));\n\n return promise;\n },\n [queryRef]\n );\n\n const refetch: RefetchFunction<TData, TVariables> = React.useCallback(\n (variables) => {\n const promise = queryRef.refetch(variables);\n\n setWrappedQueryRef(wrapQueryRef(queryRef));\n\n return promise;\n },\n [queryRef]\n );\n\n return [\n didFetchResult.current ? wrappedQueryRef : void 0,\n { fetchMore, refetch },\n ];\n}\n"]}
1
+ {"version":3,"file":"useBackgroundQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useBackgroundQuery.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAQjC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,YAAY,GACb,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAS,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAwJ1D,MAAM,UAAU,kBAAkB,CAIhC,KAA0D,EAC1D,OAG8E;IAH9E,wBAAA,EAAA,UAG2D,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;IAK9E,OAAO,QAAQ,CACb,oBAAoB,EACpB,mBAAmB,EACnB,eAAe,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAC1E,CAAC,K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C,CAAC;IACD,IAAI,QAAQ,CAAC,gBAAgB,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACjD,IAAM,OAAO,GAAG,QAAQ,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;QACzD,qBAAqB,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IAED,IAAM,SAAS,GAAyC,KAAK,CAAC,WAAW,CACvE,UAAC,OAAO;QACN,IAAM,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC,OAAqC,CAAC,CAAC;QAE1E,kBAAkB,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;QAE3C,OAAO,OAAO,CAAC;IACjB,CAAC,EACD,CAAC,QAAQ,CAAC,CACX,CAAC;IAEF,IAAM,OAAO,GAAuC,KAAK,CAAC,WAAW,CACnE,UAAC,SAAS;QACR,IAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAE5C,kBAAkB,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;QAE3C,OAAO,OAAO,CAAC;IACjB,CAAC,EACD,CAAC,QAAQ,CAAC,CACX,CAAC;IAEF,OAAO;QACL,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,KAAK,CAAC;QACjD,EAAE,SAAS,WAAA,EAAE,OAAO,SAAA,EAAE;KACvB,CAAC;AACJ,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport type {\n DocumentNode,\n FetchMoreQueryOptions,\n OperationVariables,\n TypedDocumentNode,\n WatchQueryOptions,\n} from \"../../core/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport {\n getSuspenseCache,\n unwrapQueryRef,\n updateWrappedQueryRef,\n wrapQueryRef,\n} from \"../internal/index.js\";\nimport type { CacheKey, QueryReference } from \"../internal/index.js\";\nimport type { BackgroundQueryHookOptions, NoInfer } from \"../types/types.js\";\nimport { __use, wrapHook } from \"./internal/index.js\";\nimport { useWatchQueryOptions } from \"./useSuspenseQuery.js\";\nimport type { FetchMoreFunction, RefetchFunction } from \"./useSuspenseQuery.js\";\nimport { canonicalStringify } from \"../../cache/index.js\";\nimport type { DeepPartial } from \"../../utilities/index.js\";\nimport type { SkipToken } from \"./constants.js\";\n\nexport type UseBackgroundQueryResult<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n> = {\n fetchMore: FetchMoreFunction<TData, TVariables>;\n refetch: RefetchFunction<TData, TVariables>;\n};\n\ntype BackgroundQueryHookOptionsNoInfer<\n TData,\n TVariables extends OperationVariables,\n> = BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>;\n\nexport function useBackgroundQuery<\n TData,\n TVariables extends OperationVariables,\n TOptions extends Omit<BackgroundQueryHookOptions<TData>, \"variables\">,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & TOptions\n): [\n (\n | QueryReference<\n TOptions[\"errorPolicy\"] extends \"ignore\" | \"all\" ?\n TOptions[\"returnPartialData\"] extends true ?\n DeepPartial<TData> | undefined\n : TData | undefined\n : TOptions[\"returnPartialData\"] extends true ? DeepPartial<TData>\n : TData,\n TVariables\n >\n | (TOptions[\"skip\"] extends boolean ? undefined : never)\n ),\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n): [\n QueryReference<DeepPartial<TData> | undefined, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n errorPolicy: \"ignore\" | \"all\";\n }\n): [\n QueryReference<TData | undefined, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n skip: boolean;\n returnPartialData: true;\n }\n): [\n QueryReference<DeepPartial<TData>, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n }\n): [\n QueryReference<DeepPartial<TData>, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n skip: boolean;\n }\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryReference<TData, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: SkipToken\n): [undefined, UseBackgroundQueryResult<TData, TVariables>];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | SkipToken\n | (BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n })\n): [\n QueryReference<DeepPartial<TData>, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: SkipToken | BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | (SkipToken &\n Partial<BackgroundQueryHookOptionsNoInfer<TData, TVariables>>)\n | BackgroundQueryHookOptionsNoInfer<TData, TVariables> = Object.create(null)\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n] {\n return wrapHook(\n \"useBackgroundQuery\",\n _useBackgroundQuery,\n useApolloClient(typeof options === \"object\" ? options.client : undefined)\n )(query, options);\n}\n\nfunction _useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | (SkipToken &\n Partial<BackgroundQueryHookOptionsNoInfer<TData, TVariables>>)\n | BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n] {\n const client = useApolloClient(options.client);\n const suspenseCache = getSuspenseCache(client);\n const watchQueryOptions = useWatchQueryOptions({ client, query, options });\n const { fetchPolicy, variables } = watchQueryOptions;\n const { queryKey = [] } = options;\n\n // This ref tracks the first time query execution is enabled to determine\n // whether to return a query ref or `undefined`. When initialized\n // in a skipped state (either via `skip: true` or `skipToken`) we return\n // `undefined` for the `queryRef` until the query has been enabled. Once\n // enabled, a query ref is always returned regardless of whether the query is\n // skipped again later.\n const didFetchResult = React.useRef(fetchPolicy !== \"standby\");\n didFetchResult.current ||= fetchPolicy !== \"standby\";\n\n const cacheKey: CacheKey = [\n query,\n canonicalStringify(variables),\n ...([] as any[]).concat(queryKey),\n ];\n\n const queryRef = suspenseCache.getQueryRef(cacheKey, () =>\n client.watchQuery(watchQueryOptions as WatchQueryOptions<any, any>)\n );\n\n const [wrappedQueryRef, setWrappedQueryRef] = React.useState(\n wrapQueryRef(queryRef)\n );\n if (unwrapQueryRef(wrappedQueryRef) !== queryRef) {\n setWrappedQueryRef(wrapQueryRef(queryRef));\n }\n if (queryRef.didChangeOptions(watchQueryOptions)) {\n const promise = queryRef.applyOptions(watchQueryOptions);\n updateWrappedQueryRef(wrappedQueryRef, promise);\n }\n\n const fetchMore: FetchMoreFunction<TData, TVariables> = React.useCallback(\n (options) => {\n const promise = queryRef.fetchMore(options as FetchMoreQueryOptions<any>);\n\n setWrappedQueryRef(wrapQueryRef(queryRef));\n\n return promise;\n },\n [queryRef]\n );\n\n const refetch: RefetchFunction<TData, TVariables> = React.useCallback(\n (variables) => {\n const promise = queryRef.refetch(variables);\n\n setWrappedQueryRef(wrapQueryRef(queryRef));\n\n return promise;\n },\n [queryRef]\n );\n\n return [\n didFetchResult.current ? wrappedQueryRef : void 0,\n { fetchMore, refetch },\n ];\n}\n"]}
@@ -4,8 +4,11 @@ import { equal } from "@wry/equality";
4
4
  import { mergeDeepArray } from "../../utilities/index.js";
5
5
  import { useApolloClient } from "./useApolloClient.js";
6
6
  import { useSyncExternalStore } from "./useSyncExternalStore.js";
7
- import { useDeepMemo, useLazyRef } from "./internal/index.js";
7
+ import { useDeepMemo, useLazyRef, wrapHook } from "./internal/index.js";
8
8
  export function useFragment(options) {
9
+ return wrapHook("useFragment", _useFragment, useApolloClient(options.client))(options);
10
+ }
11
+ function _useFragment(options) {
9
12
  var cache = useApolloClient(options.client).cache;
10
13
  var diffOptions = useDeepMemo(function () {
11
14
  var fragment = options.fragment, fragmentName = options.fragmentName, from = options.from, _a = options.optimistic, optimistic = _a === void 0 ? true : _a, rest = __rest(options, ["fragment", "fragmentName", "from", "optimistic"]);
@@ -1 +1 @@
1
- {"version":3,"file":"useFragment.js","sourceRoot":"","sources":["../../../src/react/hooks/useFragment.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAGtC,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAQ1D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAGjE,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAqC9D,MAAM,UAAU,WAAW,CACzB,OAAyC;IAEjC,IAAA,KAAK,GAAK,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,MAApC,CAAqC;IAElD,IAAM,WAAW,GAAG,WAAW,CAAkC;QAE7D,IAAA,QAAQ,GAKN,OAAO,SALD,EACR,YAAY,GAIV,OAAO,aAJG,EACZ,IAAI,GAGF,OAAO,KAHL,EACJ,KAEE,OAAO,WAFQ,EAAjB,UAAU,mBAAG,IAAI,KAAA,EACd,IAAI,UACL,OAAO,EANL,kDAML,CADQ,CACG;QAEZ,6BACK,IAAI,KACP,iBAAiB,EAAE,IAAI,EACvB,EAAE,EAAE,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,EAC1D,KAAK,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC,QAAQ,EAAE,YAAY,CAAC,EACtD,UAAU,YAAA,IACV;IACJ,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,IAAM,SAAS,GAAG,UAAU,CAA2B;QACrD,OAAA,YAAY,CAAC,KAAK,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,KAAK,CAAC,OAAO,EAAb,CAAa,CAAC,CAAC,CAAC;IAC9E,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport { equal } from \"@wry/equality\";\n\nimport type { DeepPartial } from \"../../utilities/index.js\";\nimport { mergeDeepArray } from \"../../utilities/index.js\";\nimport type {\n Cache,\n Reference,\n StoreObject,\n MissingTree,\n} from \"../../cache/index.js\";\n\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\nimport type { ApolloClient, OperationVariables } from \"../../core/index.js\";\nimport type { NoInfer } from \"../types/types.js\";\nimport { useDeepMemo, useLazyRef } from \"./internal/index.js\";\n\nexport interface UseFragmentOptions<TData, TVars>\n extends Omit<\n Cache.DiffOptions<NoInfer<TData>, NoInfer<TVars>>,\n \"id\" | \"query\" | \"optimistic\" | \"previousResult\" | \"returnPartialData\"\n >,\n Omit<\n Cache.ReadFragmentOptions<TData, TVars>,\n \"id\" | \"variables\" | \"returnPartialData\"\n > {\n from: StoreObject | Reference | string;\n // Override this field to make it optional (default: true).\n optimistic?: boolean;\n /**\n * The instance of {@link ApolloClient} to use to look up the fragment.\n *\n * By default, the instance that's passed down via context is used, but you\n * can provide a different instance here.\n *\n * @docGroup 1. Operation options\n */\n client?: ApolloClient<any>;\n}\n\nexport type UseFragmentResult<TData> =\n | {\n data: TData;\n complete: true;\n missing?: never;\n }\n | {\n data: DeepPartial<TData>;\n complete: false;\n missing?: MissingTree;\n };\n\nexport function useFragment<TData = any, TVars = OperationVariables>(\n options: UseFragmentOptions<TData, TVars>\n): UseFragmentResult<TData> {\n const { cache } = useApolloClient(options.client);\n\n const diffOptions = useDeepMemo<Cache.DiffOptions<TData, TVars>>(() => {\n const {\n fragment,\n fragmentName,\n from,\n optimistic = true,\n ...rest\n } = options;\n\n return {\n ...rest,\n returnPartialData: true,\n id: typeof from === \"string\" ? from : cache.identify(from),\n query: cache[\"getFragmentDoc\"](fragment, fragmentName),\n optimistic,\n };\n }, [options]);\n\n const resultRef = useLazyRef<UseFragmentResult<TData>>(() =>\n diffToResult(cache.diff<TData>(diffOptions))\n );\n\n // Used for both getSnapshot and getServerSnapshot\n const getSnapshot = React.useCallback(() => resultRef.current, []);\n\n return useSyncExternalStore(\n React.useCallback(\n (forceUpdate) => {\n let lastTimeout = 0;\n const unsubscribe = cache.watch({\n ...diffOptions,\n immediate: true,\n callback(diff) {\n if (!equal(diff.result, resultRef.current.data)) {\n resultRef.current = diffToResult(diff);\n // If we get another update before we've re-rendered, bail out of\n // the update and try again. This ensures that the relative timing\n // between useQuery and useFragment stays roughly the same as\n // fixed in https://github.com/apollographql/apollo-client/pull/11083\n clearTimeout(lastTimeout);\n lastTimeout = setTimeout(forceUpdate) as any;\n }\n },\n });\n return () => {\n unsubscribe();\n clearTimeout(lastTimeout);\n };\n },\n [cache, diffOptions]\n ),\n getSnapshot,\n getSnapshot\n );\n}\n\nfunction diffToResult<TData>(\n diff: Cache.DiffResult<TData>\n): UseFragmentResult<TData> {\n const result = {\n data: diff.result!,\n complete: !!diff.complete,\n } as UseFragmentResult<TData>;\n\n if (diff.missing) {\n result.missing = mergeDeepArray(diff.missing.map((error) => error.missing));\n }\n\n return result;\n}\n"]}
1
+ {"version":3,"file":"useFragment.js","sourceRoot":"","sources":["../../../src/react/hooks/useFragment.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAGtC,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAQ1D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAGjE,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAqCxE,MAAM,UAAU,WAAW,CACzB,OAAy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,YAAC,IAAI;gBACX,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;oBAChD,SAAS,CAAC,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;oBACvC,iEAAiE;oBACjE,kEAAkE;oBAClE,6DAA6D;oBAC7D,qEAAqE;oBACrE,YAAY,CAAC,WAAW,CAAC,CAAC;oBAC1B,WAAW,GAAG,UAAU,CAAC,WAAW,CAAQ,CAAC;gBAC/C,CAAC;YACH,CAAC,IACD,CAAC;QACH,OAAO;YACL,WAAW,EAAE,CAAC;YACd,YAAY,CAAC,WAAW,CAAC,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EACD,CAAC,KAAK,EAAE,WAAW,CAAC,CACrB,EACD,WAAW,EACX,WAAW,CACZ,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CACnB,IAA6B;IAE7B,IAAM,MAAM,GAAG;QACb,IAAI,EAAE,IAAI,CAAC,MAAO;QAClB,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ;KACE,CAAC;IAE9B,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,MAAM,CAAC,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAC,KAAK,IAAK,OAAA,KAAK,CAAC,OAAO,EAAb,CAAa,CAAC,CAAC,CAAC;IAC9E,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport { equal } from \"@wry/equality\";\n\nimport type { DeepPartial } from \"../../utilities/index.js\";\nimport { mergeDeepArray } from \"../../utilities/index.js\";\nimport type {\n Cache,\n Reference,\n StoreObject,\n MissingTree,\n} from \"../../cache/index.js\";\n\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useSyncExternalStore } from \"./useSyncExternalStore.js\";\nimport type { ApolloClient, OperationVariables } from \"../../core/index.js\";\nimport type { NoInfer } from \"../types/types.js\";\nimport { useDeepMemo, useLazyRef, wrapHook } from \"./internal/index.js\";\n\nexport interface UseFragmentOptions<TData, TVars>\n extends Omit<\n Cache.DiffOptions<NoInfer<TData>, NoInfer<TVars>>,\n \"id\" | \"query\" | \"optimistic\" | \"previousResult\" | \"returnPartialData\"\n >,\n Omit<\n Cache.ReadFragmentOptions<TData, TVars>,\n \"id\" | \"variables\" | \"returnPartialData\"\n > {\n from: StoreObject | Reference | string;\n // Override this field to make it optional (default: true).\n optimistic?: boolean;\n /**\n * The instance of {@link ApolloClient} to use to look up the fragment.\n *\n * By default, the instance that's passed down via context is used, but you\n * can provide a different instance here.\n *\n * @docGroup 1. Operation options\n */\n client?: ApolloClient<any>;\n}\n\nexport type UseFragmentResult<TData> =\n | {\n data: TData;\n complete: true;\n missing?: never;\n }\n | {\n data: DeepPartial<TData>;\n complete: false;\n missing?: MissingTree;\n };\n\nexport function useFragment<TData = any, TVars = OperationVariables>(\n options: UseFragmentOptions<TData, TVars>\n): UseFragmentResult<TData> {\n return wrapHook(\n \"useFragment\",\n _useFragment,\n useApolloClient(options.client)\n )(options);\n}\n\nfunction _useFragment<TData = any, TVars = OperationVariables>(\n options: UseFragmentOptions<TData, TVars>\n): UseFragmentResult<TData> {\n const { cache } = useApolloClient(options.client);\n\n const diffOptions = useDeepMemo<Cache.DiffOptions<TData, TVars>>(() => {\n const {\n fragment,\n fragmentName,\n from,\n optimistic = true,\n ...rest\n } = options;\n\n return {\n ...rest,\n returnPartialData: true,\n id: typeof from === \"string\" ? from : cache.identify(from),\n query: cache[\"getFragmentDoc\"](fragment, fragmentName),\n optimistic,\n };\n }, [options]);\n\n const resultRef = useLazyRef<UseFragmentResult<TData>>(() =>\n diffToResult(cache.diff<TData>(diffOptions))\n );\n\n // Used for both getSnapshot and getServerSnapshot\n const getSnapshot = React.useCallback(() => resultRef.current, []);\n\n return useSyncExternalStore(\n React.useCallback(\n (forceUpdate) => {\n let lastTimeout = 0;\n const unsubscribe = cache.watch({\n ...diffOptions,\n immediate: true,\n callback(diff) {\n if (!equal(diff.result, resultRef.current.data)) {\n resultRef.current = diffToResult(diff);\n // If we get another update before we've re-rendered, bail out of\n // the update and try again. This ensures that the relative timing\n // between useQuery and useFragment stays roughly the same as\n // fixed in https://github.com/apollographql/apollo-client/pull/11083\n clearTimeout(lastTimeout);\n lastTimeout = setTimeout(forceUpdate) as any;\n }\n },\n });\n return () => {\n unsubscribe();\n clearTimeout(lastTimeout);\n };\n },\n [cache, diffOptions]\n ),\n getSnapshot,\n getSnapshot\n );\n}\n\nfunction diffToResult<TData>(\n diff: Cache.DiffResult<TData>\n): UseFragmentResult<TData> {\n const result = {\n data: diff.result!,\n complete: !!diff.complete,\n } as UseFragmentResult<TData>;\n\n if (diff.missing) {\n result.missing = mergeDeepArray(diff.missing.map((error) => error.missing));\n }\n\n return result;\n}\n"]}
@@ -10,6 +10,7 @@ import { NetworkStatus } from "../../core/index.js";
10
10
  import { DocumentType, verifyDocumentType } from "../parser/index.js";
11
11
  import { useApolloClient } from "./useApolloClient.js";
12
12
  import { canUseWeakMap, compact, isNonEmptyArray, maybeDeepFreeze, } from "../../utilities/index.js";
13
+ import { wrapHook } from "./internal/index.js";
13
14
  var hasOwnProperty = Object.prototype.hasOwnProperty;
14
15
  /**
15
16
  * A hook for executing queries in an Apollo application.
@@ -47,6 +48,9 @@ var hasOwnProperty = Object.prototype.hasOwnProperty;
47
48
  */
48
49
  export function useQuery(query, options) {
49
50
  if (options === void 0) { options = Object.create(null); }
51
+ return wrapHook("useQuery", _useQuery, useApolloClient(options && options.client))(query, options);
52
+ }
53
+ function _useQuery(query, options) {
50
54
  return useInternalState(useApolloClient(options.client), query).useQuery(options);
51
55
  }
52
56
  export function useInternalState(client, query) {