Skip to content

Commit

Permalink
feat: Add shouldPollingBeEnabled option for useQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-kumawat committed Nov 29, 2023
1 parent 4adbb2e commit 8e38108
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/core/ObservableQuery.ts
Expand Up @@ -777,19 +777,23 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,
const maybeFetch = () => {
if (this.pollingInfo) {
if (!isNetworkRequestInFlight(this.queryInfo.networkStatus)) {
this.reobserve(
{
// Most fetchPolicy options don't make sense to use in a polling context, as
// users wouldn't want to be polling the cache directly. However, network-only and
// no-cache are both useful for when the user wants to control whether or not the
// polled results are written to the cache.
fetchPolicy:
this.options.initialFetchPolicy === "no-cache" ?
"no-cache"
: "network-only",
},
NetworkStatus.poll
).then(poll, poll);
if(this.options.shouldPollingBeDisabled?.()) {
poll();
} else {
this.reobserve(
{
// Most fetchPolicy options don't make sense to use in a polling context, as
// users wouldn't want to be polling the cache directly. However, network-only and
// no-cache are both useful for when the user wants to control whether or not the
// polled results are written to the cache.
fetchPolicy:
this.options.initialFetchPolicy === "no-cache" ?
"no-cache"
: "network-only",
},
NetworkStatus.poll
).then(poll, poll);
}
} else {
poll();
}
Expand Down
9 changes: 9 additions & 0 deletions src/core/watchQueryOptions.ts
Expand Up @@ -47,6 +47,8 @@ export type RefetchWritePolicy = "merge" | "overwrite";
*/
export type ErrorPolicy = "none" | "ignore" | "all";

export type ShouldPollingBeDisabled = () => boolean;

/**
* Query options.
*/
Expand Down Expand Up @@ -176,6 +178,13 @@ export interface WatchQueryOptions<

/** {@inheritDoc @apollo/client!QueryOptions#canonizeResults:member} */
canonizeResults?: boolean;

/**
* If returned `true`, network call will be made as per the polling interval.
* If returned `false` network call won't be made for that particular tick.
* This will be called on every polling tick.
*/
shouldPollingBeDisabled?: ShouldPollingBeDisabled;
}

export interface NextFetchPolicyContext<
Expand Down

0 comments on commit 8e38108

Please sign in to comment.