UNPKG

11.7 kBTypeScriptView Raw
1import type { ApolloClient, DefaultContext, DocumentNode, ErrorPolicy, OperationVariables, RefetchWritePolicy, TypedDocumentNode, WatchQueryFetchPolicy } from "../../core/index.js";
2import type { DeepPartial, OnlyRequiredProperties } from "../../utilities/index.js";
3import type { PreloadedQueryRef } from "../internal/index.js";
4import type { NoInfer } from "../index.js";
5type VariablesOption<TVariables extends OperationVariables> = [
6 TVariables
7] extends [never] ? {
8 /**
9 * An object containing all of the GraphQL variables your query requires to execute.
10 *
11 * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
12 *
13 * @docGroup
14 *
15 * 1. Operation options
16 */
17 variables?: Record<string, never>;
18} : {} extends OnlyRequiredProperties<TVariables> ? {
19 /**
20 * An object containing all of the GraphQL variables your query requires to execute.
21 *
22 * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
23 *
24 * @docGroup
25 *
26 * 1. Operation options
27 */
28 variables?: TVariables;
29} : {
30 /**
31 * An object containing all of the GraphQL variables your query requires to execute.
32 *
33 * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
34 *
35 * @docGroup
36 *
37 * 1. Operation options
38 */
39 variables: TVariables;
40};
41export type PreloadQueryFetchPolicy = Extract<WatchQueryFetchPolicy, "cache-first" | "network-only" | "no-cache" | "cache-and-network">;
42export type PreloadQueryOptions<TVariables extends OperationVariables = OperationVariables> = {
43 /**
44 * Whether to canonize cache results before returning them. Canonization takes some extra time, but it speeds up future deep equality comparisons. Defaults to false.
45 *
46 * @deprecated
47 *
48 * Using `canonizeResults` can result in memory leaks so we generally do not recommend using this option anymore. A future version of Apollo Client will contain a similar feature without the risk of memory leaks.
49 */
50 canonizeResults?: boolean;
51 /**
52 * If you're using [Apollo Link](https://www.apollographql.com/docs/react/api/link/introduction/), this object is the initial value of the `context` object that's passed along your link chain.
53 *
54 * @docGroup
55 *
56 * 2. Networking options
57 */
58 context?: DefaultContext;
59 /**
60 * Specifies how the query handles a response that returns both GraphQL errors and partial results.
61 *
62 * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
63 *
64 * The default value is `none`, meaning that the query result includes error details but not partial results.
65 *
66 * @docGroup
67 *
68 * 1. Operation options
69 */
70 errorPolicy?: ErrorPolicy;
71 /**
72 * Specifies how the query interacts with the Apollo Client cache during execution (for example, whether it checks the cache for results before sending a request to the server).
73 *
74 * For details, see [Setting a fetch policy](https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy).
75 *
76 * The default value is `cache-first`.
77 *
78 * @docGroup
79 *
80 * 3. Caching options
81 */
82 fetchPolicy?: PreloadQueryFetchPolicy;
83 /**
84 * If `true`, the query can return partial results from the cache if the cache doesn't contain results for all queried fields.
85 *
86 * The default value is `false`.
87 *
88 * @docGroup
89 *
90 * 3. Caching options
91 */
92 returnPartialData?: boolean;
93 /**
94 * Specifies whether a `NetworkStatus.refetch` operation should merge incoming field data with existing data, or overwrite the existing data. Overwriting is probably preferable, but merging is currently the default behavior, for backwards compatibility with Apollo Client 3.x.
95 *
96 * @docGroup
97 *
98 * 3. Caching options
99 */
100 refetchWritePolicy?: RefetchWritePolicy;
101} & VariablesOption<TVariables>;
102type PreloadQueryOptionsArg<TVariables extends OperationVariables, TOptions = unknown> = [TVariables] extends [never] ? [
103 options?: PreloadQueryOptions<never> & TOptions
104] : {} extends OnlyRequiredProperties<TVariables> ? [
105 options?: PreloadQueryOptions<NoInfer<TVariables>> & Omit<TOptions, "variables">
106] : [
107 options: PreloadQueryOptions<NoInfer<TVariables>> & Omit<TOptions, "variables">
108];
109/**
110 * A function that will begin loading a query when called. It's result can be
111 * read by `useReadQuery` which will suspend until the query is loaded.
112 * This is useful when you want to start loading a query as early as possible
113 * outside of a React component.
114 *
115 * @example
116 * ```js
117 * const preloadQuery = createQueryPreloader(client);
118 * const queryRef = preloadQuery(query, { variables, ...otherOptions });
119 *
120 * function App() {
121 * return (
122 * <Suspense fallback={<div>Loading</div>}>
123 * <MyQuery />
124 * </Suspense>
125 * );
126 * }
127 *
128 * function MyQuery() {
129 * const { data } = useReadQuery(queryRef);
130 *
131 * // do something with `data`
132 * }
133 * ```
134 */
135export interface PreloadQueryFunction {
136 /**
137 * A function that will begin loading a query when called. It's result can be read by `useReadQuery` which will suspend until the query is loaded. This is useful when you want to start loading a query as early as possible outside of a React component.
138 *
139 * @example
140 * ```js
141 * const preloadQuery = createQueryPreloader(client);
142 * const queryRef = preloadQuery(query, { variables, ...otherOptions });
143 *
144 * function App() {
145 * return (
146 * <Suspense fallback={<div>Loading</div>}>
147 * <MyQuery />
148 * </Suspense>
149 * );
150 * }
151 *
152 * function MyQuery() {
153 * const { data } = useReadQuery(queryRef);
154 *
155 * // do something with `data`
156 * }
157 * ```
158 */
159 <TData, TVariables extends OperationVariables, TOptions extends Omit<PreloadQueryOptions, "variables">>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: PreloadQueryOptionsArg<NoInfer<TVariables>, TOptions>): PreloadedQueryRef<TOptions["errorPolicy"] extends "ignore" | "all" ? TOptions["returnPartialData"] extends true ? DeepPartial<TData> | undefined : TData | undefined : TOptions["returnPartialData"] extends true ? DeepPartial<TData> : TData, TVariables>;
160 /**
161 * A function that will begin loading a query when called. It's result can be read by `useReadQuery` which will suspend until the query is loaded. This is useful when you want to start loading a query as early as possible outside of a React component.
162 *
163 * @example
164 * ```js
165 * const preloadQuery = createQueryPreloader(client);
166 * const queryRef = preloadQuery(query, { variables, ...otherOptions });
167 *
168 * function App() {
169 * return (
170 * <Suspense fallback={<div>Loading</div>}>
171 * <MyQuery />
172 * </Suspense>
173 * );
174 * }
175 *
176 * function MyQuery() {
177 * const { data } = useReadQuery(queryRef);
178 *
179 * // do something with `data`
180 * }
181 * ```
182 */
183 <TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
184 returnPartialData: true;
185 errorPolicy: "ignore" | "all";
186 }): PreloadedQueryRef<DeepPartial<TData> | undefined, TVariables>;
187 /**
188 * A function that will begin loading a query when called. It's result can be read by `useReadQuery` which will suspend until the query is loaded. This is useful when you want to start loading a query as early as possible outside of a React component.
189 *
190 * @example
191 * ```js
192 * const preloadQuery = createQueryPreloader(client);
193 * const queryRef = preloadQuery(query, { variables, ...otherOptions });
194 *
195 * function App() {
196 * return (
197 * <Suspense fallback={<div>Loading</div>}>
198 * <MyQuery />
199 * </Suspense>
200 * );
201 * }
202 *
203 * function MyQuery() {
204 * const { data } = useReadQuery(queryRef);
205 *
206 * // do something with `data`
207 * }
208 * ```
209 */
210 <TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
211 errorPolicy: "ignore" | "all";
212 }): PreloadedQueryRef<TData | undefined, TVariables>;
213 /**
214 * A function that will begin loading a query when called. It's result can be read by `useReadQuery` which will suspend until the query is loaded. This is useful when you want to start loading a query as early as possible outside of a React component.
215 *
216 * @example
217 * ```js
218 * const preloadQuery = createQueryPreloader(client);
219 * const queryRef = preloadQuery(query, { variables, ...otherOptions });
220 *
221 * function App() {
222 * return (
223 * <Suspense fallback={<div>Loading</div>}>
224 * <MyQuery />
225 * </Suspense>
226 * );
227 * }
228 *
229 * function MyQuery() {
230 * const { data } = useReadQuery(queryRef);
231 *
232 * // do something with `data`
233 * }
234 * ```
235 */
236 <TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
237 returnPartialData: true;
238 }): PreloadedQueryRef<DeepPartial<TData>, TVariables>;
239 /**
240 * A function that will begin loading a query when called. It's result can be read by `useReadQuery` which will suspend until the query is loaded. This is useful when you want to start loading a query as early as possible outside of a React component.
241 *
242 * @example
243 * ```js
244 * const preloadQuery = createQueryPreloader(client);
245 * const queryRef = preloadQuery(query, { variables, ...otherOptions });
246 *
247 * function App() {
248 * return (
249 * <Suspense fallback={<div>Loading</div>}>
250 * <MyQuery />
251 * </Suspense>
252 * );
253 * }
254 *
255 * function MyQuery() {
256 * const { data } = useReadQuery(queryRef);
257 *
258 * // do something with `data`
259 * }
260 * ```
261 */
262 <TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: PreloadQueryOptionsArg<NoInfer<TVariables>>): PreloadedQueryRef<TData, TVariables>;
263}
264/**
265 * A higher order function that returns a `preloadQuery` function which
266 * can be used to begin loading a query with the given `client`. This is useful
267 * when you want to start loading a query as early as possible outside of a
268 * React component.
269 *
270 * > 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.
271 *
272 * @param client - The `ApolloClient` instance that will be used to load queries
273 * from the returned `preloadQuery` function.
274 * @returns The `preloadQuery` function.
275 *
276 * @example
277 * ```js
278 * const preloadQuery = createQueryPreloader(client);
279 * ```
280 * @since 3.9.0
281 */
282export declare function createQueryPreloader(client: ApolloClient<any>): PreloadQueryFunction;
283export {};
284//# sourceMappingURL=createQueryPreloader.d.ts.map
\No newline at end of file