Skip to content

Commit

Permalink
Add extra to BaseQueryApi (#1378)
Browse files Browse the repository at this point in the history
Co-authored-by: Ricardo Sanchez <ricardosancheze@outlook.com>
  • Loading branch information
ricksanchez and ricksanchez authored Aug 11, 2021
1 parent cb3e158 commit 22e4074
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/toolkit/src/query/baseQueryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface BaseQueryApi {
signal: AbortSignal
dispatch: ThunkDispatch<any, any, any>
getState: () => unknown
extra: unknown
}

export type QueryReturnValue<T = unknown, E = unknown, M = unknown> =
Expand Down
3 changes: 2 additions & 1 deletion packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export function buildThunks<
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
> = async (
arg,
{ signal, rejectWithValue, fulfillWithValue, dispatch, getState }
{ signal, rejectWithValue, fulfillWithValue, dispatch, getState, extra }
) => {
const endpointDefinition = endpointDefinitions[arg.endpointName]

Expand All @@ -271,6 +271,7 @@ export function buildThunks<
signal,
dispatch,
getState,
extra,
}
if (endpointDefinition.query) {
result = await baseQuery(
Expand Down
23 changes: 23 additions & 0 deletions packages/toolkit/src/query/tests/buildThunks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { configureStore } from '@reduxjs/toolkit'
import { createApi } from '@reduxjs/toolkit/query/react'

import { renderHook } from '@testing-library/react-hooks'
import type { BaseQueryApi } from '../baseQueryTypes'
import { withProvider } from './helpers'

test('handles a non-async baseQuery without error', async () => {
Expand Down Expand Up @@ -49,6 +50,28 @@ test('handles a non-async baseQuery without error', async () => {
})
})

test('passes the extraArgument property to the baseQueryApi', async () => {
const baseQuery = (_args: any, api: BaseQueryApi) => ({ data: api.extra })
const api = createApi({
baseQuery,
endpoints: (build) => ({
getUser: build.query<unknown, void>({
query: () => '',
}),
}),
})
const store = configureStore({
reducer: {
[api.reducerPath]: api.reducer,
},
middleware: (gDM) =>
gDM({ thunk: { extraArgument: 'cakes' } }).concat(api.middleware),
})
const { getUser } = api.endpoints
const { data } = await store.dispatch(getUser.initiate())
expect(data).toBe('cakes')
})

describe('re-triggering behavior on arg change', () => {
const api = createApi({
baseQuery: () => ({ data: undefined }),
Expand Down
2 changes: 2 additions & 0 deletions packages/toolkit/src/query/tests/errorHandling.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -61,6 +62,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand Down
31 changes: 31 additions & 0 deletions packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -95,6 +96,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -113,6 +115,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -136,6 +139,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand Down Expand Up @@ -165,6 +169,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -189,6 +194,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand Down Expand Up @@ -218,6 +224,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand Down Expand Up @@ -245,6 +252,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand Down Expand Up @@ -275,6 +283,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -293,6 +302,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -318,6 +328,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -341,6 +352,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -359,6 +371,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -376,6 +389,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -393,6 +407,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -410,6 +425,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -427,6 +443,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -448,6 +465,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -471,6 +489,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -488,6 +507,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -512,6 +532,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -533,6 +554,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand Down Expand Up @@ -563,6 +585,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -582,6 +605,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -607,6 +631,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -625,6 +650,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand All @@ -644,6 +670,7 @@ describe('fetchBaseQuery', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand Down Expand Up @@ -672,6 +699,7 @@ describe('fetchFn', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
))
Expand Down Expand Up @@ -699,6 +727,7 @@ describe('fetchFn', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -725,6 +754,7 @@ describe('FormData', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand All @@ -746,6 +776,7 @@ describe('still throws on completely unexpected errors', () => {
signal: new AbortController().signal,
dispatch: storeRef.store.dispatch,
getState: storeRef.store.getState,
extra: undefined,
},
{}
)
Expand Down

0 comments on commit 22e4074

Please sign in to comment.