@apollo/client 4.0.0-alpha.3 → 4.0.0-alpha.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.
- package/.changeset/eighty-squids-fix.md +15 -0
- package/.changeset/eleven-kangaroos-jump.md +5 -0
- package/.changeset/forty-shrimps-fry.md +5 -0
- package/.changeset/funny-jeans-invent.md +16 -0
- package/.changeset/grumpy-vans-type.md +5 -0
- package/.changeset/khaki-spies-work.md +11 -0
- package/.changeset/light-apes-rescue.md +5 -0
- package/.changeset/little-spoons-kick.md +7 -0
- package/.changeset/loud-cows-raise.md +7 -0
- package/.changeset/many-buses-allow.md +5 -0
- package/.changeset/odd-lemons-relax.md +5 -0
- package/.changeset/poor-eels-punch.md +5 -0
- package/.changeset/pre.json +18 -0
- package/.changeset/real-teachers-peel.md +5 -0
- package/.changeset/slimy-chicken-melt.md +5 -0
- package/.changeset/thick-books-grin.md +5 -0
- package/.changeset/tough-rockets-allow.md +5 -0
- package/.changeset/tricky-tables-shave.md +5 -0
- package/.changeset/warm-ties-sit.md +7 -0
- package/CHANGELOG.md +84 -0
- package/__cjs/cache/core/cache.cjs +1 -1
- package/__cjs/cache/inmemory/entityStore.cjs +3 -3
- package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
- package/__cjs/cache/inmemory/policies.cjs +4 -4
- package/__cjs/cache/inmemory/readFromStore.cjs +2 -2
- package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
- package/__cjs/core/ApolloClient.cjs +25 -16
- package/__cjs/core/ApolloClient.cjs.map +1 -1
- package/__cjs/core/ApolloClient.d.cts +17 -5
- package/__cjs/core/LocalState.cjs +2 -2
- package/__cjs/core/ObservableQuery.cjs +44 -22
- package/__cjs/core/ObservableQuery.cjs.map +1 -1
- package/__cjs/core/ObservableQuery.d.cts +6 -3
- package/__cjs/core/QueryManager.cjs +72 -35
- package/__cjs/core/QueryManager.cjs.map +1 -1
- package/__cjs/core/QueryManager.d.cts +14 -3
- package/__cjs/core/types.d.cts +32 -0
- package/__cjs/core/watchQueryOptions.d.cts +18 -25
- package/__cjs/invariantErrorCodes.cjs +57 -77
- package/__cjs/link/core/ApolloLink.cjs +2 -2
- package/__cjs/link/http/checkFetcher.cjs +1 -1
- package/__cjs/link/http/createHttpLink.cjs +1 -1
- package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
- package/__cjs/link/http/serializeFetchParameter.cjs +1 -1
- package/__cjs/link/persisted-queries/index.cjs +2 -2
- package/__cjs/link/utils/validateOperation.cjs +1 -1
- package/__cjs/masking/maskDefinition.cjs +2 -2
- package/__cjs/masking/maskFragment.cjs +2 -2
- package/__cjs/masking/maskOperation.cjs +1 -1
- package/__cjs/masking/utils.cjs +1 -1
- package/__cjs/react/context/ApolloConsumer.cjs +1 -1
- package/__cjs/react/context/ApolloContext.cjs +1 -1
- package/__cjs/react/context/ApolloContext.cjs.map +1 -1
- package/__cjs/react/context/ApolloContext.d.cts +2 -2
- package/__cjs/react/context/ApolloProvider.cjs +1 -1
- package/__cjs/react/hooks/index.cjs +13 -8
- package/__cjs/react/hooks/index.cjs.map +1 -1
- package/__cjs/react/hooks/index.d.cts +6 -12
- package/__cjs/react/hooks/internal/wrapHook.cjs +26 -5
- package/__cjs/react/hooks/internal/wrapHook.cjs.map +1 -1
- package/__cjs/react/hooks/internal/wrapHook.d.cts +3 -1
- package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useBackgroundQuery.d.cts +146 -43
- package/__cjs/react/hooks/useFragment.cjs.map +1 -1
- package/__cjs/react/hooks/useFragment.d.cts +51 -26
- package/__cjs/react/hooks/useLazyQuery.cjs +1 -1
- package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useLazyQuery.d.cts +263 -262
- package/__cjs/react/hooks/useLoadableQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useLoadableQuery.d.cts +105 -32
- package/__cjs/react/hooks/useMutation.cjs +14 -19
- package/__cjs/react/hooks/useMutation.cjs.map +1 -1
- package/__cjs/react/hooks/useMutation.d.cts +198 -3
- package/__cjs/react/hooks/useQuery.cjs +82 -189
- package/__cjs/react/hooks/useQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useQuery.d.cts +283 -4
- package/__cjs/react/hooks/useQueryRefHandlers.cjs.map +1 -1
- package/__cjs/react/hooks/useQueryRefHandlers.d.cts +21 -20
- package/__cjs/react/hooks/useReadQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useReadQuery.d.cts +26 -24
- package/__cjs/react/hooks/useSubscription.cjs +8 -57
- package/__cjs/react/hooks/useSubscription.cjs.map +1 -1
- package/__cjs/react/hooks/useSubscription.d.cts +96 -9
- package/__cjs/react/hooks/useSuspenseFragment.cjs.map +1 -1
- package/__cjs/react/hooks/useSuspenseFragment.d.cts +40 -37
- package/__cjs/react/hooks/useSuspenseQuery.cjs +2 -2
- package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useSuspenseQuery.d.cts +202 -35
- package/__cjs/react/hooks/useSyncExternalStore.cjs +1 -1
- package/__cjs/react/index.cjs.map +1 -1
- package/__cjs/react/index.d.cts +2 -1
- package/__cjs/react/internal/index.cjs +3 -1
- package/__cjs/react/internal/index.cjs.map +1 -1
- package/__cjs/react/internal/index.d.cts +2 -0
- package/__cjs/react/internal/types.d.cts +46 -0
- package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
- package/__cjs/react/query-preloader/createQueryPreloader.d.cts +2 -3
- package/__cjs/react/ssr/getDataFromTree.cjs +43 -12
- package/__cjs/react/ssr/getDataFromTree.cjs.map +1 -1
- package/__cjs/react/ssr/getDataFromTree.d.cts +5 -0
- package/__cjs/react/ssr/index.cjs +1 -3
- package/__cjs/react/ssr/index.cjs.map +1 -1
- package/__cjs/react/ssr/index.d.cts +0 -1
- package/__cjs/react/ssr/useSSRQuery.cjs +60 -0
- package/__cjs/react/ssr/useSSRQuery.cjs.map +1 -0
- package/__cjs/react/ssr/useSSRQuery.d.cts +5 -0
- package/__cjs/react/types/deprecated.cjs +3 -0
- package/__cjs/react/types/deprecated.cjs.map +1 -0
- package/__cjs/react/types/deprecated.d.cts +65 -0
- package/__cjs/react/types/types.documentation.d.cts +4 -12
- package/__cjs/testing/matchers/index.cjs +4 -0
- package/__cjs/testing/matchers/index.cjs.map +1 -1
- package/__cjs/testing/matchers/toEmitStrictTyped.cjs +42 -0
- package/__cjs/testing/matchers/toEmitStrictTyped.cjs.map +1 -0
- package/__cjs/testing/matchers/toEmitStrictTyped.d.cts +7 -0
- package/__cjs/testing/matchers/toEqualLazyQueryResult.cjs.map +1 -1
- package/__cjs/testing/matchers/toEqualLazyQueryResult.d.cts +2 -2
- package/__cjs/testing/matchers/toEqualQueryResult.cjs.map +1 -1
- package/__cjs/testing/matchers/toEqualQueryResult.d.cts +2 -2
- package/__cjs/testing/matchers/toEqualStrictTyped.cjs +26 -0
- package/__cjs/testing/matchers/toEqualStrictTyped.cjs.map +1 -0
- package/__cjs/testing/matchers/toEqualStrictTyped.d.cts +3 -0
- package/__cjs/testing/matchers/utils/getSerializableProperties.cjs +23 -0
- package/__cjs/testing/matchers/utils/getSerializableProperties.cjs.map +1 -0
- package/__cjs/testing/matchers/utils/getSerializableProperties.d.cts +2 -0
- package/__cjs/version.cjs +1 -1
- package/__cjs/version.d.cts +1 -1
- package/cache/core/cache.js +1 -1
- package/cache/inmemory/entityStore.js +3 -3
- package/cache/inmemory/key-extractor.js +1 -1
- package/cache/inmemory/policies.js +4 -4
- package/cache/inmemory/readFromStore.js +2 -2
- package/cache/inmemory/writeToStore.js +4 -4
- package/core/ApolloClient.d.ts +17 -5
- package/core/ApolloClient.js +25 -16
- package/core/ApolloClient.js.map +1 -1
- package/core/LocalState.js +2 -2
- package/core/ObservableQuery.d.ts +6 -3
- package/core/ObservableQuery.js +44 -22
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryManager.d.ts +14 -3
- package/core/QueryManager.js +73 -36
- package/core/QueryManager.js.map +1 -1
- package/core/types.d.ts +32 -0
- package/core/watchQueryOptions.d.ts +18 -25
- package/invariantErrorCodes.js +57 -77
- package/link/core/ApolloLink.js +2 -2
- package/link/http/checkFetcher.js +1 -1
- package/link/http/createHttpLink.js +1 -1
- package/link/http/parseAndCheckHttpResponse.js +1 -1
- package/link/http/serializeFetchParameter.js +1 -1
- package/link/persisted-queries/index.js +2 -2
- package/link/utils/validateOperation.js +1 -1
- package/masking/maskDefinition.js +2 -2
- package/masking/maskFragment.js +2 -2
- package/masking/maskOperation.js +1 -1
- package/masking/utils.js +1 -1
- package/package.json +1 -1
- package/react/context/ApolloConsumer.js +1 -1
- package/react/context/ApolloContext.d.ts +2 -2
- package/react/context/ApolloContext.js +1 -1
- package/react/context/ApolloContext.js.map +1 -1
- package/react/context/ApolloProvider.js +1 -1
- package/react/hooks/index.d.ts +6 -12
- package/react/hooks/index.js +6 -6
- package/react/hooks/index.js.map +1 -1
- package/react/hooks/internal/wrapHook.d.ts +3 -1
- package/react/hooks/internal/wrapHook.js +24 -5
- package/react/hooks/internal/wrapHook.js.map +1 -1
- package/react/hooks/useBackgroundQuery.d.ts +146 -43
- package/react/hooks/useBackgroundQuery.js.map +1 -1
- package/react/hooks/useFragment.d.ts +51 -26
- package/react/hooks/useFragment.js.map +1 -1
- package/react/hooks/useLazyQuery.d.ts +263 -262
- package/react/hooks/useLazyQuery.js +1 -1
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useLoadableQuery.d.ts +105 -32
- package/react/hooks/useLoadableQuery.js.map +1 -1
- package/react/hooks/useMutation.d.ts +198 -3
- package/react/hooks/useMutation.js +14 -19
- package/react/hooks/useMutation.js.map +1 -1
- package/react/hooks/useQuery.d.ts +283 -4
- package/react/hooks/useQuery.js +83 -190
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useQueryRefHandlers.d.ts +21 -20
- package/react/hooks/useQueryRefHandlers.js.map +1 -1
- package/react/hooks/useReadQuery.d.ts +26 -24
- package/react/hooks/useReadQuery.js.map +1 -1
- package/react/hooks/useSubscription.d.ts +96 -9
- package/react/hooks/useSubscription.js +7 -53
- package/react/hooks/useSubscription.js.map +1 -1
- package/react/hooks/useSuspenseFragment.d.ts +40 -37
- package/react/hooks/useSuspenseFragment.js.map +1 -1
- package/react/hooks/useSuspenseQuery.d.ts +202 -35
- package/react/hooks/useSuspenseQuery.js +2 -2
- package/react/hooks/useSuspenseQuery.js.map +1 -1
- package/react/hooks/useSyncExternalStore.js +1 -1
- package/react/index.d.ts +2 -1
- package/react/index.js.map +1 -1
- package/react/internal/index.d.ts +2 -0
- package/react/internal/index.js +2 -1
- package/react/internal/index.js.map +1 -1
- package/react/internal/types.d.ts +46 -0
- package/react/query-preloader/createQueryPreloader.d.ts +2 -3
- package/react/query-preloader/createQueryPreloader.js.map +1 -1
- package/react/ssr/getDataFromTree.d.ts +5 -0
- package/react/ssr/getDataFromTree.js +43 -12
- package/react/ssr/getDataFromTree.js.map +1 -1
- package/react/ssr/index.d.ts +0 -1
- package/react/ssr/index.js +0 -1
- package/react/ssr/index.js.map +1 -1
- package/react/ssr/useSSRQuery.d.ts +5 -0
- package/react/ssr/useSSRQuery.js +56 -0
- package/react/ssr/useSSRQuery.js.map +1 -0
- package/react/types/deprecated.d.ts +65 -0
- package/react/types/deprecated.js +2 -0
- package/react/types/deprecated.js.map +1 -0
- package/react/types/types.documentation.d.ts +4 -12
- package/testing/matchers/index.js +4 -0
- package/testing/matchers/index.js.map +1 -1
- package/testing/matchers/toEmitStrictTyped.d.ts +7 -0
- package/testing/matchers/toEmitStrictTyped.js +38 -0
- package/testing/matchers/toEmitStrictTyped.js.map +1 -0
- package/testing/matchers/toEqualLazyQueryResult.d.ts +2 -2
- package/testing/matchers/toEqualLazyQueryResult.js.map +1 -1
- package/testing/matchers/toEqualQueryResult.d.ts +2 -2
- package/testing/matchers/toEqualQueryResult.js.map +1 -1
- package/testing/matchers/toEqualStrictTyped.d.ts +3 -0
- package/testing/matchers/toEqualStrictTyped.js +22 -0
- package/testing/matchers/toEqualStrictTyped.js.map +1 -0
- package/testing/matchers/utils/getSerializableProperties.d.ts +2 -0
- package/testing/matchers/utils/getSerializableProperties.js +20 -0
- package/testing/matchers/utils/getSerializableProperties.js.map +1 -0
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/__cjs/react/ssr/RenderPromises.cjs +0 -104
- package/__cjs/react/ssr/RenderPromises.cjs.map +0 -1
- package/__cjs/react/ssr/RenderPromises.d.cts +0 -22
- package/__cjs/react/types/types.d.cts +0 -661
- package/react/ssr/RenderPromises.d.ts +0 -22
- package/react/ssr/RenderPromises.js +0 -100
- package/react/ssr/RenderPromises.js.map +0 -1
- package/react/types/types.d.ts +0 -661
- /package/__cjs/react/{types → internal}/types.cjs +0 -0
- /package/__cjs/react/{types → internal}/types.cjs.map +0 -0
- /package/react/{types → internal}/types.js +0 -0
- /package/react/{types → internal}/types.js.map +0 -0
|
@@ -13,20 +13,14 @@ const tslib_1 = require("tslib");
|
|
|
13
13
|
* options
|
|
14
14
|
* watchQueryOptions
|
|
15
15
|
* makeWatchQueryOptions
|
|
16
|
-
* isSSRAllowed
|
|
17
|
-
* disableNetworkFetches
|
|
18
|
-
* renderPromises
|
|
19
|
-
* isSyncSSR
|
|
20
16
|
*/
|
|
21
17
|
/** */
|
|
22
18
|
const equality_1 = require("@wry/equality");
|
|
23
19
|
const React = tslib_1.__importStar(require("react"));
|
|
24
20
|
const rxjs_1 = require("rxjs");
|
|
25
21
|
const core_1 = require("@apollo/client/core");
|
|
26
|
-
const context_1 = require("@apollo/client/react/context");
|
|
27
22
|
const parser_1 = require("@apollo/client/react/parser");
|
|
28
23
|
const utilities_1 = require("@apollo/client/utilities");
|
|
29
|
-
const utilities_2 = require("@apollo/client/utilities");
|
|
30
24
|
const index_js_1 = require("./internal/index.cjs");
|
|
31
25
|
const useApolloClient_js_1 = require("./useApolloClient.cjs");
|
|
32
26
|
const useSyncExternalStore_js_1 = require("./useSyncExternalStore.cjs");
|
|
@@ -66,96 +60,99 @@ const lastWatchOptions = Symbol();
|
|
|
66
60
|
* @returns Query result object
|
|
67
61
|
*/
|
|
68
62
|
function useQuery(query, options = {}) {
|
|
63
|
+
"use no memo";
|
|
69
64
|
return (0, index_js_1.wrapHook)("useQuery",
|
|
70
65
|
// eslint-disable-next-line react-compiler/react-compiler
|
|
71
66
|
useQuery_, (0, useApolloClient_js_1.useApolloClient)(options && options.client))(query, options);
|
|
72
67
|
}
|
|
73
68
|
function useQuery_(query, options) {
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
69
|
+
const client = (0, useApolloClient_js_1.useApolloClient)(options.client);
|
|
70
|
+
const { skip, ssr, ...opts } = options;
|
|
71
|
+
const watchQueryOptions = (0, utilities_1.mergeOptions)(client.defaultOptions.watchQuery, { ...opts, query });
|
|
72
|
+
if (skip) {
|
|
73
|
+
// When skipping, we set watchQueryOptions.fetchPolicy initially to
|
|
74
|
+
// "standby", but we also need/want to preserve the initial non-standby
|
|
75
|
+
// fetchPolicy that would have been used if not skipping.
|
|
76
|
+
watchQueryOptions.initialFetchPolicy =
|
|
77
|
+
options.initialFetchPolicy || options.fetchPolicy;
|
|
78
|
+
watchQueryOptions.fetchPolicy = "standby";
|
|
79
|
+
}
|
|
80
|
+
function createState(previous) {
|
|
80
81
|
(0, parser_1.verifyDocumentType)(query, parser_1.DocumentType.Query);
|
|
81
|
-
const
|
|
82
|
+
const observable = client.watchQuery(watchQueryOptions);
|
|
83
|
+
return {
|
|
82
84
|
client,
|
|
83
85
|
query,
|
|
84
|
-
observable
|
|
85
|
-
// See if there is an existing observable that was used to fetch the same
|
|
86
|
-
// data and if so, use it instead since it will contain the proper queryId
|
|
87
|
-
// to fetch the result set. This is used during SSR.
|
|
88
|
-
(renderPromises &&
|
|
89
|
-
renderPromises.getSSRObservable(makeWatchQueryOptions())) ||
|
|
90
|
-
client.watchQuery(getObsQueryOptions(void 0, client, makeWatchQueryOptions())),
|
|
86
|
+
observable,
|
|
91
87
|
resultData: {
|
|
88
|
+
current: observable.getCurrentResult(),
|
|
92
89
|
// Reuse previousData from previous InternalState (if any) to provide
|
|
93
90
|
// continuity of previousData even if/when the query or client changes.
|
|
94
|
-
previousData: previous?.resultData.current
|
|
91
|
+
previousData: previous?.resultData.current.data,
|
|
95
92
|
},
|
|
96
93
|
};
|
|
97
|
-
return internalState;
|
|
98
94
|
}
|
|
99
|
-
let [
|
|
100
|
-
if (client !==
|
|
95
|
+
let [state, setState] = React.useState(createState);
|
|
96
|
+
if (client !== state.client || query !== state.query) {
|
|
101
97
|
// If the client or query have changed, we need to create a new InternalState.
|
|
102
98
|
// This will trigger a re-render with the new state, but it will also continue
|
|
103
99
|
// to run the current render function to completion.
|
|
104
100
|
// Since we sometimes trigger some side-effects in the render function, we
|
|
105
101
|
// re-assign `state` to the new state to ensure that those side-effects are
|
|
106
102
|
// triggered with the new state.
|
|
107
|
-
|
|
108
|
-
updateInternalState(newInternalState);
|
|
109
|
-
return newInternalState;
|
|
103
|
+
setState((state = createState(state)));
|
|
110
104
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
function useQueryInternals(query, options) {
|
|
114
|
-
const client = (0, useApolloClient_js_1.useApolloClient)(options.client);
|
|
115
|
-
const renderPromises = React.useContext((0, context_1.getApolloContext)()).renderPromises;
|
|
116
|
-
const isSyncSSR = !!renderPromises;
|
|
117
|
-
const disableNetworkFetches = client.disableNetworkFetches;
|
|
118
|
-
const ssrAllowed = options.ssr !== false && !options.skip;
|
|
119
|
-
const makeWatchQueryOptions = createMakeWatchQueryOptions(client, query, options, isSyncSSR);
|
|
120
|
-
const { observable, resultData } = useInternalState(client, query, renderPromises, makeWatchQueryOptions);
|
|
121
|
-
const watchQueryOptions = makeWatchQueryOptions(observable);
|
|
105
|
+
const { observable, resultData } = state;
|
|
106
|
+
useInitialFetchPolicyIfNecessary(watchQueryOptions, observable);
|
|
122
107
|
useResubscribeIfNecessary(resultData, // might get mutated during render
|
|
123
108
|
observable, // might get mutated during render
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
: void 0;
|
|
109
|
+
watchQueryOptions);
|
|
110
|
+
const ssrDisabledOverride = (0, useSyncExternalStore_js_1.useSyncExternalStore)(() => () => { }, () => void 0, () => (ssr === false ? useQuery.ssrDisabledResult : void 0));
|
|
111
|
+
const resultOverride = skip || watchQueryOptions.fetchPolicy === "standby" ?
|
|
112
|
+
// When skipping a query (ie. we're not querying for data but still want to
|
|
113
|
+
// render children), make sure the `data` is cleared out and `loading` is
|
|
114
|
+
// set to `false` (since we aren't loading anything).
|
|
115
|
+
//
|
|
116
|
+
// NOTE: We no longer think this is the correct behavior. Skipping should
|
|
117
|
+
// not automatically set `data` to `undefined`, but instead leave the
|
|
118
|
+
// previous data in place. In other words, skipping should not mandate that
|
|
119
|
+
// previously received data is all of a sudden removed. Unfortunately,
|
|
120
|
+
// changing this is breaking, so we'll have to wait until Apollo Client 4.0
|
|
121
|
+
// to address this.
|
|
122
|
+
useQuery.skipStandbyResult
|
|
123
|
+
: ssrDisabledOverride;
|
|
124
|
+
const result = useResultSubscription(observable, resultData, resultOverride);
|
|
125
|
+
const obsQueryFields = React.useMemo(() => ({
|
|
126
|
+
refetch: observable.refetch.bind(observable),
|
|
127
|
+
fetchMore: observable.fetchMore.bind(observable),
|
|
128
|
+
updateQuery: observable.updateQuery.bind(observable),
|
|
129
|
+
startPolling: observable.startPolling.bind(observable),
|
|
130
|
+
stopPolling: observable.stopPolling.bind(observable),
|
|
131
|
+
subscribeToMore: observable.subscribeToMore.bind(observable),
|
|
132
|
+
}), [observable]);
|
|
149
133
|
const previousData = resultData.previousData;
|
|
150
|
-
|
|
151
|
-
|
|
134
|
+
return React.useMemo(() => {
|
|
135
|
+
const { data, partial, ...rest } = result;
|
|
136
|
+
return {
|
|
137
|
+
data, // Ensure always defined, even if result.data is missing.
|
|
138
|
+
...rest,
|
|
139
|
+
client: client,
|
|
140
|
+
observable: observable,
|
|
141
|
+
variables: observable.variables,
|
|
142
|
+
previousData,
|
|
143
|
+
...obsQueryFields,
|
|
144
|
+
};
|
|
145
|
+
}, [result, client, observable, previousData, obsQueryFields]);
|
|
146
|
+
}
|
|
147
|
+
function useInitialFetchPolicyIfNecessary(watchQueryOptions, observable) {
|
|
148
|
+
"use no memo";
|
|
149
|
+
if (!watchQueryOptions.fetchPolicy) {
|
|
150
|
+
watchQueryOptions.fetchPolicy = observable.options.initialFetchPolicy;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function useResultSubscription(observable, resultData, resultOverride) {
|
|
154
|
+
"use no memo";
|
|
152
155
|
return (0, useSyncExternalStore_js_1.useSyncExternalStore)(React.useCallback((handleStoreChange) => {
|
|
153
|
-
// reference `disableNetworkFetches` here to ensure that the rules of hooks
|
|
154
|
-
// keep it as a dependency of this effect, even though it's not used
|
|
155
|
-
disableNetworkFetches;
|
|
156
|
-
if (isSyncSSR) {
|
|
157
|
-
return () => { };
|
|
158
|
-
}
|
|
159
156
|
const subscription = observable
|
|
160
157
|
// We use the asapScheduler here to prevent issues with trying to
|
|
161
158
|
// update in the middle of a render. `reobserve` is kicked off in the
|
|
@@ -176,7 +173,12 @@ function useObservableSubscriptionResult(resultData, observable, client, options
|
|
|
176
173
|
(0, equality_1.equal)(previousResult.error, result.error)) {
|
|
177
174
|
return;
|
|
178
175
|
}
|
|
179
|
-
|
|
176
|
+
if (previousResult && previousResult.data) {
|
|
177
|
+
// eslint-disable-next-line react-compiler/react-compiler
|
|
178
|
+
resultData.previousData = previousResult.data;
|
|
179
|
+
}
|
|
180
|
+
resultData.current = result;
|
|
181
|
+
handleStoreChange();
|
|
180
182
|
});
|
|
181
183
|
// Do the "unsubscribe" with a short delay.
|
|
182
184
|
// This way, an existing subscription can be reused without an additional
|
|
@@ -185,16 +187,7 @@ function useObservableSubscriptionResult(resultData, observable, client, options
|
|
|
185
187
|
return () => {
|
|
186
188
|
setTimeout(() => subscription.unsubscribe());
|
|
187
189
|
};
|
|
188
|
-
}, [
|
|
189
|
-
}
|
|
190
|
-
function useRegisterSSRObservable(observable, renderPromises, ssrAllowed) {
|
|
191
|
-
if (renderPromises && ssrAllowed) {
|
|
192
|
-
renderPromises.registerSSRObservable(observable);
|
|
193
|
-
if (observable.getCurrentResult().loading) {
|
|
194
|
-
// TODO: This is a legacy API which could probably be cleaned up
|
|
195
|
-
renderPromises.addObservableQueryPromise(observable);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
190
|
+
}, [observable, resultData]), () => resultOverride || resultData.current, () => resultOverride || resultData.current);
|
|
198
191
|
}
|
|
199
192
|
// this hook is not compatible with any rules of React, and there's no good way to rewrite it.
|
|
200
193
|
// it should stay a separate hook that will not be optimized by the compiler
|
|
@@ -202,7 +195,8 @@ function useResubscribeIfNecessary(
|
|
|
202
195
|
/** this hook will mutate properties on `resultData` */
|
|
203
196
|
resultData,
|
|
204
197
|
/** this hook will mutate properties on `observable` */
|
|
205
|
-
observable,
|
|
198
|
+
observable, watchQueryOptions) {
|
|
199
|
+
"use no memo";
|
|
206
200
|
if (observable[lastWatchOptions] &&
|
|
207
201
|
!(0, equality_1.equal)(observable[lastWatchOptions], watchQueryOptions)) {
|
|
208
202
|
// Though it might be tempting to postpone this reobserve call to the
|
|
@@ -213,129 +207,28 @@ observable, client, watchQueryOptions) {
|
|
|
213
207
|
// subscriptions, though it does feel less than ideal that reobserve
|
|
214
208
|
// (potentially) kicks off a network request (for example, when the
|
|
215
209
|
// variables have changed), which is technically a side-effect.
|
|
216
|
-
observable.reobserve(
|
|
210
|
+
observable.reobserve(watchQueryOptions);
|
|
217
211
|
// Make sure getCurrentResult returns a fresh ApolloQueryResult<TData>,
|
|
218
212
|
// but save the current data as this.previousData, just like setResult
|
|
219
213
|
// usually does.
|
|
220
214
|
resultData.previousData =
|
|
221
|
-
resultData.current
|
|
222
|
-
resultData.current =
|
|
215
|
+
resultData.current.data || resultData.previousData;
|
|
216
|
+
resultData.current = observable.getCurrentResult();
|
|
223
217
|
}
|
|
224
218
|
observable[lastWatchOptions] = watchQueryOptions;
|
|
225
219
|
}
|
|
226
|
-
|
|
227
|
-
* A function to massage options before passing them to ObservableQuery.
|
|
228
|
-
* This is two-step curried because we want to reuse the `make` function,
|
|
229
|
-
* but the `observable` might differ between calls to `make`.
|
|
230
|
-
*/
|
|
231
|
-
function createMakeWatchQueryOptions(client, query, { skip, ssr,
|
|
232
|
-
// The above options are useQuery-specific, so this ...otherOptions spread
|
|
233
|
-
// makes otherOptions almost a WatchQueryOptions object, except for the
|
|
234
|
-
// query property that we add below.
|
|
235
|
-
...otherOptions } = {}, isSyncSSR) {
|
|
236
|
-
return (observable) => {
|
|
237
|
-
// This Object.assign is safe because otherOptions is a fresh ...rest object
|
|
238
|
-
// that did not exist until just now, so modifications are still allowed.
|
|
239
|
-
const watchQueryOptions = Object.assign(otherOptions, { query });
|
|
240
|
-
if (isSyncSSR &&
|
|
241
|
-
(watchQueryOptions.fetchPolicy === "network-only" ||
|
|
242
|
-
watchQueryOptions.fetchPolicy === "cache-and-network")) {
|
|
243
|
-
// this behavior was added to react-apollo without explanation in this PR
|
|
244
|
-
// https://github.com/apollographql/react-apollo/pull/1579
|
|
245
|
-
watchQueryOptions.fetchPolicy = "cache-first";
|
|
246
|
-
}
|
|
247
|
-
if (!watchQueryOptions.variables) {
|
|
248
|
-
watchQueryOptions.variables = {};
|
|
249
|
-
}
|
|
250
|
-
if (skip) {
|
|
251
|
-
// When skipping, we set watchQueryOptions.fetchPolicy initially to
|
|
252
|
-
// "standby", but we also need/want to preserve the initial non-standby
|
|
253
|
-
// fetchPolicy that would have been used if not skipping.
|
|
254
|
-
watchQueryOptions.initialFetchPolicy =
|
|
255
|
-
watchQueryOptions.initialFetchPolicy ||
|
|
256
|
-
watchQueryOptions.fetchPolicy ||
|
|
257
|
-
client.defaultOptions?.watchQuery?.fetchPolicy ||
|
|
258
|
-
"cache-first";
|
|
259
|
-
watchQueryOptions.fetchPolicy = "standby";
|
|
260
|
-
}
|
|
261
|
-
else if (!watchQueryOptions.fetchPolicy) {
|
|
262
|
-
watchQueryOptions.fetchPolicy = observable?.options.initialFetchPolicy;
|
|
263
|
-
}
|
|
264
|
-
return watchQueryOptions;
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
function getObsQueryOptions(observable, client, watchQueryOptions) {
|
|
268
|
-
const toMerge = [];
|
|
269
|
-
const globalDefaults = client.defaultOptions.watchQuery;
|
|
270
|
-
if (globalDefaults)
|
|
271
|
-
toMerge.push(globalDefaults);
|
|
272
|
-
// We use compact rather than mergeOptions for this part of the merge,
|
|
273
|
-
// because we want watchQueryOptions.variables (if defined) to replace
|
|
274
|
-
// this.observable.options.variables whole. This replacement allows
|
|
275
|
-
// removing variables by removing them from the variables input to
|
|
276
|
-
// useQuery. If the variables were always merged together (rather than
|
|
277
|
-
// replaced), there would be no way to remove existing variables.
|
|
278
|
-
// However, the variables from options.defaultOptions and globalDefaults
|
|
279
|
-
// (if provided) should be merged, to ensure individual defaulted
|
|
280
|
-
// variables always have values, if not otherwise defined in
|
|
281
|
-
// observable.options or watchQueryOptions.
|
|
282
|
-
toMerge.push((0, utilities_1.compact)(observable && observable.options, watchQueryOptions));
|
|
283
|
-
return toMerge.reduce(utilities_2.mergeOptions);
|
|
284
|
-
}
|
|
285
|
-
function setResult(nextResult, resultData, observable, client, forceUpdate) {
|
|
286
|
-
const previousResult = resultData.current;
|
|
287
|
-
if (previousResult && previousResult.data) {
|
|
288
|
-
resultData.previousData = previousResult.data;
|
|
289
|
-
}
|
|
290
|
-
resultData.current = toQueryResult(nextResult, resultData.previousData, observable, client);
|
|
291
|
-
// Calling state.setResult always triggers an update, though some call sites
|
|
292
|
-
// perform additional equality checks before committing to an update.
|
|
293
|
-
forceUpdate();
|
|
294
|
-
}
|
|
295
|
-
function getCurrentResult(resultData, observable, client) {
|
|
296
|
-
// Using this.result as a cache ensures getCurrentResult continues returning
|
|
297
|
-
// the same (===) result object, unless state.setResult has been called, or
|
|
298
|
-
// we're doing server rendering and therefore override the result below.
|
|
299
|
-
if (!resultData.current) {
|
|
300
|
-
setResult(observable.getCurrentResult(), resultData, observable, client, () => { });
|
|
301
|
-
}
|
|
302
|
-
return resultData.current;
|
|
303
|
-
}
|
|
304
|
-
function toQueryResult(result, previousData, observable, client) {
|
|
305
|
-
const { data, partial, ...resultWithoutPartial } = result;
|
|
306
|
-
const queryResult = {
|
|
307
|
-
data, // Ensure always defined, even if result.data is missing.
|
|
308
|
-
...resultWithoutPartial,
|
|
309
|
-
client: client,
|
|
310
|
-
observable: observable,
|
|
311
|
-
variables: observable.variables,
|
|
312
|
-
previousData,
|
|
313
|
-
};
|
|
314
|
-
return queryResult;
|
|
315
|
-
}
|
|
316
|
-
const ssrDisabledResult = (0, utilities_1.maybeDeepFreeze)({
|
|
220
|
+
useQuery.ssrDisabledResult = (0, utilities_1.maybeDeepFreeze)({
|
|
317
221
|
loading: true,
|
|
318
222
|
data: void 0,
|
|
319
223
|
error: void 0,
|
|
320
224
|
networkStatus: core_1.NetworkStatus.loading,
|
|
321
225
|
partial: true,
|
|
322
226
|
});
|
|
323
|
-
|
|
227
|
+
useQuery.skipStandbyResult = (0, utilities_1.maybeDeepFreeze)({
|
|
324
228
|
loading: false,
|
|
325
229
|
data: void 0,
|
|
326
230
|
error: void 0,
|
|
327
231
|
networkStatus: core_1.NetworkStatus.ready,
|
|
328
232
|
partial: true,
|
|
329
233
|
});
|
|
330
|
-
function bindObservableMethods(observable) {
|
|
331
|
-
return {
|
|
332
|
-
refetch: observable.refetch.bind(observable),
|
|
333
|
-
reobserve: observable.reobserve.bind(observable),
|
|
334
|
-
fetchMore: observable.fetchMore.bind(observable),
|
|
335
|
-
updateQuery: observable.updateQuery.bind(observable),
|
|
336
|
-
startPolling: observable.startPolling.bind(observable),
|
|
337
|
-
stopPolling: observable.stopPolling.bind(observable),
|
|
338
|
-
subscribeToMore: observable.subscribeToMore.bind(observable),
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
234
|
//# sourceMappingURL=useQuery.cjs.map
|