Skip to content
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

Check middleware registration directly to avoid persistence issues #2850

Merged
merged 1 commit into from
Oct 30, 2022
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
26 changes: 17 additions & 9 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,23 @@ export function buildInitiate({
Object.values(runningMutations.get(dispatch) || {}).filter(isNotNullish)
}

function middlewareWarning(getState: () => RootState<{}, string, string>) {
function middlewareWarning(dispatch: Dispatch) {
if (process.env.NODE_ENV !== 'production') {
if ((middlewareWarning as any).triggered) return
const registered =
getState()[api.reducerPath]?.config?.middlewareRegistered
if (registered !== undefined) {
;(middlewareWarning as any).triggered = true
}
if (registered === false) {
const registered:
| ReturnType<typeof api.internalActions.internal_probeSubscription>
| boolean = dispatch(
api.internalActions.internal_probeSubscription({
queryCacheKey: 'DOES_NOT_EXIST',
requestId: 'DUMMY_REQUEST_ID',
})
)

;(middlewareWarning as any).triggered = true

// The RTKQ middleware _should_ always return a boolean for `probeSubscription`
if (typeof registered !== 'boolean') {
// Otherwise, must not have been added
throw new Error(
`Warning: Middleware for RTK-Query API at reducerPath "${api.reducerPath}" has not been added to the store.
You must add the middleware for RTK-Query to function correctly!`
Expand Down Expand Up @@ -346,7 +354,7 @@ You must add the middleware for RTK-Query to function correctly!`
const thunkResult = dispatch(thunk)
const stateAfter = selector(getState())

middlewareWarning(getState)
middlewareWarning(dispatch)

const { requestId, abort } = thunkResult

Expand Down Expand Up @@ -440,7 +448,7 @@ You must add the middleware for RTK-Query to function correctly!`
fixedCacheKey,
})
const thunkResult = dispatch(thunk)
middlewareWarning(getState)
middlewareWarning(dispatch)
const { requestId, abort, unwrap } = thunkResult
const returnValuePromise = thunkResult
.unwrap()
Expand Down
7 changes: 5 additions & 2 deletions packages/toolkit/src/query/tests/devWarnings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,16 @@ describe('missing reducer', () => {
})
})

test('warns only for reducer if everything is missing', async () => {
test('warns for reducer and also throws error if everything is missing', async () => {
const store = configureStore({
reducer: { x: () => 0 },
})
// @ts-expect-error
api1.endpoints.q1.select(undefined)(store.getState())
await store.dispatch(api1.endpoints.q1.initiate(undefined))
const doDispatch = () => {
store.dispatch(api1.endpoints.q1.initiate(undefined))
}
expect(doDispatch).toThrowError(reMatchMissingMiddlewareError)
expect(getLog().log).toBe(
'Error: No data found at `state.api`. Did you forget to add the reducer to the store?'
)
Expand Down