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