Skip to content

Commit

Permalink
Improve options API of client.refetchQueries method.
Browse files Browse the repository at this point in the history
This is obviously a breaking change for client.refetchQueries, but that
method was introduced in PR #7431, targeting the release-3.4 branch.

Since Apollo Client v3.4 is still in beta, we still have room to rework
the signature of the client.refetchQueries method (introduced in #7431 and
released in @apollo/client@3.4.0-beta.3), not only adding significant new
functionality like options.updateCache and options.onQueryUpdated, but
also leaving room to add functionality more easily in the future, without
breaking backwards compatibility, since client.refetchQueries takes named
options and returns an object of named results, so adding new options or
returning new results never needs to be a breaking change.
  • Loading branch information
benjamn committed May 18, 2021
1 parent 204ae77 commit 82c68c8
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 111 deletions.
4 changes: 3 additions & 1 deletion src/__tests__/client.ts
Expand Up @@ -2436,7 +2436,9 @@ describe('client', () => {

// @ts-ignore
const spy = jest.spyOn(client.queryManager, 'refetchQueries');
await client.refetchQueries(['Author1']);
await client.refetchQueries({
include: ['Author1'],
});
expect(spy).toHaveBeenCalled();
});

Expand Down
29 changes: 25 additions & 4 deletions src/core/ApolloClient.ts
Expand Up @@ -23,7 +23,7 @@ import {
MutationOptions,
SubscriptionOptions,
WatchQueryFetchPolicy,
RefetchQueryDescription,
RefetchQueriesOptions,
} from './watchQueryOptions';

import {
Expand Down Expand Up @@ -536,9 +536,30 @@ export class ApolloClient<TCacheShape> implements DataProxy {
* Takes optional parameter `includeStandby` which will include queries in standby-mode when refetching.
*/
public refetchQueries(
queries: RefetchQueryDescription,
): Promise<ApolloQueryResult<any>[]> {
return Promise.all(this.queryManager.refetchQueries(queries));
options: Pick<
RefetchQueriesOptions<ApolloCache<TCacheShape>>,
| "updateCache"
| "include"
| "optimistic"
| "onQueryUpdated"
>,
): Promise<{
queries: ObservableQuery<any>[];
results: Map<ObservableQuery<any>, ApolloQueryResult<any>>;
}> {
const results = this.queryManager.refetchQueries(options);
const queries: ObservableQuery<any>[] = [];
const values: any[] = [];

results.forEach((value, obsQuery) => {
queries.push(obsQuery);
values.push(value);
});

return Promise.all(values).then(values => {
values.forEach((value, i) => results.set(queries[i], value));
return { queries, results };
});
}

/**
Expand Down

0 comments on commit 82c68c8

Please sign in to comment.