Skip to content

Commit

Permalink
Improve InternalRefetchQueriesResult<TResult> generic type.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn authored and Stephen Barlow committed Jul 21, 2021
1 parent 770e020 commit ed1c9ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ export class QueryManager<TStore> {
// options.include.
includedQueriesById.delete(oq.queryId);

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

if (result === true) {
Expand All @@ -1250,7 +1250,7 @@ export class QueryManager<TStore> {
// 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);
results.set(oq, result as InternalRefetchQueriesResult<TResult>);
}

// Prevent the normal cache broadcast of this result, since we've
Expand All @@ -1271,7 +1271,7 @@ export class QueryManager<TStore> {

if (includedQueriesById.size) {
includedQueriesById.forEach(({ oq, lastDiff, diff }, queryId) => {
let result: undefined | boolean | InternalRefetchQueriesResult<TResult>;
let result: TResult | boolean | Promise<ApolloQueryResult<any>> | undefined;

// If onQueryUpdated is provided, we want to use it for all included
// queries, even the QueryOptions ones.
Expand All @@ -1290,7 +1290,7 @@ export class QueryManager<TStore> {
}

if (result !== false) {
results.set(oq, result!);
results.set(oq, result as InternalRefetchQueriesResult<TResult>);
}

if (queryId.indexOf("legacyOneTimeQuery") >= 0) {
Expand Down
9 changes: 8 additions & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ export interface InternalRefetchQueriesOptions<
}

export type InternalRefetchQueriesResult<TResult> =
TResult | Promise<ApolloQueryResult<any>>;
// If onQueryUpdated returns a boolean, that's equivalent to refetching the
// query when the boolean is true and skipping the query when false, so the
// internal type of refetched results is Promise<ApolloQueryResult<any>>.
TResult extends boolean ? Promise<ApolloQueryResult<any>> :
// Otherwise, onQueryUpdated returns whatever it returns. If onQueryUpdated is
// not provided, TResult defaults to Promise<ApolloQueryResult<any>> (see the
// generic type parameters of client.refetchQueries).
TResult;

export type InternalRefetchQueriesMap<TResult> =
Map<ObservableQuery<any>,
Expand Down

0 comments on commit ed1c9ee

Please sign in to comment.