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