Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add skipPollAttempt option to control polling refetch behavior #11397

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .api-reports/api-report-core.md
Expand Up @@ -2069,6 +2069,7 @@ export interface WatchQueryOptions<TVariables extends OperationVariables = Opera
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand Down Expand Up @@ -2115,7 +2116,7 @@ interface WriteContext extends ReadMergeModifyContext {
// src/core/QueryManager.ts:120:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
// src/core/QueryManager.ts:154:5 - (ae-forgotten-export) The symbol "LocalState" needs to be exported by the entry point index.d.ts
// src/core/QueryManager.ts:395:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/link/http/selectHttpOptionsAndBody.ts:128:32 - (ae-forgotten-export) The symbol "HttpQueryOptions" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react.md
Expand Up @@ -2235,6 +2235,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand All @@ -2256,7 +2257,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:30:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:31:3 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useLoadableQuery.ts:50:5 - (ae-forgotten-export) The symbol "ResetFunction" needs to be exported by the entry point index.d.ts
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_components.md
Expand Up @@ -1664,6 +1664,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand All @@ -1685,7 +1686,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_context.md
Expand Up @@ -1560,6 +1560,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand All @@ -1581,7 +1582,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_hoc.md
Expand Up @@ -1588,6 +1588,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand Down Expand Up @@ -1626,7 +1627,7 @@ export function withSubscription<TProps extends TGraphQLVariables | {} = {}, TDa
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_hooks.md
Expand Up @@ -2126,6 +2126,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand All @@ -2147,7 +2148,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:30:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:31:3 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useLoadableQuery.ts:50:5 - (ae-forgotten-export) The symbol "ResetFunction" needs to be exported by the entry point index.d.ts
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-react_ssr.md
Expand Up @@ -1546,6 +1546,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand All @@ -1567,7 +1568,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-testing.md
Expand Up @@ -1599,6 +1599,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand Down Expand Up @@ -1629,7 +1630,7 @@ export function withWarningSpy<TArgs extends any[], TResult>(it: (...args: TArgs
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-testing_core.md
Expand Up @@ -1556,6 +1556,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand Down Expand Up @@ -1586,7 +1587,7 @@ export function withWarningSpy<TArgs extends any[], TResult>(it: (...args: TArgs
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report-utilities.md
Expand Up @@ -2413,6 +2413,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand Down Expand Up @@ -2469,7 +2470,7 @@ interface WriteContext extends ReadMergeModifyContext {
// src/core/types.ts:156:3 - (ae-forgotten-export) The symbol "NetworkStatus" needs to be exported by the entry point index.d.ts
// src/core/types.ts:174:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:201:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/storeUtils.ts:226:12 - (ae-forgotten-export) The symbol "storeKeyNameStringify" needs to be exported by the entry point index.d.ts
// src/utilities/policies/pagination.ts:76:3 - (ae-forgotten-export) The symbol "TRelayEdge" needs to be exported by the entry point index.d.ts
// src/utilities/policies/pagination.ts:77:3 - (ae-forgotten-export) The symbol "TRelayPageInfo" needs to be exported by the entry point index.d.ts
Expand Down
3 changes: 2 additions & 1 deletion .api-reports/api-report.md
Expand Up @@ -2853,6 +2853,7 @@ export interface WatchQueryOptions<TVariables extends OperationVariables = Opera
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
variables?: TVariables;
}

Expand Down Expand Up @@ -2899,7 +2900,7 @@ interface WriteContext extends ReadMergeModifyContext {
// src/core/QueryManager.ts:120:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
// src/core/QueryManager.ts:154:5 - (ae-forgotten-export) The symbol "LocalState" needs to be exported by the entry point index.d.ts
// src/core/QueryManager.ts:395:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:253:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:260:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/link/http/selectHttpOptionsAndBody.ts:128:32 - (ae-forgotten-export) The symbol "HttpQueryOptions" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:30:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:31:3 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
Expand Down
19 changes: 19 additions & 0 deletions .changeset/swift-zoos-collect.md
@@ -0,0 +1,19 @@
---
@apollo/client: minor
---

Adds a new `skipPollAttempt` callback function that's called whenever a refetch attempt occurs while polling. If the function returns `true`, the refetch is skipped and not reattempted until the next poll interval. This will solve the frequent use-case of disabling polling when the window is inactive.

```ts
useQuery(QUERY, {
pollInterval: 1000,
skipPollAttempt: () => document.hidden // or !document.hasFocus()
});
// or define it globally
new ApolloClient({
defaultOptions: {
watchQuery: {
skipPollAttempt: () => document.hidden // or !document.hasFocus()
}
}
})
4 changes: 2 additions & 2 deletions .size-limits.json
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 38546,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32318
"dist/apollo-client.min.cjs": 38576,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32352
}
5 changes: 4 additions & 1 deletion src/core/ObservableQuery.ts
Expand Up @@ -776,7 +776,10 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,

const maybeFetch = () => {
if (this.pollingInfo) {
if (!isNetworkRequestInFlight(this.queryInfo.networkStatus)) {
if (
!isNetworkRequestInFlight(this.queryInfo.networkStatus) &&
!this.options.skipPollAttempt?.()
) {
this.reobserve(
{
// Most fetchPolicy options don't make sense to use in a polling context, as
Expand Down
7 changes: 7 additions & 0 deletions src/core/watchQueryOptions.ts
Expand Up @@ -176,6 +176,13 @@ export interface WatchQueryOptions<

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

/**
* A callback function that's called whenever a refetch attempt occurs
* while polling. If the function returns `true`, the refetch is
* skipped and not reattempted until the next poll interval.
*/
skipPollAttempt?: () => boolean;
}

export interface NextFetchPolicyContext<
Expand Down