Skip to content

Commit

Permalink
Add skip condition on endpoint definition
Browse files Browse the repository at this point in the history
  • Loading branch information
SYoder1 committed Jan 13, 2025
1 parent 4f3bc9f commit 36fde17
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
getPendingMeta() {
return { startedTimeStamp: Date.now(), [SHOULD_AUTOBATCH]: true }
},
condition(queryThunkArgs, { getState }) {
condition(queryThunkArgs, options) {
const { getState } = options
const state = getState()

const requestState =
Expand Down Expand Up @@ -551,6 +552,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?.({
Expand Down
3 changes: 3 additions & 0 deletions packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
UnwrapPromise,
} from './tsHelpers'
import { isNotNullish } from './utils'
import type { QueryThunkArg, ThunkApiMetaConfig } from './core/buildThunks'

const resultType = /* @__PURE__ */ Symbol()
const baseQuery = /* @__PURE__ */ Symbol()
Expand Down Expand Up @@ -349,6 +350,8 @@ export interface QueryExtraOptions<
*/
invalidatesTags?: never

skipCondition?: (state: RootState<any, string, ReducerPath>) => boolean

/**
* Can be provided to return a custom cache key value based on the query arguments.
*
Expand Down
11 changes: 10 additions & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { UNINITIALIZED_VALUE } from './constants'
import type { ReactHooksModuleOptions } from './module'
import { useStableQueryArgs } from './useSerializedStableValue'
import { useShallowStableValue } from './useShallowStableValue'
import { isQueryDefinition } from '../endpointDefinitions'

// Copy-pasted from React-Redux
const canUseDOM = () =>
Expand Down Expand Up @@ -1316,9 +1317,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[name]
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,
Expand Down

0 comments on commit 36fde17

Please sign in to comment.