Skip to content

Add skip condition on endpoint definition #4813

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
@@ -919,6 +919,14 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
return true
}

if (
isQueryDefinition(endpointDefinition) &&
endpointDefinition?.skipCondition &&
endpointDefinition?.skipCondition(state)
) {
return false
}

if (
isQueryDefinition(endpointDefinition) &&
endpointDefinition?.forceRefetch?.({
12 changes: 12 additions & 0 deletions packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
@@ -612,6 +612,18 @@ export interface QueryExtraOptions<
*/
invalidatesTags?: never

/**
* Can be provided to skip the query if the condition is met.
*
* It's the equivalent of the `skip` option on the query hook but for the endpoint definition.
*
* @example
* ```ts
* skipCondition: (state) => state.user.isLoggedIn === false
* ```
*/
skipCondition?: (state: RootState<any, string, ReducerPath>) => boolean

/**
* Can be provided to return a custom cache key value based on the query arguments.
*
15 changes: 13 additions & 2 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
@@ -64,7 +64,10 @@ import type { ReactHooksModuleOptions } from './module'
import { useStableQueryArgs } from './useSerializedStableValue'
import { useShallowStableValue } from './useShallowStableValue'
import type { InfiniteQueryDirection } from '../core/apiState'
import { isInfiniteQueryDefinition } from '../endpointDefinitions'
import {
isInfiniteQueryDefinition,
isQueryDefinition,
} from '../endpointDefinitions'
import type { StartInfiniteQueryActionCreator } from '../core/buildInitiate'

// Copy-pasted from React-Redux
@@ -1992,9 +1995,17 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
},
useQuery(arg, options) {
const querySubscriptionResults = useQuerySubscription(arg, options)
const store = useStore<RootState<Definitions, any, any>>()

const endpointDefinition = context.endpointDefinitions[endpointName]
const shouldSkipFromCondition =
isQueryDefinition(endpointDefinition) &&
endpointDefinition?.skipCondition &&
endpointDefinition?.skipCondition(store.getState())

const queryStateResults = useQueryState(arg, {
selectFromResult:
arg === skipToken || options?.skip
arg === skipToken || options?.skip || shouldSkipFromCondition
? undefined
: noPendingQueryStateSelector,
...options,