Skip to content

Commit 8a86182

Browse files
GLabatGuillaume LabatTkDodo
authored
feat(react-query): better error in development mode to identify bad useQuery calls (#6288)
* refactor(query-core): export isPlainObject * refactor(useBaseQuery): dev error in case of bad argument Mainly for project without TS. This helps tracking bad signature when migrating from v4 to v5. * refactor(useQueryClient): dev error in case of bad queryClient Mainly for project without TS. Make sure the queryClient passed as parameter is really a query. With the change of signature introduced by v5, queryOptions from useQuery could end up as queryClient due to how useBaseQuery works. * revert: "refactor(useQueryClient): dev error in case of bad queryClient" This reverts commit 9bedf21. * refactor(useQuery): manual test to avoid export from query-core. * refactor(useQuery test): TS ignore invalid call --------- Co-authored-by: Guillaume Labat <[email protected]> Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 196cced commit 8a86182

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/react-query/src/__tests__/useQuery.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6225,4 +6225,25 @@ describe('useQuery', () => {
62256225

62266226
await waitFor(() => rendered.getByText('Works'))
62276227
})
6228+
6229+
// For Project without TS, when migrating from v4 to v5, make sure invalid calls due to bad parameters are tracked.
6230+
it('should throw in case of bad arguments to enhance DevX', async () => {
6231+
// Mock console error to avoid noise when test is run
6232+
const consoleMock = vi
6233+
.spyOn(console, 'error')
6234+
.mockImplementation(() => undefined)
6235+
6236+
const key = queryKey()
6237+
const queryFn = () => 'data'
6238+
6239+
function Page() {
6240+
// Invalid call on purpose
6241+
// @ts-expect-error
6242+
useQuery(key, { queryFn })
6243+
return <div>Does not matter</div>
6244+
}
6245+
6246+
expect(() => render(<Page />)).toThrow('Bad argument type')
6247+
consoleMock.mockRestore()
6248+
})
62286249
})

packages/react-query/src/useBaseQuery.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ export function useBaseQuery<
3131
Observer: typeof QueryObserver,
3232
queryClient?: QueryClient,
3333
) {
34+
if (process.env.NODE_ENV !== 'production') {
35+
if (typeof options !== 'object' || Array.isArray(options)) {
36+
throw new Error(
37+
'Bad argument type. Starting with v5, only the "Object" form is allowed when calling query related functions. Please use the error stack to find the culprit call. More info here: https://tanstack.com/query/latest/docs/react/guides/migrating-to-v5#supports-a-single-signature-one-object',
38+
)
39+
}
40+
}
41+
3442
const client = useQueryClient(queryClient)
3543
const isRestoring = useIsRestoring()
3644
const errorResetBoundary = useQueryErrorResetBoundary()

0 commit comments

Comments
 (0)