UNPKG

19.3 kBTypeScriptView Raw
1import type { DocumentNode } from "graphql";
2import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
3import type { FetchResult } from "../link/core/index.js";
4import type { DefaultContext, MutationQueryReducersMap, OperationVariables, MutationUpdaterFunction, OnQueryUpdated, InternalRefetchQueriesInclude } from "./types.js";
5import type { ApolloCache } from "../cache/index.js";
6import type { ObservableQuery } from "./ObservableQuery.js";
7import type { IgnoreModifier } from "../cache/core/types/common.js";
8/**
9 * fetchPolicy determines where the client may return a result from. The options are:
10 * - cache-first (default): return result from cache. Only fetch from network if cached result is not available.
11 * - cache-and-network: return result from cache first (if it exists), then return network result once it's available.
12 * - cache-only: return result from cache if available, fail otherwise.
13 * - no-cache: return result from network, fail if network call doesn't succeed, don't save to cache
14 * - network-only: return result from network, fail if network call doesn't succeed, save to cache
15 * - standby: only for queries that aren't actively watched, but should be available for refetch and updateQueries.
16 */
17export type FetchPolicy = "cache-first" | "network-only" | "cache-only" | "no-cache" | "standby";
18export type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";
19export type MutationFetchPolicy = Extract<FetchPolicy, "network-only" | "no-cache">;
20export type RefetchWritePolicy = "merge" | "overwrite";
21/**
22 * errorPolicy determines the level of events for errors in the execution result. The options are:
23 * - none (default): any errors from the request are treated like runtime errors and the observable is stopped
24 * - ignore: errors from the request do not stop the observable, but also don't call `next`
25 * - all: errors are treated like data and will notify observables
26 */
27export type ErrorPolicy = "none" | "ignore" | "all";
28/**
29 * Query options.
30 */
31export interface QueryOptions<TVariables = OperationVariables, TData = any> {
32 /**
33 * A GraphQL query string parsed into an AST with the gql template literal.
34 *
35 * @docGroup
36 *
37 * 1. Operation options
38 */
39 query: DocumentNode | TypedDocumentNode<TData, TVariables>;
40 /**
41 * An object containing all of the GraphQL variables your query requires to execute.
42 *
43 * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
44 *
45 * @docGroup
46 *
47 * 1. Operation options
48 */
49 variables?: TVariables;
50 /**
51 * Specifies how the query handles a response that returns both GraphQL errors and partial results.
52 *
53 * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
54 *
55 * The default value is `none`, meaning that the query result includes error details but not partial results.
56 *
57 * @docGroup
58 *
59 * 1. Operation options
60 */
61 errorPolicy?: ErrorPolicy;
62 /**
63 * 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.
64 *
65 * @docGroup
66 *
67 * 2. Networking options
68 */
69 context?: DefaultContext;
70 /**
71 * 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).
72 *
73 * For details, see [Setting a fetch policy](https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy).
74 *
75 * The default value is `cache-first`.
76 *
77 * @docGroup
78 *
79 * 3. Caching options
80 */
81 fetchPolicy?: FetchPolicy;
82 /**
83 * Specifies the interval (in milliseconds) at which the query polls for updated results.
84 *
85 * The default value is `0` (no polling).
86 *
87 * @docGroup
88 *
89 * 2. Networking options
90 */
91 pollInterval?: number;
92 /**
93 * If `true`, the in-progress query's associated component re-renders whenever the network status changes or a network error occurs.
94 *
95 * The default value is `false`.
96 *
97 * @docGroup
98 *
99 * 2. Networking options
100 */
101 notifyOnNetworkStatusChange?: boolean;
102 /**
103 * If `true`, the query can return partial results from the cache if the cache doesn't contain results for all queried fields.
104 *
105 * The default value is `false`.
106 *
107 * @docGroup
108 *
109 * 3. Caching options
110 */
111 returnPartialData?: boolean;
112 /**
113 * If `true`, causes a query refetch if the query result is detected as partial.
114 *
115 * The default value is `false`.
116 *
117 * @deprecated
118 *
119 * Setting this option is unnecessary in Apollo Client 3, thanks to a more consistent application of fetch policies. It might be removed in a future release.
120 */
121 partialRefetch?: boolean;
122 /**
123 * Whether to canonize cache results before returning them. Canonization takes some extra time, but it speeds up future deep equality comparisons. Defaults to false.
124 *
125 * @deprecated
126 *
127 * 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.
128 */
129 canonizeResults?: boolean;
130}
131/**
132 * Watched query options.
133 */
134export interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
135 /**
136 * A GraphQL query string parsed into an AST with the gql template literal.
137 *
138 * @docGroup
139 *
140 * 1. Operation options
141 */
142 query: DocumentNode | TypedDocumentNode<TData, TVariables>;
143}
144export interface SharedWatchQueryOptions<TVariables extends OperationVariables, TData> {
145 /**
146 * 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).
147 *
148 * For details, see [Setting a fetch policy](https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy).
149 *
150 * The default value is `cache-first`.
151 *
152 * @docGroup
153 *
154 * 3. Caching options
155 */
156 fetchPolicy?: WatchQueryFetchPolicy;
157 /**
158 * Specifies the `FetchPolicy` to be used after this query has completed.
159 *
160 * @docGroup
161 *
162 * 3. Caching options
163 */
164 nextFetchPolicy?: WatchQueryFetchPolicy | ((this: WatchQueryOptions<TVariables, TData>, currentFetchPolicy: WatchQueryFetchPolicy, context: NextFetchPolicyContext<TData, TVariables>) => WatchQueryFetchPolicy);
165 /**
166 * Defaults to the initial value of options.fetchPolicy, but can be explicitly configured to specify the WatchQueryFetchPolicy to revert back to whenever variables change (unless nextFetchPolicy intervenes).
167 *
168 * @docGroup
169 *
170 * 3. Caching options
171 */
172 initialFetchPolicy?: WatchQueryFetchPolicy;
173 /**
174 * 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.
175 *
176 * @docGroup
177 *
178 * 3. Caching options
179 */
180 refetchWritePolicy?: RefetchWritePolicy;
181 /**
182 * An object containing all of the GraphQL variables your query requires to execute.
183 *
184 * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
185 *
186 * @docGroup
187 *
188 * 1. Operation options
189 */
190 variables?: TVariables;
191 /**
192 * Specifies how the query handles a response that returns both GraphQL errors and partial results.
193 *
194 * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
195 *
196 * The default value is `none`, meaning that the query result includes error details but not partial results.
197 *
198 * @docGroup
199 *
200 * 1. Operation options
201 */
202 errorPolicy?: ErrorPolicy;
203 /**
204 * 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.
205 *
206 * @docGroup
207 *
208 * 2. Networking options
209 */
210 context?: DefaultContext;
211 /**
212 * Specifies the interval (in milliseconds) at which the query polls for updated results.
213 *
214 * The default value is `0` (no polling).
215 *
216 * @docGroup
217 *
218 * 2. Networking options
219 */
220 pollInterval?: number;
221 /**
222 * If `true`, the in-progress query's associated component re-renders whenever the network status changes or a network error occurs.
223 *
224 * The default value is `false`.
225 *
226 * @docGroup
227 *
228 * 2. Networking options
229 */
230 notifyOnNetworkStatusChange?: boolean;
231 /**
232 * If `true`, the query can return partial results from the cache if the cache doesn't contain results for all queried fields.
233 *
234 * The default value is `false`.
235 *
236 * @docGroup
237 *
238 * 3. Caching options
239 */
240 returnPartialData?: boolean;
241 /**
242 * If `true`, causes a query refetch if the query result is detected as partial.
243 *
244 * The default value is `false`.
245 *
246 * @deprecated
247 *
248 * Setting this option is unnecessary in Apollo Client 3, thanks to a more consistent application of fetch policies. It might be removed in a future release.
249 */
250 partialRefetch?: boolean;
251 /**
252 * Whether to canonize cache results before returning them. Canonization takes some extra time, but it speeds up future deep equality comparisons. Defaults to false.
253 *
254 * @deprecated
255 *
256 * 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.
257 */
258 canonizeResults?: boolean;
259 /**
260 * A callback function that's called whenever a refetch attempt occurs while polling. If the function returns `true`, the refetch is skipped and not reattempted until the next poll interval.
261 *
262 * @docGroup
263 *
264 * 2. Networking options
265 */
266 skipPollAttempt?: () => boolean;
267}
268export interface NextFetchPolicyContext<TData, TVariables extends OperationVariables> {
269 reason: "after-fetch" | "variables-changed";
270 observable: ObservableQuery<TData, TVariables>;
271 options: WatchQueryOptions<TVariables, TData>;
272 initialFetchPolicy: WatchQueryFetchPolicy;
273}
274export interface FetchMoreQueryOptions<TVariables, TData = any> {
275 /**
276 * A GraphQL query string parsed into an AST with the gql template literal.
277 *
278 * @docGroup
279 *
280 * 1. Operation options
281 */
282 query?: DocumentNode | TypedDocumentNode<TData, TVariables>;
283 /**
284 * An object containing all of the GraphQL variables your query requires to execute.
285 *
286 * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
287 *
288 * @docGroup
289 *
290 * 1. Operation options
291 */
292 variables?: Partial<TVariables>;
293 context?: DefaultContext;
294}
295export type UpdateQueryFn<TData = any, TSubscriptionVariables = OperationVariables, TSubscriptionData = TData> = (previousQueryResult: TData, options: {
296 subscriptionData: {
297 data: TSubscriptionData;
298 };
299 variables?: TSubscriptionVariables;
300}) => TData;
301export type SubscribeToMoreOptions<TData = any, TSubscriptionVariables = OperationVariables, TSubscriptionData = TData> = {
302 document: DocumentNode | TypedDocumentNode<TSubscriptionData, TSubscriptionVariables>;
303 variables?: TSubscriptionVariables;
304 updateQuery?: UpdateQueryFn<TData, TSubscriptionVariables, TSubscriptionData>;
305 onError?: (error: Error) => void;
306 context?: DefaultContext;
307};
308export interface SubscriptionOptions<TVariables = OperationVariables, TData = any> {
309 /**
310 * A GraphQL document, often created with `gql` from the `graphql-tag` package, that contains a single subscription inside of it.
311 */
312 query: DocumentNode | TypedDocumentNode<TData, TVariables>;
313 /**
314 * An object containing all of the variables your subscription needs to execute
315 */
316 variables?: TVariables;
317 /**
318 * How you want your component to interact with the Apollo cache. For details, see [Setting a fetch policy](https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy).
319 */
320 fetchPolicy?: FetchPolicy;
321 /**
322 * Specifies the `ErrorPolicy` to be used for this operation
323 */
324 errorPolicy?: ErrorPolicy;
325 /**
326 * Shared context between your component and your network interface (Apollo Link).
327 */
328 context?: DefaultContext;
329 /**
330 * Shared context between your component and your network interface (Apollo Link).
331 */
332 extensions?: Record<string, any>;
333}
334export interface MutationBaseOptions<TData = any, TVariables = OperationVariables, TContext = DefaultContext, TCache extends ApolloCache<any> = ApolloCache<any>> {
335 /**
336 * By providing either an object or a callback function that, when invoked after a mutation, allows you to return optimistic data and optionally skip updates via the `IGNORE` sentinel object, Apollo Client caches this temporary (and potentially incorrect) response until the mutation completes, enabling more responsive UI updates.
337 *
338 * For more information, see [Optimistic mutation results](https://www.apollographql.com/docs/react/performance/optimistic-ui/).
339 *
340 * @docGroup
341 *
342 * 3. Caching options
343 */
344 optimisticResponse?: TData | ((vars: TVariables, { IGNORE }: {
345 IGNORE: IgnoreModifier;
346 }) => TData | IgnoreModifier);
347 /**
348 * A `MutationQueryReducersMap`, which is map from query names to mutation query reducers. Briefly, this map defines how to incorporate the results of the mutation into the results of queries that are currently being watched by your application.
349 */
350 updateQueries?: MutationQueryReducersMap<TData>;
351 /**
352 * An array (or a function that _returns_ an array) that specifies which queries you want to refetch after the mutation occurs.
353 *
354 * Each array value can be either:
355 *
356 * - An object containing the `query` to execute, along with any `variables`
357 *
358 * - A string indicating the operation name of the query to refetch
359 *
360 * @docGroup
361 *
362 * 1. Operation options
363 */
364 refetchQueries?: ((result: FetchResult<TData>) => InternalRefetchQueriesInclude) | InternalRefetchQueriesInclude;
365 /**
366 * If `true`, makes sure all queries included in `refetchQueries` are completed before the mutation is considered complete.
367 *
368 * The default value is `false` (queries are refetched asynchronously).
369 *
370 * @docGroup
371 *
372 * 1. Operation options
373 */
374 awaitRefetchQueries?: boolean;
375 /**
376 * A function used to update the Apollo Client cache after the mutation completes.
377 *
378 * For more information, see [Updating the cache after a mutation](https://www.apollographql.com/docs/react/data/mutations#updating-the-cache-after-a-mutation).
379 *
380 * @docGroup
381 *
382 * 3. Caching options
383 */
384 update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
385 /**
386 * Optional callback for intercepting queries whose cache data has been updated by the mutation, as well as any queries specified in the `refetchQueries: [...]` list passed to `client.mutate`.
387 *
388 * Returning a `Promise` from `onQueryUpdated` will cause the final mutation `Promise` to await the returned `Promise`. Returning `false` causes the query to be ignored.
389 *
390 * @docGroup
391 *
392 * 1. Operation options
393 */
394 onQueryUpdated?: OnQueryUpdated<any>;
395 /**
396 * Specifies how the mutation handles a response that returns both GraphQL errors and partial results.
397 *
398 * For details, see [GraphQL error policies](https://www.apollographql.com/docs/react/data/error-handling/#graphql-error-policies).
399 *
400 * The default value is `none`, meaning that the mutation result includes error details but _not_ partial results.
401 *
402 * @docGroup
403 *
404 * 1. Operation options
405 */
406 errorPolicy?: ErrorPolicy;
407 /**
408 * An object containing all of the GraphQL variables your mutation requires to execute.
409 *
410 * Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.
411 *
412 * @docGroup
413 *
414 * 1. Operation options
415 */
416 variables?: TVariables;
417 /**
418 * 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.
419 *
420 * @docGroup
421 *
422 * 2. Networking options
423 */
424 context?: TContext;
425}
426export interface MutationOptions<TData = any, TVariables = OperationVariables, TContext = DefaultContext, TCache extends ApolloCache<any> = ApolloCache<any>> extends MutationSharedOptions<TData, TVariables, TContext, TCache> {
427 /**
428 * A GraphQL document, often created with `gql` from the `graphql-tag` package, that contains a single mutation inside of it.
429 *
430 * @docGroup
431 *
432 * 1. Operation options
433 */
434 mutation: DocumentNode | TypedDocumentNode<TData, TVariables>;
435}
436export interface MutationSharedOptions<TData = any, TVariables = OperationVariables, TContext = DefaultContext, TCache extends ApolloCache<any> = ApolloCache<any>> extends MutationBaseOptions<TData, TVariables, TContext, TCache> {
437 /**
438 * Provide `no-cache` if the mutation's result should _not_ be written to the Apollo Client cache.
439 *
440 * The default value is `network-only` (which means the result _is_ written to the cache).
441 *
442 * Unlike queries, mutations _do not_ support [fetch policies](https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy) besides `network-only` and `no-cache`.
443 *
444 * @docGroup
445 *
446 * 3. Caching options
447 */
448 fetchPolicy?: MutationFetchPolicy;
449 /**
450 * To avoid retaining sensitive information from mutation root field arguments, Apollo Client v3.4+ automatically clears any `ROOT_MUTATION` fields from the cache after each mutation finishes. If you need this information to remain in the cache, you can prevent the removal by passing `keepRootFields: true` to the mutation. `ROOT_MUTATION` result data are also passed to the mutation `update` function, so we recommend obtaining the results that way, rather than using this option, if possible.
451 */
452 keepRootFields?: boolean;
453}
454//# sourceMappingURL=watchQueryOptions.d.ts.map
\No newline at end of file