Skip to content

Commit ca541ae

Browse files
committed
client.refetchQueries section
1 parent 0a98862 commit ca541ae

File tree

3 files changed

+43
-71
lines changed

3 files changed

+43
-71
lines changed

docs/shared/MdxProvidedComponents.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ declare const Example: React.FC<{
5757
declare const FunctionDetails: React.FC<{
5858
/** canonical reference of the DocBlock to include. */
5959
canonicalReference: string;
60+
/** can be used to override the heading, which would usually just be the name of the function. This is useful if the interface name only makes sense in the context of a class, such as `client.refetchResult` or `ApolloClient.refetchResult` instead of `refetchResult`, which would be too generic */
61+
displayName?: string;
6062
/** describes the nesting depth of the main heading for the markdown generated by this codeblock */
6163
headingLevel: number;
6264
/** can be set to `false` to hide the result and parameters sections in the generated Docs page - this is useful if the parameters are not well documented */

docs/source/data/refetching.mdx

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Refetching queries in Apollo Client
33
---
44

5+
{/** @import {MDXProvidedComponents} from '../../shared/MdxProvidedComponents.js' */}
56

67
Apollo Client allows you to make local modifications to your GraphQL data by [updating the cache](./mutations/#updating-the-cache-directly), but sometimes it's more straightforward to update your client-side GraphQL data by refetching queries from the server.
78

@@ -13,73 +14,30 @@ Refetching is especially common after a mutation, so [mutate functions](./mutati
1314

1415
To selectively refetch queries _outside_ of a mutation, you instead use the `refetchQueries` method of `ApolloClient`, which is documented here.
1516

16-
## `client.refetchQueries`
17-
18-
### Refetch options
19-
20-
<InterfaceDetails
21-
canonicalReference="@apollo/client!RefetchQueriesOptions:interface"
22-
headingLevel={4}
23-
/>
24-
25-
### Refetch results
26-
27-
<!-- TODO -->
17+
<FunctionDetails
18+
canonicalReference="@apollo/client!ApolloClient#refetchQueries:member(1)"
19+
headingLevel={2}
20+
displayName="client.refetchQueries"
21+
>
2822

2923
The `client.refetchQueries` method collects the `TResult` results returned by `onQueryUpdated`, defaulting to `TResult = Promise<ApolloQueryResult<any>>` if `onQueryUpdated` is not provided. It combines those results into a single `Promise<TResolved[]>` using `Promise.all(results)`.
3024

31-
> Thanks to the `Promise`-unwrapping behavior of `Promise.all`, this `TResolved` type is often the same type as `TResult`, except when `TResult` is a `PromiseLike<TResolved>` or a `boolean`.
25+
<Note>
26+
Thanks to the `Promise`-unwrapping behavior of `Promise.all`, this `TResolved` type is often the same type as `TResult`, except when `TResult` is a `PromiseLike<TResolved>` or a `boolean`.
27+
</Note>
3228

3329
The returned `Promise` object has two other useful properties:
3430

35-
<table class="field-table api-ref">
36-
<thead>
37-
<tr>
38-
<th>Name /<br/>Type</th>
39-
<th>Description</th>
40-
</tr>
41-
</thead>
42-
<tbody>
43-
44-
<tr>
45-
<td>
46-
47-
###### `queries`
48-
49-
`ObservableQuery[]`
50-
</td>
51-
52-
<td>
53-
54-
An array of `ObservableQuery` objects that were refetched.
55-
56-
</td>
57-
</tr>
58-
59-
<tr>
60-
<td>
61-
62-
###### `results`
63-
64-
`TResult[]`
65-
</td>
66-
67-
<td>
68-
69-
An array of results that were either returned by `onQueryUpdated`, or provided by default in the absence of `onQueryUpdated`, including pending promises.
70-
71-
If `onQueryUpdated` returns `false` for a given query, no result is provided for that query.
72-
73-
If `onQueryUpdated` returns `true`, the resulting `Promise<ApolloQueryResult<any>>` is included in the `results` array instead of `true`.
74-
75-
</td>
76-
</tr>
77-
78-
</tbody>
79-
</table>
31+
<InterfaceDetails
32+
canonicalReference="@apollo/client!RefetchQueriesResult.AdditionalProperties:interface"
33+
headingLevel={4}
34+
displayName="RefetchQueriesResult.AdditionalProperties"
35+
/>
8036

8137
These two arrays parallel each other: they have the same length, and `results[i]` is the result produced by `onQueryUpdated` when called with the `ObservableQuery` found at `queries[i]`, for any index `i`.
8238

39+
</FunctionDetails>
40+
8341
### Refetch recipes
8442

8543
#### Refetching a specific query

src/core/types.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,20 +271,32 @@ export type RefetchQueriesPromiseResults<TResult> =
271271
// default to ApolloQueryResult<any> if no onQueryUpdated function is passed
272272
// to client.refetchQueries.
273273
TResult[];
274-
275-
// The result of client.refetchQueries is thenable/awaitable, if you just want
276-
// an array of fully resolved results, but you can also access the raw results
277-
// immediately by examining the additional { queries, results } properties of
278-
// the RefetchQueriesResult<TResult> object.
274+
/**
275+
* The result of client.refetchQueries is thenable/awaitable, if you just want
276+
* an array of fully resolved results, but you can also access the raw results
277+
* immediately by examining the additional { queries, results } properties of
278+
* the RefetchQueriesResult<TResult> object.
279+
*/
279280
export interface RefetchQueriesResult<TResult>
280-
extends Promise<RefetchQueriesPromiseResults<TResult>> {
281-
// An array of ObservableQuery objects corresponding 1:1 to TResult values
282-
// in the results arrays (both the TResult[] array below, and the results
283-
// array resolved by the Promise above).
284-
queries: ObservableQuery<any>[];
285-
// These are the raw TResult values returned by any onQueryUpdated functions
286-
// that were invoked by client.refetchQueries.
287-
results: InternalRefetchQueriesResult<TResult>[];
281+
extends Promise<RefetchQueriesPromiseResults<TResult>>,
282+
RefetchQueriesResult.AdditionalProperties<TResult> {}
283+
284+
export namespace RefetchQueriesResult {
285+
export interface AdditionalProperties<TResult> {
286+
/**
287+
* An array of ObservableQuery objects corresponding 1:1 to TResult values
288+
* in the results arrays (both the `result` property and the resolved value).
289+
*/
290+
queries: ObservableQuery<any>[];
291+
/**
292+
* An array of results that were either returned by `onQueryUpdated`, or provided by default in the absence of `onQueryUpdated`, including pending promises.
293+
*
294+
* If `onQueryUpdated` returns `false` for a given query, no result is provided for that query.
295+
*
296+
* If `onQueryUpdated` returns `true`, the resulting `Promise<ApolloQueryResult<any>>` is included in the `results` array instead of `true`.
297+
*/
298+
results: InternalRefetchQueriesResult<TResult>[];
299+
}
288300
}
289301

290302
// Used by QueryManager["refetchQueries"]

0 commit comments

Comments
 (0)