Skip to content

Commit 9077bb9

Browse files
GLabatGuillaume LabatTkDodo
authored
refactor(query): add more context to "Missing queryFn" error (#5620)
* refactor(query): add more context to "Missing queryFn" error * refactor(query): use queryHash in error message for serialisation concern --------- Co-authored-by: Guillaume Labat <[email protected]> Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 6dc8d2e commit 9077bb9

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

packages/query-core/src/infiniteQueryBehavior.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export function infiniteQueryBehavior<
4444

4545
// Get query function
4646
const queryFn =
47-
context.options.queryFn || (() => Promise.reject('Missing queryFn'))
47+
context.options.queryFn ||
48+
(() =>
49+
Promise.reject(
50+
`Missing queryFn for queryKey '${context.options.queryHash}'`,
51+
))
4852

4953
const buildNewPages = (
5054
pages: unknown[],

packages/query-core/src/query.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ export class Query<
392392
// Create fetch function
393393
const fetchFn = () => {
394394
if (!this.options.queryFn) {
395-
return Promise.reject('Missing queryFn')
395+
return Promise.reject(
396+
`Missing queryFn for queryKey '${this.options.queryHash}'`,
397+
)
396398
}
397399
this.abortSignalConsumed = false
398400
return this.options.queryFn(queryFnContext)

packages/query-core/src/tests/infiniteQueryBehavior.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import { waitFor } from '@testing-library/react'
22
import type {
33
QueryClient,
44
InfiniteQueryObserverResult,
5+
QueryCache,
56
} from '@tanstack/query-core'
67
import { InfiniteQueryObserver } from '@tanstack/query-core'
78
import { createQueryClient, queryKey } from './utils'
89

910
describe('InfiniteQueryBehavior', () => {
1011
let queryClient: QueryClient
12+
let queryCache: QueryCache
1113

1214
beforeEach(() => {
1315
queryClient = createQueryClient()
16+
queryCache = queryClient.getQueryCache()
1417
queryClient.mount()
1518
})
1619

@@ -35,9 +38,10 @@ describe('InfiniteQueryBehavior', () => {
3538
})
3639

3740
await waitFor(() => {
41+
const query = queryCache.find(key)!
3842
return expect(observerResult).toMatchObject({
3943
isError: true,
40-
error: 'Missing queryFn',
44+
error: `Missing queryFn for queryKey '${query.queryHash}'`,
4145
})
4246
})
4347

packages/query-core/src/tests/query.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,12 @@ describe('query', () => {
794794
})
795795

796796
const unsubscribe = observer.subscribe(() => undefined)
797+
797798
await sleep(10)
798-
expect(mockLogger.error).toHaveBeenCalledWith('Missing queryFn')
799+
const query = queryCache.find(key)!
800+
expect(mockLogger.error).toHaveBeenCalledWith(
801+
`Missing queryFn for queryKey '${query.queryHash}'`,
802+
)
799803

800804
unsubscribe()
801805
})

0 commit comments

Comments
 (0)