Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5772588
Added rough draft of initial endpoint definition for infinite query
riqts Mar 1, 2024
ed8af73
Added placeholder select and initiate functions where the merge and q…
riqts Mar 1, 2024
8fc9632
Theoretical slice added to buildSlice to potentially handle changing …
riqts Mar 1, 2024
c18c6ee
Added new endpointDefinition to createApi + module
riqts Mar 1, 2024
0a7a35a
Added infiniteQueryThunks to core module file
riqts Mar 6, 2024
7deab9c
Initial implementation of executeEndpoint/Thunk with infiniteQuery
riqts Mar 6, 2024
ce98c04
Extended infiniteQueryInitiate, added fetchNextPage and fetchPrevious…
riqts Mar 6, 2024
56e9e25
Added types for InfiniteQueryConfig
riqts Mar 6, 2024
2027e75
Shelf
riqts Mar 20, 2024
09586d1
Built out initial react hook definitions for InfiniteQuery
riqts Apr 23, 2024
270e4c1
Added InfiniteQuerySubState with param and direction for slice operat…
riqts May 4, 2024
5f41a9d
Set up Test Hook to actually test if next page gets data
riqts May 4, 2024
0999681
Introduced correct typing and infiniteQuery hooks into buildHooks.ts
riqts May 4, 2024
abf828e
Initiate handling and types for infiniteQuery
riqts May 4, 2024
79dfd50
Added InfiniteQuery Selectors types for the hook
riqts May 4, 2024
670972b
Added necessary slice actions for inf query substate
riqts May 4, 2024
8d2a4ce
Forced into the thunk working InfQuery handling for api proof of concept
riqts May 4, 2024
ae9b98a
Altered EndPointDefinition for type inference
riqts May 4, 2024
6215273
Index has inf query type
riqts May 4, 2024
2d58473
Cleanup imports from first pass
riqts May 4, 2024
d7defa3
add inf query definition option for module.ts
riqts May 4, 2024
d541932
Revert "Shelf"
riqts May 4, 2024
5ec2430
Force selector type for infinite query test
riqts May 5, 2024
a3ced0f
Pass through infinite query options
markerikson Sep 29, 2024
fedead4
Fix remaining TS issues
markerikson Sep 29, 2024
9fa89d7
Hack around TS issues
markerikson Sep 29, 2024
7248c38
Try to fix exported types
markerikson Sep 29, 2024
9319061
Pass through correct data types for infinite queries
markerikson Oct 22, 2024
70f49a0
Add an initial test for infinite query thunks
markerikson Oct 22, 2024
5ce06d1
Add `serializeQueryArgs` type support
markerikson Oct 23, 2024
3f0b35f
WIP Try separating QueryArg and PageParam for infinite queries
markerikson Oct 23, 2024
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
75 changes: 68 additions & 7 deletions packages/toolkit/src/query/core/apiState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
BaseEndpointDefinition,
ResultTypeFrom,
QueryArgFrom,
InfiniteQueryDefinition,
PageParamFrom,
} from '../endpointDefinitions'
import type { Id, WithRequiredProp } from '../tsHelpers'

Expand All @@ -28,6 +30,34 @@ export type RefetchConfigOptions = {
refetchOnFocus: boolean
}

export type GetNextPageParamFunction<TPageParam, TQueryFnData> = (
lastPage: TQueryFnData,
allPages: Array<TQueryFnData>,
lastPageParam: TPageParam,
allPageParams: Array<TPageParam>,
) => TPageParam | undefined | null

export type GetPreviousPageParamFunction<TPageParam, TQueryFnData> = (
firstPage: TQueryFnData,
allPages: Array<TQueryFnData>,
firstPageParam: TPageParam,
allPageParams: Array<TPageParam>,
) => TPageParam | undefined | null

export type InfiniteQueryConfigOptions<TQueryFnData, TPageParam> = {
/**
* This function can be set to automatically get the previous cursor for infinite queries.
* The result will also be used to determine the value of `hasPreviousPage`.
*/
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TQueryFnData>
getNextPageParam: GetNextPageParamFunction<TPageParam, TQueryFnData>
}

export interface InfiniteData<TData, TPageParam> {
pages: Array<TData>
pageParams: Array<TPageParam>
}

/**
* Strings describing the query state at any given time.
*/
Expand Down Expand Up @@ -133,7 +163,10 @@ export type MutationKeys<Definitions extends EndpointDefinitions> = {
: never
}[keyof Definitions]

type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
type BaseQuerySubState<
D extends BaseEndpointDefinition<any, any, any>,
DataType = ResultTypeFrom<D>,
> = {
/**
* The argument originally passed into the hook or `initiate` action call
*/
Expand All @@ -145,7 +178,7 @@ type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
/**
* The received data from the query
*/
data?: ResultTypeFrom<D>
data?: DataType
/**
* The received error if applicable
*/
Expand All @@ -166,21 +199,31 @@ type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
* Time that the latest query was fulfilled
*/
fulfilledTimeStamp?: number
/**
* Infinite Query Specific substate properties
*/
hasNextPage?: boolean
hasPreviousPage?: boolean
direction?: 'forward' | 'backwards'
param?: QueryArgFrom<D>
}

export type QuerySubState<D extends BaseEndpointDefinition<any, any, any>> = Id<
export type QuerySubState<
D extends BaseEndpointDefinition<any, any, any>,
DataType = ResultTypeFrom<D>,
> = Id<
| ({
status: QueryStatus.fulfilled
} & WithRequiredProp<
BaseQuerySubState<D>,
BaseQuerySubState<D, DataType>,
'data' | 'fulfilledTimeStamp'
> & { error: undefined })
| ({
status: QueryStatus.pending
} & BaseQuerySubState<D>)
} & BaseQuerySubState<D, DataType>)
| ({
status: QueryStatus.rejected
} & WithRequiredProp<BaseQuerySubState<D>, 'error'>)
} & WithRequiredProp<BaseQuerySubState<D, DataType>, 'error'>)
| {
status: QueryStatus.uninitialized
originalArgs?: undefined
Expand All @@ -193,6 +236,21 @@ export type QuerySubState<D extends BaseEndpointDefinition<any, any, any>> = Id<
}
>

export type InfiniteQuerySubState<
D extends BaseEndpointDefinition<any, any, any>,
> =
D extends InfiniteQueryDefinition<any, any, any, any, any>
? QuerySubState<D, InfiniteData<ResultTypeFrom<D>, PageParamFrom<D>>> & {
// TODO: These shouldn't be optional
hasNextPage?: boolean
hasPreviousPage?: boolean
isFetchingNextPage?: boolean
isFetchingPreviousPage?: boolean
param?: PageParamFrom<D>
direction?: 'forward' | 'backwards'
}
: never

type BaseMutationSubState<D extends BaseEndpointDefinition<any, any, any>> = {
requestId: string
data?: ResultTypeFrom<D>
Expand Down Expand Up @@ -249,7 +307,10 @@ export type InvalidationState<TagTypes extends string> = {
}

export type QueryState<D extends EndpointDefinitions> = {
[queryCacheKey: string]: QuerySubState<D[string]> | undefined
[queryCacheKey: string]:
| QuerySubState<D[string]>
| InfiniteQuerySubState<D[string]>
| undefined
}

export type SubscriptionState = {
Expand Down
Loading
Loading