@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
@@ -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 { compact, maybeDeepFreeze, mergeOptions, } from "@apollo/client/utilities";
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 result = useQueryInternals(query, options);
70
- const obsQueryFields = React.useMemo(() => bindObservableMethods(result.observable), [result.observable]);
71
- return React.useMemo(() => ({ ...result, ...obsQueryFields }), [result, obsQueryFields]);
72
- }
73
- function useInternalState(client, query, renderPromises, makeWatchQueryOptions) {
74
- function createInternalState(previous) {
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 internalState = {
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?.data,
87
+ previousData: previous?.resultData.current.data,
90
88
  },
91
89
  };
92
- return internalState;
93
90
  }
94
- let [internalState, updateInternalState] = React.useState(createInternalState);
95
- if (client !== internalState.client || query !== internalState.query) {
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
- const newInternalState = createInternalState(internalState);
103
- updateInternalState(newInternalState);
104
- return newInternalState;
99
+ setState((state = createState(state)));
105
100
  }
106
- return internalState;
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
- client, watchQueryOptions);
120
- useRegisterSSRObservable(observable, renderPromises, ssrAllowed);
121
- const result = useObservableSubscriptionResult(resultData, observable, client, options, watchQueryOptions, prioritizeCacheValues, isSyncSSR);
122
- return result;
123
- }
124
- function useObservableSubscriptionResult(resultData, observable, client, options, watchQueryOptions, prioritizeCacheValues, isSyncSSR) {
125
- const resultOverride = ((isSyncSSR || prioritizeCacheValues) &&
126
- options.ssr === false &&
127
- !options.skip) ?
128
- // If SSR has been explicitly disabled, and this function has been called
129
- // on the server side, return the default loading state.
130
- ssrDisabledResult
131
- : options.skip || watchQueryOptions.fetchPolicy === "standby" ?
132
- // When skipping a query (ie. we're not querying for data but still want to
133
- // render children), make sure the `data` is cleared out and `loading` is
134
- // set to `false` (since we aren't loading anything).
135
- //
136
- // NOTE: We no longer think this is the correct behavior. Skipping should
137
- // not automatically set `data` to `undefined`, but instead leave the
138
- // previous data in place. In other words, skipping should not mandate that
139
- // previously received data is all of a sudden removed. Unfortunately,
140
- // changing this is breaking, so we'll have to wait until Apollo Client 4.0
141
- // to address this.
142
- skipStandbyResult
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
- const currentResultOverride = React.useMemo(() => resultOverride &&
146
- toQueryResult(resultOverride, previousData, observable, client), [client, observable, resultOverride, previousData]);
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
- setResult(result, resultData, observable, client, handleStoreChange);
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
- }, [prioritizeCacheValues, isSyncSSR, observable, resultData, client]), () => currentResultOverride || getCurrentResult(resultData, observable, client), () => currentResultOverride || getCurrentResult(resultData, observable, client));
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, client, watchQueryOptions) {
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(getObsQueryOptions(observable, client, watchQueryOptions));
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?.data || resultData.previousData;
217
- resultData.current = void 0;
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
- const skipStandbyResult = maybeDeepFreeze({
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