@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.
Files changed (168) hide show
  1. package/.changeset/eleven-kangaroos-jump.md +5 -0
  2. package/.changeset/forty-shrimps-fry.md +5 -0
  3. package/.changeset/grumpy-vans-type.md +5 -0
  4. package/.changeset/little-spoons-kick.md +7 -0
  5. package/.changeset/loud-cows-raise.md +7 -0
  6. package/.changeset/many-buses-allow.md +5 -0
  7. package/.changeset/poor-eels-punch.md +5 -0
  8. package/.changeset/pre.json +13 -0
  9. package/.changeset/real-teachers-peel.md +5 -0
  10. package/.changeset/slimy-chicken-melt.md +5 -0
  11. package/.changeset/thick-books-grin.md +5 -0
  12. package/.changeset/tough-rockets-allow.md +5 -0
  13. package/.changeset/tricky-tables-shave.md +5 -0
  14. package/.changeset/warm-ties-sit.md +7 -0
  15. package/CHANGELOG.md +40 -0
  16. package/__cjs/cache/core/cache.cjs +1 -1
  17. package/__cjs/cache/inmemory/entityStore.cjs +3 -3
  18. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  19. package/__cjs/cache/inmemory/policies.cjs +4 -4
  20. package/__cjs/cache/inmemory/readFromStore.cjs +2 -2
  21. package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
  22. package/__cjs/core/ApolloClient.cjs +3 -3
  23. package/__cjs/core/ApolloClient.cjs.map +1 -1
  24. package/__cjs/core/ApolloClient.d.cts +4 -4
  25. package/__cjs/core/LocalState.cjs +2 -2
  26. package/__cjs/core/ObservableQuery.cjs +15 -12
  27. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  28. package/__cjs/core/QueryManager.cjs +57 -35
  29. package/__cjs/core/QueryManager.cjs.map +1 -1
  30. package/__cjs/core/QueryManager.d.cts +3 -3
  31. package/__cjs/core/types.d.cts +32 -0
  32. package/__cjs/invariantErrorCodes.cjs +57 -77
  33. package/__cjs/link/core/ApolloLink.cjs +2 -2
  34. package/__cjs/link/http/checkFetcher.cjs +1 -1
  35. package/__cjs/link/http/createHttpLink.cjs +1 -1
  36. package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
  37. package/__cjs/link/http/serializeFetchParameter.cjs +1 -1
  38. package/__cjs/link/persisted-queries/index.cjs +2 -2
  39. package/__cjs/link/utils/validateOperation.cjs +1 -1
  40. package/__cjs/masking/maskDefinition.cjs +2 -2
  41. package/__cjs/masking/maskFragment.cjs +2 -2
  42. package/__cjs/masking/maskOperation.cjs +1 -1
  43. package/__cjs/masking/utils.cjs +1 -1
  44. package/__cjs/react/context/ApolloConsumer.cjs +1 -1
  45. package/__cjs/react/context/ApolloContext.cjs +1 -1
  46. package/__cjs/react/context/ApolloContext.cjs.map +1 -1
  47. package/__cjs/react/context/ApolloContext.d.cts +2 -2
  48. package/__cjs/react/context/ApolloProvider.cjs +1 -1
  49. package/__cjs/react/hooks/internal/wrapHook.cjs +26 -5
  50. package/__cjs/react/hooks/internal/wrapHook.cjs.map +1 -1
  51. package/__cjs/react/hooks/internal/wrapHook.d.cts +3 -1
  52. package/__cjs/react/hooks/useMutation.cjs +14 -19
  53. package/__cjs/react/hooks/useMutation.cjs.map +1 -1
  54. package/__cjs/react/hooks/useMutation.d.cts +4 -4
  55. package/__cjs/react/hooks/useQuery.cjs +82 -187
  56. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  57. package/__cjs/react/hooks/useQuery.d.cts +4 -0
  58. package/__cjs/react/hooks/useSubscription.cjs +8 -57
  59. package/__cjs/react/hooks/useSubscription.cjs.map +1 -1
  60. package/__cjs/react/hooks/useSubscription.d.cts +6 -42
  61. package/__cjs/react/hooks/useSuspenseQuery.cjs +2 -2
  62. package/__cjs/react/hooks/useSyncExternalStore.cjs +1 -1
  63. package/__cjs/react/internal/index.cjs +3 -1
  64. package/__cjs/react/internal/index.cjs.map +1 -1
  65. package/__cjs/react/internal/index.d.cts +1 -0
  66. package/__cjs/react/ssr/getDataFromTree.cjs +43 -12
  67. package/__cjs/react/ssr/getDataFromTree.cjs.map +1 -1
  68. package/__cjs/react/ssr/getDataFromTree.d.cts +5 -0
  69. package/__cjs/react/ssr/index.cjs +1 -3
  70. package/__cjs/react/ssr/index.cjs.map +1 -1
  71. package/__cjs/react/ssr/index.d.cts +0 -1
  72. package/__cjs/react/ssr/useSSRQuery.cjs +60 -0
  73. package/__cjs/react/ssr/useSSRQuery.cjs.map +1 -0
  74. package/__cjs/react/ssr/useSSRQuery.d.cts +5 -0
  75. package/__cjs/react/types/deprecated.d.cts +1 -1
  76. package/__cjs/react/types/types.documentation.d.cts +4 -12
  77. package/__cjs/testing/matchers/index.cjs +2 -0
  78. package/__cjs/testing/matchers/index.cjs.map +1 -1
  79. package/__cjs/testing/matchers/toEmitStrictTyped.cjs +42 -0
  80. package/__cjs/testing/matchers/toEmitStrictTyped.cjs.map +1 -0
  81. package/__cjs/testing/matchers/toEmitStrictTyped.d.cts +7 -0
  82. package/__cjs/testing/matchers/toEqualStrictTyped.cjs +2 -20
  83. package/__cjs/testing/matchers/toEqualStrictTyped.cjs.map +1 -1
  84. package/__cjs/testing/matchers/utils/getSerializableProperties.cjs +23 -0
  85. package/__cjs/testing/matchers/utils/getSerializableProperties.cjs.map +1 -0
  86. package/__cjs/testing/matchers/utils/getSerializableProperties.d.cts +2 -0
  87. package/__cjs/version.cjs +1 -1
  88. package/__cjs/version.d.cts +1 -1
  89. package/cache/core/cache.js +1 -1
  90. package/cache/inmemory/entityStore.js +3 -3
  91. package/cache/inmemory/key-extractor.js +1 -1
  92. package/cache/inmemory/policies.js +4 -4
  93. package/cache/inmemory/readFromStore.js +2 -2
  94. package/cache/inmemory/writeToStore.js +4 -4
  95. package/core/ApolloClient.d.ts +4 -4
  96. package/core/ApolloClient.js +3 -3
  97. package/core/ApolloClient.js.map +1 -1
  98. package/core/LocalState.js +2 -2
  99. package/core/ObservableQuery.js +15 -12
  100. package/core/ObservableQuery.js.map +1 -1
  101. package/core/QueryManager.d.ts +3 -3
  102. package/core/QueryManager.js +58 -36
  103. package/core/QueryManager.js.map +1 -1
  104. package/core/types.d.ts +32 -0
  105. package/invariantErrorCodes.js +57 -77
  106. package/link/core/ApolloLink.js +2 -2
  107. package/link/http/checkFetcher.js +1 -1
  108. package/link/http/createHttpLink.js +1 -1
  109. package/link/http/parseAndCheckHttpResponse.js +1 -1
  110. package/link/http/serializeFetchParameter.js +1 -1
  111. package/link/persisted-queries/index.js +2 -2
  112. package/link/utils/validateOperation.js +1 -1
  113. package/masking/maskDefinition.js +2 -2
  114. package/masking/maskFragment.js +2 -2
  115. package/masking/maskOperation.js +1 -1
  116. package/masking/utils.js +1 -1
  117. package/package.json +1 -1
  118. package/react/context/ApolloConsumer.js +1 -1
  119. package/react/context/ApolloContext.d.ts +2 -2
  120. package/react/context/ApolloContext.js +1 -1
  121. package/react/context/ApolloContext.js.map +1 -1
  122. package/react/context/ApolloProvider.js +1 -1
  123. package/react/hooks/internal/wrapHook.d.ts +3 -1
  124. package/react/hooks/internal/wrapHook.js +24 -5
  125. package/react/hooks/internal/wrapHook.js.map +1 -1
  126. package/react/hooks/useMutation.d.ts +4 -4
  127. package/react/hooks/useMutation.js +14 -19
  128. package/react/hooks/useMutation.js.map +1 -1
  129. package/react/hooks/useQuery.d.ts +4 -0
  130. package/react/hooks/useQuery.js +83 -188
  131. package/react/hooks/useQuery.js.map +1 -1
  132. package/react/hooks/useSubscription.d.ts +6 -42
  133. package/react/hooks/useSubscription.js +7 -53
  134. package/react/hooks/useSubscription.js.map +1 -1
  135. package/react/hooks/useSuspenseQuery.js +2 -2
  136. package/react/hooks/useSyncExternalStore.js +1 -1
  137. package/react/internal/index.d.ts +1 -0
  138. package/react/internal/index.js +2 -1
  139. package/react/internal/index.js.map +1 -1
  140. package/react/ssr/getDataFromTree.d.ts +5 -0
  141. package/react/ssr/getDataFromTree.js +43 -12
  142. package/react/ssr/getDataFromTree.js.map +1 -1
  143. package/react/ssr/index.d.ts +0 -1
  144. package/react/ssr/index.js +0 -1
  145. package/react/ssr/index.js.map +1 -1
  146. package/react/ssr/useSSRQuery.d.ts +5 -0
  147. package/react/ssr/useSSRQuery.js +56 -0
  148. package/react/ssr/useSSRQuery.js.map +1 -0
  149. package/react/types/deprecated.d.ts +1 -1
  150. package/react/types/types.documentation.d.ts +4 -12
  151. package/testing/matchers/index.js +2 -0
  152. package/testing/matchers/index.js.map +1 -1
  153. package/testing/matchers/toEmitStrictTyped.d.ts +7 -0
  154. package/testing/matchers/toEmitStrictTyped.js +38 -0
  155. package/testing/matchers/toEmitStrictTyped.js.map +1 -0
  156. package/testing/matchers/toEqualStrictTyped.js +1 -19
  157. package/testing/matchers/toEqualStrictTyped.js.map +1 -1
  158. package/testing/matchers/utils/getSerializableProperties.d.ts +2 -0
  159. package/testing/matchers/utils/getSerializableProperties.js +20 -0
  160. package/testing/matchers/utils/getSerializableProperties.js.map +1 -0
  161. package/version.d.ts +1 -1
  162. package/version.js +1 -1
  163. package/__cjs/react/ssr/RenderPromises.cjs +0 -104
  164. package/__cjs/react/ssr/RenderPromises.cjs.map +0 -1
  165. package/__cjs/react/ssr/RenderPromises.d.cts +0 -25
  166. package/react/ssr/RenderPromises.d.ts +0 -25
  167. package/react/ssr/RenderPromises.js +0 -100
  168. 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 result = useQueryInternals(query, options);
74
- const obsQueryFields = React.useMemo(() => bindObservableMethods(result.observable), [result.observable]);
75
- return React.useMemo(() => ({ ...result, ...obsQueryFields }), [result, obsQueryFields]);
76
- }
77
- function useInternalState(client, query, renderPromises, makeWatchQueryOptions) {
78
- function createInternalState(previous) {
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 internalState = {
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?.data,
91
+ previousData: previous?.resultData.current.data,
94
92
  },
95
93
  };
96
- return internalState;
97
94
  }
98
- let [internalState, updateInternalState] = React.useState(createInternalState);
99
- if (client !== internalState.client || query !== internalState.query) {
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
- const newInternalState = createInternalState(internalState);
107
- updateInternalState(newInternalState);
108
- return newInternalState;
103
+ setState((state = createState(state)));
109
104
  }
110
- return internalState;
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
- client, watchQueryOptions);
124
- useRegisterSSRObservable(observable, renderPromises, ssrAllowed);
125
- const result = useObservableSubscriptionResult(resultData, observable, client, options, watchQueryOptions, prioritizeCacheValues, isSyncSSR);
126
- return result;
127
- }
128
- function useObservableSubscriptionResult(resultData, observable, client, options, watchQueryOptions, prioritizeCacheValues, isSyncSSR) {
129
- const resultOverride = ((isSyncSSR || prioritizeCacheValues) &&
130
- options.ssr === false &&
131
- !options.skip) ?
132
- // If SSR has been explicitly disabled, and this function has been called
133
- // on the server side, return the default loading state.
134
- ssrDisabledResult
135
- : options.skip || watchQueryOptions.fetchPolicy === "standby" ?
136
- // When skipping a query (ie. we're not querying for data but still want to
137
- // render children), make sure the `data` is cleared out and `loading` is
138
- // set to `false` (since we aren't loading anything).
139
- //
140
- // NOTE: We no longer think this is the correct behavior. Skipping should
141
- // not automatically set `data` to `undefined`, but instead leave the
142
- // previous data in place. In other words, skipping should not mandate that
143
- // previously received data is all of a sudden removed. Unfortunately,
144
- // changing this is breaking, so we'll have to wait until Apollo Client 4.0
145
- // to address this.
146
- skipStandbyResult
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
- const currentResultOverride = React.useMemo(() => resultOverride &&
150
- toQueryResult(resultOverride, previousData, observable, client), [client, observable, resultOverride, previousData]);
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
- setResult(result, resultData, observable, client, handleStoreChange);
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
- }, [prioritizeCacheValues, isSyncSSR, observable, resultData, client]), () => currentResultOverride || getCurrentResult(resultData, observable, client), () => currentResultOverride || getCurrentResult(resultData, observable, client));
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, client, watchQueryOptions) {
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(getObsQueryOptions(observable, client, watchQueryOptions));
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?.data || resultData.previousData;
221
- resultData.current = void 0;
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
- const skipStandbyResult = (0, utilities_1.maybeDeepFreeze)({
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