Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions packages/toolkit/src/query/core/buildSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,77 +582,6 @@ export function buildSlice({
},
})

const infiniteQuerySlice = createSlice({
name: `${reducerPath}/infinitequeries`,
initialState: initialState as QueryState<any>,
reducers: {
fetchNextPage(
d,
a: PayloadAction<
{
endpointName: string
requestId: string
options: Subscribers[number]
} & QuerySubstateIdentifier
>,
) {
// Dummy
},
unsubscribeQueryResult(
d,
a: PayloadAction<{ requestId: string } & QuerySubstateIdentifier>,
) {
// Dummy
},
internal_getRTKQSubscriptions() {},
},
// extraReducers(builder) {
// builder
// .addCase(queryThunk.fulfilled, (draft, { meta, payload }) => {
// updateQuerySubstateIfExists(
// draft,
// meta.arg.queryCacheKey,
// (substate) => {
// const { infiniteQueryOptions } = definitions[
// meta.arg.endpointName
// ] as InfiniteQueryDefinition<any, any, any, any>
// substate.status = QueryStatus.fulfilled
// if(!infiniteQueryOptions) return
//
// if (substate.data !== undefined) {
// const { fulfilledTimeStamp, arg, baseQueryMeta, requestId } =
// meta
// // There's existing cache data. Let the user merge it in themselves.
// // We're already inside an Immer-powered reducer, and the user could just mutate `substate.data`
// // themselves inside of `merge()`. But, they might also want to return a new value.
// // Try to let Immer figure that part out, save the result, and assign it to `substate.data`.
// substate.data = payload
// } else {
// // Presumably a fresh request. Just cache the response data.
// substate.data = payload
// }
// } else {
// // Assign or safely update the cache data.
// substate.data =
// definitions[meta.arg.endpointName].structuralSharing ?? true
// ? copyWithStructuralSharing(
// isDraft(substate.data)
// ? original(substate.data)
// : substate.data,
// payload,
// )
// : payload
// }
//
// delete substate.error
// substate.fulfilledTimeStamp = meta.fulfilledTimeStamp
// },
// )
// })

// },
})

// Dummy slice to generate actions
const subscriptionSlice = createSlice({
name: `${reducerPath}/subscriptions`,
Expand Down
73 changes: 73 additions & 0 deletions packages/toolkit/src/query/tests/infiniteQueries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,4 +858,77 @@ describe('Infinite queries', () => {
meta: undefined,
})
})

test('Can use transformResponse', async () => {
type PokemonPage = { items: Pokemon[]; page: number }
const pokemonApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getInfinitePokemonWithTransform: builder.infiniteQuery<
PokemonPage,
string,
number
>({
infiniteQueryOptions: {
initialPageParam: 0,
getNextPageParam: (
lastPage,
allPages,
// Page param type should be `number`
lastPageParam,
allPageParams,
) => lastPageParam + 1,
},
query(pageParam) {
return `https://example.com/listItems?page=${pageParam}`
},
transformResponse(baseQueryReturnValue: Pokemon[], meta, arg) {
expect(Array.isArray(baseQueryReturnValue)).toBe(true)
return {
items: baseQueryReturnValue,
page: arg,
}
},
}),
}),
})

const storeRef = setupApiStore(
pokemonApi,
{ ...actionsReducer },
{
withoutTestLifecycles: true,
},
)

const checkResultData = (
result: InfiniteQueryResult,
expectedValues: PokemonPage[],
) => {
expect(result.status).toBe(QueryStatus.fulfilled)
if (result.status === QueryStatus.fulfilled) {
expect(result.data.pages).toEqual(expectedValues)
}
}

const res1 = storeRef.store.dispatch(
pokemonApi.endpoints.getInfinitePokemonWithTransform.initiate('fire', {}),
)

const entry1InitialLoad = await res1
checkResultData(entry1InitialLoad, [
{ items: [{ id: '0', name: 'Pokemon 0' }], page: 0 },
])

const entry1Updated = await storeRef.store.dispatch(
pokemonApi.endpoints.getInfinitePokemonWithTransform.initiate('fire', {
direction: 'forward',
}),
)

checkResultData(entry1Updated, [
{ items: [{ id: '0', name: 'Pokemon 0' }], page: 0 },
{ items: [{ id: '1', name: 'Pokemon 1' }], page: 1 },
])
})
})
Loading