@apollo/client 4.2.0-alpha.5 → 4.2.0-alpha.7
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/CHANGELOG.md +33 -0
- package/__cjs/core/ApolloClient.cjs.map +1 -1
- package/__cjs/core/ApolloClient.d.cts +29 -2
- package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useBackgroundQuery.d.cts +1019 -19
- package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useLazyQuery.d.cts +115 -7
- package/__cjs/react/hooks/useLoadableQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useLoadableQuery.d.cts +195 -8
- package/__cjs/react/hooks/useMutation.cjs.map +1 -1
- package/__cjs/react/hooks/useMutation.d.cts +20 -9
- package/__cjs/react/hooks/useQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useQuery.d.cts +280 -11
- package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
- package/__cjs/react/hooks/useSuspenseQuery.d.cts +405 -13
- package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
- package/__cjs/react/query-preloader/createQueryPreloader.d.cts +395 -123
- package/__cjs/version.cjs +1 -1
- package/core/ApolloClient.d.ts +29 -2
- package/core/ApolloClient.js.map +1 -1
- package/package.json +1 -1
- package/react/hooks/useBackgroundQuery.d.ts +1019 -19
- package/react/hooks/useBackgroundQuery.js.map +1 -1
- package/react/hooks/useLazyQuery.d.ts +115 -7
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useLoadableQuery.d.ts +195 -8
- package/react/hooks/useLoadableQuery.js.map +1 -1
- package/react/hooks/useMutation.d.ts +20 -9
- package/react/hooks/useMutation.js.map +1 -1
- package/react/hooks/useQuery.d.ts +280 -11
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useSuspenseQuery.d.ts +405 -13
- package/react/hooks/useSuspenseQuery.js.map +1 -1
- package/react/hooks-compiled/useBackgroundQuery.d.ts +1019 -19
- package/react/hooks-compiled/useBackgroundQuery.js.map +1 -1
- package/react/hooks-compiled/useLazyQuery.d.ts +115 -7
- package/react/hooks-compiled/useLazyQuery.js.map +1 -1
- package/react/hooks-compiled/useLoadableQuery.d.ts +195 -8
- package/react/hooks-compiled/useLoadableQuery.js.map +1 -1
- package/react/hooks-compiled/useMutation.d.ts +20 -9
- package/react/hooks-compiled/useMutation.js.map +1 -1
- package/react/hooks-compiled/useQuery.d.ts +280 -11
- package/react/hooks-compiled/useQuery.js.map +1 -1
- package/react/hooks-compiled/useSuspenseQuery.d.ts +405 -13
- package/react/hooks-compiled/useSuspenseQuery.js.map +1 -1
- package/react/query-preloader/createQueryPreloader.d.ts +395 -123
- package/react/query-preloader/createQueryPreloader.js.map +1 -1
- package/version.js +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ApolloClient, DefaultContext, DocumentNode, ErrorPolicy, OperationVariables, RefetchOn, RefetchWritePolicy, TypedDocumentNode, WatchQueryFetchPolicy } from "@apollo/client";
|
|
2
2
|
import type { PreloadedQueryRef } from "@apollo/client/react";
|
|
3
|
-
import type { NoInfer, VariablesOption } from "@apollo/client/utilities/internal";
|
|
3
|
+
import type { NoInfer, OptionWithFallback, SignatureStyle, VariablesOption } from "@apollo/client/utilities/internal";
|
|
4
4
|
export type PreloadQueryFetchPolicy = Extract<WatchQueryFetchPolicy, "cache-first" | "network-only" | "no-cache" | "cache-and-network">;
|
|
5
5
|
export type PreloadQueryOptions<TVariables extends OperationVariables = OperationVariables> = {
|
|
6
6
|
/**
|
|
@@ -93,128 +93,7 @@ export type PreloadQueryOptions<TVariables extends OperationVariables = Operatio
|
|
|
93
93
|
* }
|
|
94
94
|
* ```
|
|
95
95
|
*/
|
|
96
|
-
export interface PreloadQueryFunction {
|
|
97
|
-
/**
|
|
98
|
-
* A function that will begin loading a query when called. It's result can be
|
|
99
|
-
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
100
|
-
* This is useful when you want to start loading a query as early as possible
|
|
101
|
-
* outside of a React component.
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
*
|
|
105
|
-
* ```js
|
|
106
|
-
* const preloadQuery = createQueryPreloader(client);
|
|
107
|
-
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
108
|
-
*
|
|
109
|
-
* function App() {
|
|
110
|
-
* return (
|
|
111
|
-
* <Suspense fallback={<div>Loading</div>}>
|
|
112
|
-
* <MyQuery />
|
|
113
|
-
* </Suspense>
|
|
114
|
-
* );
|
|
115
|
-
* }
|
|
116
|
-
*
|
|
117
|
-
* function MyQuery() {
|
|
118
|
-
* const { data } = useReadQuery(queryRef);
|
|
119
|
-
*
|
|
120
|
-
* // do something with `data`
|
|
121
|
-
* }
|
|
122
|
-
* ```
|
|
123
|
-
*/
|
|
124
|
-
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
125
|
-
returnPartialData: true;
|
|
126
|
-
errorPolicy: "ignore" | "all";
|
|
127
|
-
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "partial" | "empty">;
|
|
128
|
-
/**
|
|
129
|
-
* A function that will begin loading a query when called. It's result can be
|
|
130
|
-
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
131
|
-
* This is useful when you want to start loading a query as early as possible
|
|
132
|
-
* outside of a React component.
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
*
|
|
136
|
-
* ```js
|
|
137
|
-
* const preloadQuery = createQueryPreloader(client);
|
|
138
|
-
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
139
|
-
*
|
|
140
|
-
* function App() {
|
|
141
|
-
* return (
|
|
142
|
-
* <Suspense fallback={<div>Loading</div>}>
|
|
143
|
-
* <MyQuery />
|
|
144
|
-
* </Suspense>
|
|
145
|
-
* );
|
|
146
|
-
* }
|
|
147
|
-
*
|
|
148
|
-
* function MyQuery() {
|
|
149
|
-
* const { data } = useReadQuery(queryRef);
|
|
150
|
-
*
|
|
151
|
-
* // do something with `data`
|
|
152
|
-
* }
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
156
|
-
errorPolicy: "ignore" | "all";
|
|
157
|
-
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "empty">;
|
|
158
|
-
/**
|
|
159
|
-
* A function that will begin loading a query when called. It's result can be
|
|
160
|
-
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
161
|
-
* This is useful when you want to start loading a query as early as possible
|
|
162
|
-
* outside of a React component.
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
*
|
|
166
|
-
* ```js
|
|
167
|
-
* const preloadQuery = createQueryPreloader(client);
|
|
168
|
-
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
169
|
-
*
|
|
170
|
-
* function App() {
|
|
171
|
-
* return (
|
|
172
|
-
* <Suspense fallback={<div>Loading</div>}>
|
|
173
|
-
* <MyQuery />
|
|
174
|
-
* </Suspense>
|
|
175
|
-
* );
|
|
176
|
-
* }
|
|
177
|
-
*
|
|
178
|
-
* function MyQuery() {
|
|
179
|
-
* const { data } = useReadQuery(queryRef);
|
|
180
|
-
*
|
|
181
|
-
* // do something with `data`
|
|
182
|
-
* }
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
186
|
-
returnPartialData: true;
|
|
187
|
-
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "partial">;
|
|
188
|
-
/**
|
|
189
|
-
* A function that will begin loading a query when called. It's result can be
|
|
190
|
-
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
191
|
-
* This is useful when you want to start loading a query as early as possible
|
|
192
|
-
* outside of a React component.
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
*
|
|
196
|
-
* ```js
|
|
197
|
-
* const preloadQuery = createQueryPreloader(client);
|
|
198
|
-
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
199
|
-
*
|
|
200
|
-
* function App() {
|
|
201
|
-
* return (
|
|
202
|
-
* <Suspense fallback={<div>Loading</div>}>
|
|
203
|
-
* <MyQuery />
|
|
204
|
-
* </Suspense>
|
|
205
|
-
* );
|
|
206
|
-
* }
|
|
207
|
-
*
|
|
208
|
-
* function MyQuery() {
|
|
209
|
-
* const { data } = useReadQuery(queryRef);
|
|
210
|
-
*
|
|
211
|
-
* // do something with `data`
|
|
212
|
-
* }
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: {} extends TVariables ? [
|
|
216
|
-
options?: PreloadQueryOptions<NoInfer<TVariables>>
|
|
217
|
-
] : [options: PreloadQueryOptions<NoInfer<TVariables>>]): PreloadedQueryRef<TData, TVariables, "complete" | "streaming">;
|
|
96
|
+
export interface PreloadQueryFunction extends PreloadQueryFunction.Signatures.Evaluated {
|
|
218
97
|
/**
|
|
219
98
|
* A function that returns a promise that resolves when the query has finished
|
|
220
99
|
* loading. The promise resolves with the `QueryReference` itself.
|
|
@@ -253,6 +132,399 @@ export interface PreloadQueryFunction {
|
|
|
253
132
|
*/
|
|
254
133
|
toPromise<TQueryRef extends PreloadedQueryRef<any, any, any>>(queryRef: TQueryRef): Promise<TQueryRef>;
|
|
255
134
|
}
|
|
135
|
+
export declare namespace PreloadQueryFunction {
|
|
136
|
+
interface DefaultOptions extends ApolloClient.DefaultOptions.WatchQuery.Calculated {
|
|
137
|
+
}
|
|
138
|
+
namespace DocumentationTypes {
|
|
139
|
+
/**
|
|
140
|
+
* @deprecated Avoid manually specifying generics on `preloadQuery`.
|
|
141
|
+
* Instead, rely on TypeScript's type inference along with a correctly typed `TypedDocumentNode` to get accurate types for your query results.
|
|
142
|
+
*
|
|
143
|
+
*
|
|
144
|
+
* A function that will begin loading a query when called. It's result can be
|
|
145
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
146
|
+
* This is useful when you want to start loading a query as early as possible
|
|
147
|
+
* outside of a React component.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
*
|
|
151
|
+
* ```js
|
|
152
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
153
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
154
|
+
*
|
|
155
|
+
* function App() {
|
|
156
|
+
* return (
|
|
157
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
158
|
+
* <MyQuery />
|
|
159
|
+
* </Suspense>
|
|
160
|
+
* );
|
|
161
|
+
* }
|
|
162
|
+
*
|
|
163
|
+
* function MyQuery() {
|
|
164
|
+
* const { data } = useReadQuery(queryRef);
|
|
165
|
+
*
|
|
166
|
+
* // do something with `data`
|
|
167
|
+
* }
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
interface PreloadQueryFunction_Deprecated extends PreloadQueryFunction {
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
type ResultForOptions<TData, TVariables extends OperationVariables, TOptions extends Record<string, never> | PreloadQueryOptions<TVariables>> = TOptions extends any ? PreloadedQueryRef<TData, TVariables, ResultForOptions.States<TOptions, DefaultOptions>> : never;
|
|
174
|
+
namespace ResultForOptions {
|
|
175
|
+
type States<TOptions, TDefaultOptions extends DefaultOptions = DefaultOptions> = "complete" | "streaming" | (OptionWithFallback<TOptions, TDefaultOptions, "errorPolicy"> extends ("none") ? never : "empty") | (OptionWithFallback<TOptions, TDefaultOptions, "returnPartialData"> extends false ? never : "partial");
|
|
176
|
+
}
|
|
177
|
+
namespace Signatures {
|
|
178
|
+
interface Classic {
|
|
179
|
+
/**
|
|
180
|
+
* A function that will begin loading a query when called. It's result can be
|
|
181
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
182
|
+
* This is useful when you want to start loading a query as early as possible
|
|
183
|
+
* outside of a React component.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
*
|
|
187
|
+
* ```js
|
|
188
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
189
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
190
|
+
*
|
|
191
|
+
* function App() {
|
|
192
|
+
* return (
|
|
193
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
194
|
+
* <MyQuery />
|
|
195
|
+
* </Suspense>
|
|
196
|
+
* );
|
|
197
|
+
* }
|
|
198
|
+
*
|
|
199
|
+
* function MyQuery() {
|
|
200
|
+
* const { data } = useReadQuery(queryRef);
|
|
201
|
+
*
|
|
202
|
+
* // do something with `data`
|
|
203
|
+
* }
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
<TData, TVariables extends OperationVariables, _INFERENCE_ONLY_DO_NOT_SPECIFY extends "inferred">(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
207
|
+
returnPartialData: true;
|
|
208
|
+
errorPolicy: "ignore" | "all";
|
|
209
|
+
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "partial" | "empty">;
|
|
210
|
+
/**
|
|
211
|
+
* A function that will begin loading a query when called. It's result can be
|
|
212
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
213
|
+
* This is useful when you want to start loading a query as early as possible
|
|
214
|
+
* outside of a React component.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
*
|
|
218
|
+
* ```js
|
|
219
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
220
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
221
|
+
*
|
|
222
|
+
* function App() {
|
|
223
|
+
* return (
|
|
224
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
225
|
+
* <MyQuery />
|
|
226
|
+
* </Suspense>
|
|
227
|
+
* );
|
|
228
|
+
* }
|
|
229
|
+
*
|
|
230
|
+
* function MyQuery() {
|
|
231
|
+
* const { data } = useReadQuery(queryRef);
|
|
232
|
+
*
|
|
233
|
+
* // do something with `data`
|
|
234
|
+
* }
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
<TData, TVariables extends OperationVariables, _INFERENCE_ONLY_DO_NOT_SPECIFY extends "inferred">(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
238
|
+
errorPolicy: "ignore" | "all";
|
|
239
|
+
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "empty">;
|
|
240
|
+
/**
|
|
241
|
+
* A function that will begin loading a query when called. It's result can be
|
|
242
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
243
|
+
* This is useful when you want to start loading a query as early as possible
|
|
244
|
+
* outside of a React component.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
*
|
|
248
|
+
* ```js
|
|
249
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
250
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
251
|
+
*
|
|
252
|
+
* function App() {
|
|
253
|
+
* return (
|
|
254
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
255
|
+
* <MyQuery />
|
|
256
|
+
* </Suspense>
|
|
257
|
+
* );
|
|
258
|
+
* }
|
|
259
|
+
*
|
|
260
|
+
* function MyQuery() {
|
|
261
|
+
* const { data } = useReadQuery(queryRef);
|
|
262
|
+
*
|
|
263
|
+
* // do something with `data`
|
|
264
|
+
* }
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
<TData, TVariables extends OperationVariables, _INFERENCE_ONLY_DO_NOT_SPECIFY extends "inferred">(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
268
|
+
returnPartialData: true;
|
|
269
|
+
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "partial">;
|
|
270
|
+
/**
|
|
271
|
+
* A function that will begin loading a query when called. It's result can be
|
|
272
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
273
|
+
* This is useful when you want to start loading a query as early as possible
|
|
274
|
+
* outside of a React component.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
*
|
|
278
|
+
* ```js
|
|
279
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
280
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
281
|
+
*
|
|
282
|
+
* function App() {
|
|
283
|
+
* return (
|
|
284
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
285
|
+
* <MyQuery />
|
|
286
|
+
* </Suspense>
|
|
287
|
+
* );
|
|
288
|
+
* }
|
|
289
|
+
*
|
|
290
|
+
* function MyQuery() {
|
|
291
|
+
* const { data } = useReadQuery(queryRef);
|
|
292
|
+
*
|
|
293
|
+
* // do something with `data`
|
|
294
|
+
* }
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
<TData, TVariables extends OperationVariables, _INFERENCE_ONLY_DO_NOT_SPECIFY extends "inferred">(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: {} extends TVariables ? [
|
|
298
|
+
options?: PreloadQueryOptions<NoInfer<TVariables>>
|
|
299
|
+
] : [options: PreloadQueryOptions<NoInfer<TVariables>>]): PreloadedQueryRef<TData, TVariables, "complete" | "streaming">;
|
|
300
|
+
/**
|
|
301
|
+
* @deprecated Avoid manually specifying generics on `preloadQuery`.
|
|
302
|
+
* Instead, rely on TypeScript's type inference along with a correctly typed `TypedDocumentNode` to get accurate types for your query results.
|
|
303
|
+
*
|
|
304
|
+
*
|
|
305
|
+
* A function that will begin loading a query when called. It's result can be
|
|
306
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
307
|
+
* This is useful when you want to start loading a query as early as possible
|
|
308
|
+
* outside of a React component.
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
*
|
|
312
|
+
* ```js
|
|
313
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
314
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
315
|
+
*
|
|
316
|
+
* function App() {
|
|
317
|
+
* return (
|
|
318
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
319
|
+
* <MyQuery />
|
|
320
|
+
* </Suspense>
|
|
321
|
+
* );
|
|
322
|
+
* }
|
|
323
|
+
*
|
|
324
|
+
* function MyQuery() {
|
|
325
|
+
* const { data } = useReadQuery(queryRef);
|
|
326
|
+
*
|
|
327
|
+
* // do something with `data`
|
|
328
|
+
* }
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
331
|
+
<TData, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
332
|
+
returnPartialData: true;
|
|
333
|
+
errorPolicy: "ignore" | "all";
|
|
334
|
+
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "partial" | "empty">;
|
|
335
|
+
/**
|
|
336
|
+
* @deprecated Avoid manually specifying generics on `preloadQuery`.
|
|
337
|
+
* Instead, rely on TypeScript's type inference along with a correctly typed `TypedDocumentNode` to get accurate types for your query results.
|
|
338
|
+
*
|
|
339
|
+
*
|
|
340
|
+
* A function that will begin loading a query when called. It's result can be
|
|
341
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
342
|
+
* This is useful when you want to start loading a query as early as possible
|
|
343
|
+
* outside of a React component.
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
*
|
|
347
|
+
* ```js
|
|
348
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
349
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
350
|
+
*
|
|
351
|
+
* function App() {
|
|
352
|
+
* return (
|
|
353
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
354
|
+
* <MyQuery />
|
|
355
|
+
* </Suspense>
|
|
356
|
+
* );
|
|
357
|
+
* }
|
|
358
|
+
*
|
|
359
|
+
* function MyQuery() {
|
|
360
|
+
* const { data } = useReadQuery(queryRef);
|
|
361
|
+
*
|
|
362
|
+
* // do something with `data`
|
|
363
|
+
* }
|
|
364
|
+
* ```
|
|
365
|
+
*/
|
|
366
|
+
<TData, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
367
|
+
errorPolicy: "ignore" | "all";
|
|
368
|
+
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "empty">;
|
|
369
|
+
/**
|
|
370
|
+
* @deprecated Avoid manually specifying generics on `preloadQuery`.
|
|
371
|
+
* Instead, rely on TypeScript's type inference along with a correctly typed `TypedDocumentNode` to get accurate types for your query results.
|
|
372
|
+
*
|
|
373
|
+
*
|
|
374
|
+
* A function that will begin loading a query when called. It's result can be
|
|
375
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
376
|
+
* This is useful when you want to start loading a query as early as possible
|
|
377
|
+
* outside of a React component.
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
*
|
|
381
|
+
* ```js
|
|
382
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
383
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
384
|
+
*
|
|
385
|
+
* function App() {
|
|
386
|
+
* return (
|
|
387
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
388
|
+
* <MyQuery />
|
|
389
|
+
* </Suspense>
|
|
390
|
+
* );
|
|
391
|
+
* }
|
|
392
|
+
*
|
|
393
|
+
* function MyQuery() {
|
|
394
|
+
* const { data } = useReadQuery(queryRef);
|
|
395
|
+
*
|
|
396
|
+
* // do something with `data`
|
|
397
|
+
* }
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
<TData, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
|
|
401
|
+
returnPartialData: true;
|
|
402
|
+
}): PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "partial">;
|
|
403
|
+
/**
|
|
404
|
+
* @deprecated Avoid manually specifying generics on `preloadQuery`.
|
|
405
|
+
* Instead, rely on TypeScript's type inference along with a correctly typed `TypedDocumentNode` to get accurate types for your query results.
|
|
406
|
+
*
|
|
407
|
+
*
|
|
408
|
+
* A function that will begin loading a query when called. It's result can be
|
|
409
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
410
|
+
* This is useful when you want to start loading a query as early as possible
|
|
411
|
+
* outside of a React component.
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
*
|
|
415
|
+
* ```js
|
|
416
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
417
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
418
|
+
*
|
|
419
|
+
* function App() {
|
|
420
|
+
* return (
|
|
421
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
422
|
+
* <MyQuery />
|
|
423
|
+
* </Suspense>
|
|
424
|
+
* );
|
|
425
|
+
* }
|
|
426
|
+
*
|
|
427
|
+
* function MyQuery() {
|
|
428
|
+
* const { data } = useReadQuery(queryRef);
|
|
429
|
+
*
|
|
430
|
+
* // do something with `data`
|
|
431
|
+
* }
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
<TData, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: {} extends TVariables ? [
|
|
435
|
+
options?: PreloadQueryOptions<NoInfer<TVariables>>
|
|
436
|
+
] : [options: PreloadQueryOptions<NoInfer<TVariables>>]): PreloadedQueryRef<TData, TVariables, "complete" | "streaming">;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* A function that will begin loading a query when called. It's result can be
|
|
440
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
441
|
+
* This is useful when you want to start loading a query as early as possible
|
|
442
|
+
* outside of a React component.
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
*
|
|
446
|
+
* ```js
|
|
447
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
448
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
449
|
+
*
|
|
450
|
+
* function App() {
|
|
451
|
+
* return (
|
|
452
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
453
|
+
* <MyQuery />
|
|
454
|
+
* </Suspense>
|
|
455
|
+
* );
|
|
456
|
+
* }
|
|
457
|
+
*
|
|
458
|
+
* function MyQuery() {
|
|
459
|
+
* const { data } = useReadQuery(queryRef);
|
|
460
|
+
*
|
|
461
|
+
* // do something with `data`
|
|
462
|
+
* }
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
interface Modern {
|
|
466
|
+
/**
|
|
467
|
+
* A function that will begin loading a query when called. It's result can be
|
|
468
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
469
|
+
* This is useful when you want to start loading a query as early as possible
|
|
470
|
+
* outside of a React component.
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
*
|
|
474
|
+
* ```js
|
|
475
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
476
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
477
|
+
*
|
|
478
|
+
* function App() {
|
|
479
|
+
* return (
|
|
480
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
481
|
+
* <MyQuery />
|
|
482
|
+
* </Suspense>
|
|
483
|
+
* );
|
|
484
|
+
* }
|
|
485
|
+
*
|
|
486
|
+
* function MyQuery() {
|
|
487
|
+
* const { data } = useReadQuery(queryRef);
|
|
488
|
+
*
|
|
489
|
+
* // do something with `data`
|
|
490
|
+
* }
|
|
491
|
+
* ```
|
|
492
|
+
*/
|
|
493
|
+
<TData, TVariables extends OperationVariables, TOptions extends never>(query: {} extends TVariables ? DocumentNode | TypedDocumentNode<TData, TVariables> : never): PreloadQueryFunction.ResultForOptions<TData, TVariables, Record<string, never>>;
|
|
494
|
+
/**
|
|
495
|
+
* A function that will begin loading a query when called. It's result can be
|
|
496
|
+
* read by `useReadQuery` which will suspend until the query is loaded.
|
|
497
|
+
* This is useful when you want to start loading a query as early as possible
|
|
498
|
+
* outside of a React component.
|
|
499
|
+
*
|
|
500
|
+
* @example
|
|
501
|
+
*
|
|
502
|
+
* ```js
|
|
503
|
+
* const preloadQuery = createQueryPreloader(client);
|
|
504
|
+
* const queryRef = preloadQuery(query, { variables, ...otherOptions });
|
|
505
|
+
*
|
|
506
|
+
* function App() {
|
|
507
|
+
* return (
|
|
508
|
+
* <Suspense fallback={<div>Loading</div>}>
|
|
509
|
+
* <MyQuery />
|
|
510
|
+
* </Suspense>
|
|
511
|
+
* );
|
|
512
|
+
* }
|
|
513
|
+
*
|
|
514
|
+
* function MyQuery() {
|
|
515
|
+
* const { data } = useReadQuery(queryRef);
|
|
516
|
+
*
|
|
517
|
+
* // do something with `data`
|
|
518
|
+
* }
|
|
519
|
+
* ```
|
|
520
|
+
*/
|
|
521
|
+
<TData, TVariables extends OperationVariables, TOptions extends PreloadQueryOptions<NoInfer<TVariables>> & VariablesOption<TVariables & {
|
|
522
|
+
[K in Exclude<keyof TOptions["variables"], keyof TVariables>]?: never;
|
|
523
|
+
}>>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: {} extends TVariables ? [options?: TOptions] : [options: TOptions]): PreloadQueryFunction.ResultForOptions<TData, TVariables, TOptions>;
|
|
524
|
+
}
|
|
525
|
+
type Evaluated = SignatureStyle extends "classic" ? Classic : Modern;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
256
528
|
/**
|
|
257
529
|
* A higher order function that returns a `preloadQuery` function which
|
|
258
530
|
* can be used to begin loading a query with the given `client`. This is useful
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQueryPreloader.js","sourceRoot":"","sources":["../../../src/react/query-preloader/createQueryPreloader.ts"],"names":[],"mappings":"AAYA,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,sBAAsB,EACtB,YAAY,GACb,MAAM,+BAA+B,CAAC;AAKvC,OAAO,EAAE,oBAAoB,EAAE,MAAM,6CAA6C,CAAC;AAEnF,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAkItD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,oBAAoB,CAClC,MAAoB;IAEpB,OAAO,QAAQ,CACb,sBAAsB,EACtB,qBAAqB,EACrB,MAAM,CACP,CAAC,MAAM,CAAC,CAAC;AACZ,CAAC;AAED,MAAM,qBAAqB,GAAgC,CAAC,MAAM,EAAE,EAAE;IACpE,SAAS,YAAY,CAInB,KAA0D,EAC1D,UACgC,EAAS;QAEzC,MAAM,QAAQ,GAAG,IAAI,sBAAsB,CACzC,MAAM,CAAC,UAAU,CAAC;YAChB,GAAG,OAAO;YACV,KAAK;YACL,2BAA2B,EAAE,KAAK;SACS,CAAC,EAC9C;YACE,oBAAoB,EAClB,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,QAAQ,EAAE,oBAAoB;SAC9D,CACF,CAAC;QAEF,MAAM,OAAO,GAAG,YAAY,CAAC,QAAQ,CAGpC,CAAC;QACF,yBAAyB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAC7C,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE;QACjC,SAAS,CACP,QAAmB;YAEnB,qBAAqB,CAAC,QAAQ,CAAC,CAAC;YAChC,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,CAAC;QAC1D,CAAC;KACF,CAAC,CAAC;AACL,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,sFAAsF;AACtF,SAAS,yBAAyB,CAChC,OAAyC,EACzC,QAAgC;IAEhC,MAAM,EAAE,WAAW,EAAE,kBAAkB,EAAE,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IACjE,QAAQ,CAAC,QAAQ,CAAC,OAAO,EAAE,kBAAkB,EAAE,QAAQ,CAAC,CAAC;IACzD,uEAAuE;IACvE,qCAAqC;IACrC,6EAA6E;IAC7E,wFAAwF;IACxF,QAAQ,CAAC,MAAM,GAAG,kBAAkB,CAAC,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AACrE,CAAC;AAOD,sFAAsF;AACtF,SAAS,kBAAkB,CACzB,cAA8B,EAC9B,WAAuB;IAEvB,OAAO,UAAU,GAAG,IAAI;QACtB,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC1B,MAAM,OAAO,GAAG,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACjD,WAAW,EAAE,CAAC;QACd,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;AACJ,CAAC;AAED,sFAAsF;AACtF,SAAS,UAAU,CAAC,QAAgC;IAClD,MAAM,WAAW,GAAG,QAAQ,CAAC,UAAU,EAAE,CAAC;IAC1C,MAAM,cAAc,GAAG,QAAQ,CAAC,OAAO,CAAC;IAExC,OAAO;QACL,WAAW;QACX,kBAAkB,EAAE,GAAG,EAAE,CACvB,cAAc,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;KACtD,CAAC;AACJ,CAAC;AAED,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAa,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC","sourcesContent":["import type {\n ApolloClient,\n DefaultContext,\n DocumentNode,\n ErrorPolicy,\n OperationVariables,\n RefetchOn,\n RefetchWritePolicy,\n TypedDocumentNode,\n WatchQueryFetchPolicy,\n} from \"@apollo/client\";\nimport type { PreloadedQueryRef } from \"@apollo/client/react\";\nimport {\n assertWrappedQueryRef,\n getWrappedPromise,\n InternalQueryReference,\n wrapQueryRef,\n} from \"@apollo/client/react/internal\";\nimport type {\n NoInfer,\n VariablesOption,\n} from \"@apollo/client/utilities/internal\";\nimport { FinalizationRegistry } from \"@apollo/client/utilities/internal/ponyfills\";\n\nimport { wrapHook } from \"../hooks/internal/index.js\";\n\nexport type PreloadQueryFetchPolicy = Extract<\n WatchQueryFetchPolicy,\n \"cache-first\" | \"network-only\" | \"no-cache\" | \"cache-and-network\"\n>;\n\nexport type PreloadQueryOptions<\n TVariables extends OperationVariables = OperationVariables,\n> = {\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#context:member} */\n context?: DefaultContext;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#errorPolicy:member} */\n errorPolicy?: ErrorPolicy;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#fetchPolicy:member} */\n fetchPolicy?: PreloadQueryFetchPolicy;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#returnPartialData:member} */\n returnPartialData?: boolean;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#refetchWritePolicy:member} */\n refetchWritePolicy?: RefetchWritePolicy;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#refetchOn:member} */\n refetchOn?: RefetchOn.Option;\n} & VariablesOption<TVariables>;\n\n/**\n * A function that will begin loading a query when called. It's result can be\n * read by `useReadQuery` which will suspend until the query is loaded.\n * This is useful when you want to start loading a query as early as possible\n * outside of a React component.\n *\n * @example\n *\n * ```js\n * const preloadQuery = createQueryPreloader(client);\n * const queryRef = preloadQuery(query, { variables, ...otherOptions });\n *\n * function App() {\n * return (\n * <Suspense fallback={<div>Loading</div>}>\n * <MyQuery />\n * </Suspense>\n * );\n * }\n *\n * function MyQuery() {\n * const { data } = useReadQuery(queryRef);\n *\n * // do something with `data`\n * }\n * ```\n */\nexport interface PreloadQueryFunction {\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <TData = unknown, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n ): PreloadedQueryRef<\n TData,\n TVariables,\n \"complete\" | \"streaming\" | \"partial\" | \"empty\"\n >;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <TData = unknown, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n errorPolicy: \"ignore\" | \"all\";\n }\n ): PreloadedQueryRef<TData, TVariables, \"complete\" | \"streaming\" | \"empty\">;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <TData = unknown, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n returnPartialData: true;\n }\n ): PreloadedQueryRef<TData, TVariables, \"complete\" | \"streaming\" | \"partial\">;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <TData = unknown, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n ...[options]: {} extends TVariables ?\n [options?: PreloadQueryOptions<NoInfer<TVariables>>]\n : [options: PreloadQueryOptions<NoInfer<TVariables>>]\n ): PreloadedQueryRef<TData, TVariables, \"complete\" | \"streaming\">;\n\n /**\n * A function that returns a promise that resolves when the query has finished\n * loading. The promise resolves with the `QueryReference` itself.\n *\n * @remarks\n * This method is useful for preloading queries in data loading routers, such\n * as [React Router](https://reactrouter.com/en/main) or [TanStack Router](https://tanstack.com/router),\n * to prevent routes from transitioning until the query has finished loading.\n * `data` is not exposed on the promise to discourage using the data in\n * `loader` functions and exposing it to your route components. Instead, we\n * prefer you rely on `useReadQuery` to access the data to ensure your\n * component can rerender with cache updates. If you need to access raw query\n * data, use `client.query()` directly.\n *\n * @example\n * Here's an example using React Router's `loader` function:\n *\n * ```ts\n * import { createQueryPreloader } from \"@apollo/client\";\n *\n * const preloadQuery = createQueryPreloader(client);\n *\n * export async function loader() {\n * const queryRef = preloadQuery(GET_DOGS_QUERY);\n *\n * return preloadQuery.toPromise(queryRef);\n * }\n *\n * export function RouteComponent() {\n * const queryRef = useLoaderData();\n * const { data } = useReadQuery(queryRef);\n *\n * // ...\n * }\n * ```\n */\n toPromise<TQueryRef extends PreloadedQueryRef<any, any, any>>(\n queryRef: TQueryRef\n ): Promise<TQueryRef>;\n}\n\n/**\n * A higher order function that returns a `preloadQuery` function which\n * can be used to begin loading a query with the given `client`. This is useful\n * when you want to start loading a query as early as possible outside of a\n * React component.\n *\n * > Refer to the [Suspense - Initiating queries outside React](https://www.apollographql.com/docs/react/data/suspense#initiating-queries-outside-react) section for a more in-depth overview.\n *\n * @param client - The `ApolloClient` instance that will be used to load queries\n * from the returned `preloadQuery` function.\n * @returns The `preloadQuery` function.\n *\n * @example\n *\n * ```js\n * const preloadQuery = createQueryPreloader(client);\n * ```\n */\nexport function createQueryPreloader(\n client: ApolloClient\n): PreloadQueryFunction {\n return wrapHook(\n \"createQueryPreloader\",\n _createQueryPreloader,\n client\n )(client);\n}\n\nconst _createQueryPreloader: typeof createQueryPreloader = (client) => {\n function preloadQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n >(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> &\n VariablesOption<TVariables> = {} as any\n ): PreloadedQueryRef<TData, TVariables> {\n const queryRef = new InternalQueryReference(\n client.watchQuery({\n ...options,\n query,\n notifyOnNetworkStatusChange: false,\n } as ApolloClient.WatchQueryOptions<any, any>),\n {\n autoDisposeTimeoutMs:\n client.defaultOptions.react?.suspense?.autoDisposeTimeoutMs,\n }\n );\n\n const wrapped = wrapQueryRef(queryRef) as unknown as PreloadedQueryRef<\n TData,\n TVariables\n >;\n softRetainWhileReferenced(wrapped, queryRef);\n return wrapped;\n }\n\n return Object.assign(preloadQuery, {\n toPromise<TQueryRef extends PreloadedQueryRef<any, any, any>>(\n queryRef: TQueryRef\n ) {\n assertWrappedQueryRef(queryRef);\n return getWrappedPromise(queryRef).then(() => queryRef);\n },\n });\n};\n\n/**\n * Soft-retains the underlying `InternalQueryReference` while the `PreloadedQueryRef`\n * is still reachable.\n * When the `PreloadedQueryRef` is garbage collected, the soft retain is\n * disposed of, but only after the initial query has finished loading.\n * Once the `InternalQueryReference` is properly retained, the check for garbage\n * collection is unregistered and the soft retain is disposed of immediately.\n */\n// this is an individual function to avoid closing over any values more than necessary\nfunction softRetainWhileReferenced(\n wrapped: PreloadedQueryRef<any, any, any>,\n queryRef: InternalQueryReference\n) {\n const { softDispose, delayedSoftDispose } = getCleanup(queryRef);\n registry.register(wrapped, delayedSoftDispose, queryRef);\n // This will unregister the cleanup from the finalization registry when\n // the queryRef is properly retained.\n // This is mostly done to keep the FinalizationRegistry from holding too many\n // cleanup functions, as our React Native polyfill has to iterate all of them regularly.\n queryRef.retain = unregisterOnRetain(queryRef.retain, softDispose);\n}\n\ntype RetainFunction = (\n this: InternalQueryReference,\n ...args: Parameters<InternalQueryReference[\"retain\"]>\n) => ReturnType<InternalQueryReference[\"retain\"]>;\n\n// this is an individual function to avoid closing over any values more than necessary\nfunction unregisterOnRetain(\n originalRetain: RetainFunction,\n softDispose: () => void\n): RetainFunction {\n return function (...args) {\n registry.unregister(this);\n const dispose = originalRetain.apply(this, args);\n softDispose();\n return dispose;\n };\n}\n\n// this is an individual function to avoid closing over any values more than necessary\nfunction getCleanup(queryRef: InternalQueryReference) {\n const softDispose = queryRef.softRetain();\n const initialPromise = queryRef.promise;\n\n return {\n softDispose,\n delayedSoftDispose: () =>\n initialPromise.finally(softDispose).catch(() => {}),\n };\n}\n\nconst registry = new FinalizationRegistry<() => void>((cleanup) => cleanup());\n"]}
|
|
1
|
+
{"version":3,"file":"createQueryPreloader.js","sourceRoot":"","sources":["../../../src/react/query-preloader/createQueryPreloader.ts"],"names":[],"mappings":"AAYA,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,sBAAsB,EACtB,YAAY,GACb,MAAM,+BAA+B,CAAC;AAOvC,OAAO,EAAE,oBAAoB,EAAE,MAAM,6CAA6C,CAAC;AAEnF,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AA8StD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,oBAAoB,CAClC,MAAoB;IAEpB,OAAO,QAAQ,CACb,sBAAsB,EACtB,qBAAqB,EACrB,MAAM,CACP,CAAC,MAAM,CAAC,CAAC;AACZ,CAAC;AAED,MAAM,qBAAqB,GAAgC,CAAC,MAAM,EAAE,EAAE;IACpE,SAAS,YAAY,CAInB,KAA0D,EAC1D,UACgC,EAAS;QAEzC,MAAM,QAAQ,GAAG,IAAI,sBAAsB,CACzC,MAAM,CAAC,UAAU,CAAC;YAChB,GAAG,OAAO;YACV,KAAK;YACL,2BAA2B,EAAE,KAAK;SACS,CAAC,EAC9C;YACE,oBAAoB,EAClB,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,QAAQ,EAAE,oBAAoB;SAC9D,CACF,CAAC;QAEF,MAAM,OAAO,GAAG,YAAY,CAAC,QAAQ,CAGpC,CAAC;QACF,yBAAyB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAC7C,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE;QACjC,SAAS,CACP,QAAmB;YAEnB,qBAAqB,CAAC,QAAQ,CAAC,CAAC;YAChC,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,CAAC;QAC1D,CAAC;KACF,CAAC,CAAC;AACL,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,sFAAsF;AACtF,SAAS,yBAAyB,CAChC,OAAyC,EACzC,QAAgC;IAEhC,MAAM,EAAE,WAAW,EAAE,kBAAkB,EAAE,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IACjE,QAAQ,CAAC,QAAQ,CAAC,OAAO,EAAE,kBAAkB,EAAE,QAAQ,CAAC,CAAC;IACzD,uEAAuE;IACvE,qCAAqC;IACrC,6EAA6E;IAC7E,wFAAwF;IACxF,QAAQ,CAAC,MAAM,GAAG,kBAAkB,CAAC,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AACrE,CAAC;AAOD,sFAAsF;AACtF,SAAS,kBAAkB,CACzB,cAA8B,EAC9B,WAAuB;IAEvB,OAAO,UAAU,GAAG,IAAI;QACtB,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC1B,MAAM,OAAO,GAAG,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACjD,WAAW,EAAE,CAAC;QACd,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;AACJ,CAAC;AAED,sFAAsF;AACtF,SAAS,UAAU,CAAC,QAAgC;IAClD,MAAM,WAAW,GAAG,QAAQ,CAAC,UAAU,EAAE,CAAC;IAC1C,MAAM,cAAc,GAAG,QAAQ,CAAC,OAAO,CAAC;IAExC,OAAO;QACL,WAAW;QACX,kBAAkB,EAAE,GAAG,EAAE,CACvB,cAAc,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;KACtD,CAAC;AACJ,CAAC;AAED,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAa,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC","sourcesContent":["import type {\n ApolloClient,\n DefaultContext,\n DocumentNode,\n ErrorPolicy,\n OperationVariables,\n RefetchOn,\n RefetchWritePolicy,\n TypedDocumentNode,\n WatchQueryFetchPolicy,\n} from \"@apollo/client\";\nimport type { PreloadedQueryRef } from \"@apollo/client/react\";\nimport {\n assertWrappedQueryRef,\n getWrappedPromise,\n InternalQueryReference,\n wrapQueryRef,\n} from \"@apollo/client/react/internal\";\nimport type {\n NoInfer,\n OptionWithFallback,\n SignatureStyle,\n VariablesOption,\n} from \"@apollo/client/utilities/internal\";\nimport { FinalizationRegistry } from \"@apollo/client/utilities/internal/ponyfills\";\n\nimport { wrapHook } from \"../hooks/internal/index.js\";\n\nexport type PreloadQueryFetchPolicy = Extract<\n WatchQueryFetchPolicy,\n \"cache-first\" | \"network-only\" | \"no-cache\" | \"cache-and-network\"\n>;\n\nexport type PreloadQueryOptions<\n TVariables extends OperationVariables = OperationVariables,\n> = {\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#context:member} */\n context?: DefaultContext;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#errorPolicy:member} */\n errorPolicy?: ErrorPolicy;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#fetchPolicy:member} */\n fetchPolicy?: PreloadQueryFetchPolicy;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#returnPartialData:member} */\n returnPartialData?: boolean;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#refetchWritePolicy:member} */\n refetchWritePolicy?: RefetchWritePolicy;\n /** {@inheritDoc @apollo/client!QueryOptionsDocumentation#refetchOn:member} */\n refetchOn?: RefetchOn.Option;\n} & VariablesOption<TVariables>;\n\n/**\n * A function that will begin loading a query when called. It's result can be\n * read by `useReadQuery` which will suspend until the query is loaded.\n * This is useful when you want to start loading a query as early as possible\n * outside of a React component.\n *\n * @example\n *\n * ```js\n * const preloadQuery = createQueryPreloader(client);\n * const queryRef = preloadQuery(query, { variables, ...otherOptions });\n *\n * function App() {\n * return (\n * <Suspense fallback={<div>Loading</div>}>\n * <MyQuery />\n * </Suspense>\n * );\n * }\n *\n * function MyQuery() {\n * const { data } = useReadQuery(queryRef);\n *\n * // do something with `data`\n * }\n * ```\n */\nexport interface PreloadQueryFunction\n extends PreloadQueryFunction.Signatures.Evaluated {\n /**\n * A function that returns a promise that resolves when the query has finished\n * loading. The promise resolves with the `QueryReference` itself.\n *\n * @remarks\n * This method is useful for preloading queries in data loading routers, such\n * as [React Router](https://reactrouter.com/en/main) or [TanStack Router](https://tanstack.com/router),\n * to prevent routes from transitioning until the query has finished loading.\n * `data` is not exposed on the promise to discourage using the data in\n * `loader` functions and exposing it to your route components. Instead, we\n * prefer you rely on `useReadQuery` to access the data to ensure your\n * component can rerender with cache updates. If you need to access raw query\n * data, use `client.query()` directly.\n *\n * @example\n * Here's an example using React Router's `loader` function:\n *\n * ```ts\n * import { createQueryPreloader } from \"@apollo/client\";\n *\n * const preloadQuery = createQueryPreloader(client);\n *\n * export async function loader() {\n * const queryRef = preloadQuery(GET_DOGS_QUERY);\n *\n * return preloadQuery.toPromise(queryRef);\n * }\n *\n * export function RouteComponent() {\n * const queryRef = useLoaderData();\n * const { data } = useReadQuery(queryRef);\n *\n * // ...\n * }\n * ```\n */\n toPromise<TQueryRef extends PreloadedQueryRef<any, any, any>>(\n queryRef: TQueryRef\n ): Promise<TQueryRef>;\n}\n\nexport declare namespace PreloadQueryFunction {\n export interface DefaultOptions\n extends ApolloClient.DefaultOptions.WatchQuery.Calculated {}\n\n export namespace DocumentationTypes {\n /**\n * @deprecated Avoid manually specifying generics on `preloadQuery`.\n * Instead, rely on TypeScript's type inference along with a correctly typed `TypedDocumentNode` to get accurate types for your query results.\n *\n * {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface}\n */\n export interface PreloadQueryFunction_Deprecated\n extends PreloadQueryFunction {}\n }\n\n export type ResultForOptions<\n TData,\n TVariables extends OperationVariables,\n TOptions extends Record<string, never> | PreloadQueryOptions<TVariables>,\n > = TOptions extends any ?\n PreloadedQueryRef<\n TData,\n TVariables,\n ResultForOptions.States<TOptions, DefaultOptions>\n >\n : never;\n\n export namespace ResultForOptions {\n export type States<\n TOptions,\n TDefaultOptions extends DefaultOptions = DefaultOptions,\n > =\n | \"complete\"\n | \"streaming\"\n | (OptionWithFallback<TOptions, TDefaultOptions, \"errorPolicy\"> extends (\n \"none\"\n ) ?\n never\n : \"empty\")\n | (OptionWithFallback<\n TOptions,\n TDefaultOptions,\n \"returnPartialData\"\n > extends false ?\n never\n : \"partial\");\n }\n\n export namespace Signatures {\n export interface Classic {\n // _INFERENCE_ONLY_DO_NOT_SPECIFY is used to distinguish between inferred\n // generics arguments and explicit generic arguments so that we can\n // provide a `@deprecated` signature for explicit generic arguments. As\n // soon as a user provides a generic arg (e.g. preloadQuery<TData>(query))`,\n // the overload falls through to the overloads without\n // _INFERENCE_ONLY_DO_NOT_SPECIFY.\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <\n TData,\n TVariables extends OperationVariables,\n _INFERENCE_ONLY_DO_NOT_SPECIFY extends \"inferred\",\n >(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n ): PreloadedQueryRef<\n TData,\n TVariables,\n \"complete\" | \"streaming\" | \"partial\" | \"empty\"\n >;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <\n TData,\n TVariables extends OperationVariables,\n _INFERENCE_ONLY_DO_NOT_SPECIFY extends \"inferred\",\n >(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n errorPolicy: \"ignore\" | \"all\";\n }\n ): PreloadedQueryRef<\n TData,\n TVariables,\n \"complete\" | \"streaming\" | \"empty\"\n >;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <\n TData,\n TVariables extends OperationVariables,\n _INFERENCE_ONLY_DO_NOT_SPECIFY extends \"inferred\",\n >(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n returnPartialData: true;\n }\n ): PreloadedQueryRef<\n TData,\n TVariables,\n \"complete\" | \"streaming\" | \"partial\"\n >;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <\n TData,\n TVariables extends OperationVariables,\n _INFERENCE_ONLY_DO_NOT_SPECIFY extends \"inferred\",\n >(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n ...[options]: {} extends TVariables ?\n [options?: PreloadQueryOptions<NoInfer<TVariables>>]\n : [options: PreloadQueryOptions<NoInfer<TVariables>>]\n ): PreloadedQueryRef<TData, TVariables, \"complete\" | \"streaming\">;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction.DocumentationTypes.PreloadQueryFunction_Deprecated:interface} */\n <TData, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n ): PreloadedQueryRef<\n TData,\n TVariables,\n \"complete\" | \"streaming\" | \"partial\" | \"empty\"\n >;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction.DocumentationTypes.PreloadQueryFunction_Deprecated:interface} */\n <TData, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n errorPolicy: \"ignore\" | \"all\";\n }\n ): PreloadedQueryRef<\n TData,\n TVariables,\n \"complete\" | \"streaming\" | \"empty\"\n >;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction.DocumentationTypes.PreloadQueryFunction_Deprecated:interface} */\n <TData, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> & {\n returnPartialData: true;\n }\n ): PreloadedQueryRef<\n TData,\n TVariables,\n \"complete\" | \"streaming\" | \"partial\"\n >;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction.DocumentationTypes.PreloadQueryFunction_Deprecated:interface} */\n <TData, TVariables extends OperationVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n ...[options]: {} extends TVariables ?\n [options?: PreloadQueryOptions<NoInfer<TVariables>>]\n : [options: PreloadQueryOptions<NoInfer<TVariables>>]\n ): PreloadedQueryRef<TData, TVariables, \"complete\" | \"streaming\">;\n }\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n export interface Modern {\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <\n TData,\n TVariables extends OperationVariables,\n // this overload should never be manually defined, it should always be inferred\n TOptions extends never,\n >(\n query: {} extends TVariables ?\n DocumentNode | TypedDocumentNode<TData, TVariables>\n : // this overload should only be accessible if all `TVariables` are optional\n never\n ): PreloadQueryFunction.ResultForOptions<\n TData,\n TVariables,\n Record<string, never>\n >;\n\n /** {@inheritDoc @apollo/client/react!PreloadQueryFunction:interface} */\n <\n TData,\n TVariables extends OperationVariables,\n // this overload should never be manually defined, it should always be inferred\n TOptions extends PreloadQueryOptions<NoInfer<TVariables>> &\n VariablesOption<\n TVariables & {\n [K in Exclude<\n keyof TOptions[\"variables\"],\n keyof TVariables\n >]?: never;\n }\n >,\n >(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n ...[options]: {} extends TVariables ? [options?: TOptions]\n : [options: TOptions]\n ): PreloadQueryFunction.ResultForOptions<TData, TVariables, TOptions>;\n }\n\n export type Evaluated = SignatureStyle extends \"classic\" ? Classic : Modern;\n }\n}\n\n/**\n * A higher order function that returns a `preloadQuery` function which\n * can be used to begin loading a query with the given `client`. This is useful\n * when you want to start loading a query as early as possible outside of a\n * React component.\n *\n * > Refer to the [Suspense - Initiating queries outside React](https://www.apollographql.com/docs/react/data/suspense#initiating-queries-outside-react) section for a more in-depth overview.\n *\n * @param client - The `ApolloClient` instance that will be used to load queries\n * from the returned `preloadQuery` function.\n * @returns The `preloadQuery` function.\n *\n * @example\n *\n * ```js\n * const preloadQuery = createQueryPreloader(client);\n * ```\n */\nexport function createQueryPreloader(\n client: ApolloClient\n): PreloadQueryFunction {\n return wrapHook(\n \"createQueryPreloader\",\n _createQueryPreloader,\n client\n )(client);\n}\n\nconst _createQueryPreloader: typeof createQueryPreloader = (client) => {\n function preloadQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n >(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: PreloadQueryOptions<NoInfer<TVariables>> &\n VariablesOption<TVariables> = {} as any\n ): PreloadedQueryRef<TData, TVariables> {\n const queryRef = new InternalQueryReference(\n client.watchQuery({\n ...options,\n query,\n notifyOnNetworkStatusChange: false,\n } as ApolloClient.WatchQueryOptions<any, any>),\n {\n autoDisposeTimeoutMs:\n client.defaultOptions.react?.suspense?.autoDisposeTimeoutMs,\n }\n );\n\n const wrapped = wrapQueryRef(queryRef) as unknown as PreloadedQueryRef<\n TData,\n TVariables\n >;\n softRetainWhileReferenced(wrapped, queryRef);\n return wrapped;\n }\n\n return Object.assign(preloadQuery, {\n toPromise<TQueryRef extends PreloadedQueryRef<any, any, any>>(\n queryRef: TQueryRef\n ) {\n assertWrappedQueryRef(queryRef);\n return getWrappedPromise(queryRef).then(() => queryRef);\n },\n });\n};\n\n/**\n * Soft-retains the underlying `InternalQueryReference` while the `PreloadedQueryRef`\n * is still reachable.\n * When the `PreloadedQueryRef` is garbage collected, the soft retain is\n * disposed of, but only after the initial query has finished loading.\n * Once the `InternalQueryReference` is properly retained, the check for garbage\n * collection is unregistered and the soft retain is disposed of immediately.\n */\n// this is an individual function to avoid closing over any values more than necessary\nfunction softRetainWhileReferenced(\n wrapped: PreloadedQueryRef<any, any, any>,\n queryRef: InternalQueryReference\n) {\n const { softDispose, delayedSoftDispose } = getCleanup(queryRef);\n registry.register(wrapped, delayedSoftDispose, queryRef);\n // This will unregister the cleanup from the finalization registry when\n // the queryRef is properly retained.\n // This is mostly done to keep the FinalizationRegistry from holding too many\n // cleanup functions, as our React Native polyfill has to iterate all of them regularly.\n queryRef.retain = unregisterOnRetain(queryRef.retain, softDispose);\n}\n\ntype RetainFunction = (\n this: InternalQueryReference,\n ...args: Parameters<InternalQueryReference[\"retain\"]>\n) => ReturnType<InternalQueryReference[\"retain\"]>;\n\n// this is an individual function to avoid closing over any values more than necessary\nfunction unregisterOnRetain(\n originalRetain: RetainFunction,\n softDispose: () => void\n): RetainFunction {\n return function (...args) {\n registry.unregister(this);\n const dispose = originalRetain.apply(this, args);\n softDispose();\n return dispose;\n };\n}\n\n// this is an individual function to avoid closing over any values more than necessary\nfunction getCleanup(queryRef: InternalQueryReference) {\n const softDispose = queryRef.softRetain();\n const initialPromise = queryRef.promise;\n\n return {\n softDispose,\n delayedSoftDispose: () =>\n initialPromise.finally(softDispose).catch(() => {}),\n };\n}\n\nconst registry = new FinalizationRegistry<() => void>((cleanup) => cleanup());\n"]}
|
package/version.js
CHANGED