Skip to content

Commit

Permalink
Use fewer forced typecasts in QueryManager#refetchQueries.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed May 14, 2021
1 parent 0554be2 commit faa6ac6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Resolvers,
RefetchQueriesOptions,
RefetchQueriesResult,
InternalRefetchQueriesResult,
} from './types';

import {
Expand Down Expand Up @@ -544,18 +545,22 @@ export class ApolloClient<TCacheShape> implements DataProxy {
): RefetchQueriesResult<TResult> {
const map = this.queryManager.refetchQueries(options);
const queries: ObservableQuery<any>[] = [];
const results: TResult[] = [];
const results: InternalRefetchQueriesResult<TResult>[] = [];

map.forEach((update, obsQuery) => {
map.forEach((result, obsQuery) => {
queries.push(obsQuery);
results.push(update);
results.push(result);
});

const result = Promise.all(results) as RefetchQueriesResult<TResult>;
const result = Promise.all<TResult>(
results as TResult[]
) as RefetchQueriesResult<TResult>;

// In case you need the raw results immediately, without awaiting
// Promise.all(results):
result.queries = queries;
result.results = results;

return result;
}

Expand Down
19 changes: 11 additions & 8 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
RefetchQueryDescription,
InternalRefetchQueriesOptions,
RefetchQueryDescriptor,
InternalRefetchQueriesResult,
InternalRefetchQueriesMap,
} from './types';
import { LocalState } from './LocalState';

Expand Down Expand Up @@ -1045,7 +1047,7 @@ export class QueryManager<TStore> {
removeOptimistic = optimistic ? makeUniqueId("refetchQueries") : void 0,
onQueryUpdated,
}: InternalRefetchQueriesOptions<ApolloCache<TStore>, TResult>
): Map<ObservableQuery<any>, TResult> {
): InternalRefetchQueriesMap<TResult> {
const includedQueriesById = new Map<string, {
desc: RefetchQueryDescriptor;
lastDiff?: Cache.DiffResult<any>;
Expand All @@ -1065,7 +1067,7 @@ export class QueryManager<TStore> {
});
}

const results = new Map<ObservableQuery<any>, TResult>();
const results: InternalRefetchQueriesMap<TResult> = new Map;

if (updateCache) {
this.cache.batch({
Expand Down Expand Up @@ -1123,18 +1125,19 @@ export class QueryManager<TStore> {
// options.include.
includedQueriesById.delete(oq.queryId);

let result = onQueryUpdated(oq, diff, lastDiff);
let result: boolean | InternalRefetchQueriesResult<TResult> =
onQueryUpdated(oq, diff, lastDiff);

if (result === true) {
// The onQueryUpdated function requested the default refetching
// behavior by returning true.
result = oq.refetch() as any;
result = oq.refetch();
}

// Record the result in the results Map, as long as onQueryUpdated
// did not return false to skip/ignore this result.
if (result !== false) {
results.set(oq, result as TResult);
results.set(oq, result);
}

// Prevent the normal cache broadcast of this result, since we've
Expand All @@ -1161,7 +1164,7 @@ export class QueryManager<TStore> {
includedQueriesById.forEach(({ desc, lastDiff, diff }, queryId) => {
const queryInfo = this.getQuery(queryId);
let oq = queryInfo.observableQuery;
let fallback: undefined | (() => any);
let fallback: undefined | (() => Promise<ApolloQueryResult<any>>);

if (typeof desc === "string") {
fallback = () => oq!.refetch();
Expand All @@ -1181,7 +1184,7 @@ export class QueryManager<TStore> {
}

if (oq && fallback) {
let result: boolean | TResult | undefined;
let result: undefined | boolean | InternalRefetchQueriesResult<TResult>;
// If onQueryUpdated is provided, we want to use it for all included
// queries, even the PureQueryOptions ones. Otherwise, we call the
// fallback function defined above.
Expand All @@ -1196,7 +1199,7 @@ export class QueryManager<TStore> {
result = fallback();
}
if (result !== false) {
results.set(oq, result as TResult);
results.set(oq, result!);
}
}
});
Expand Down
9 changes: 8 additions & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extends Promise<RefetchQueriesPromiseResults<TResult>> {
queries: ObservableQuery<any>[];
// These are the raw TResult values returned by any onQueryUpdated functions
// that were invoked by client.refetchQueries.
results: TResult[];
results: InternalRefetchQueriesResult<TResult>[];
}

// Used by QueryManager["refetchQueries"]
Expand All @@ -101,6 +101,13 @@ export interface InternalRefetchQueriesOptions<
removeOptimistic?: string;
}

export type InternalRefetchQueriesResult<TResult> =
TResult | Promise<ApolloQueryResult<any>>;

export type InternalRefetchQueriesMap<TResult> =
Map<ObservableQuery<any>,
InternalRefetchQueriesResult<TResult>>;

export type OperationVariables = Record<string, any>;

export type PureQueryOptions = {
Expand Down

0 comments on commit faa6ac6

Please sign in to comment.