Skip to content

Commit c0ec9fe

Browse files
JosNunlachlancollinsautofix-ci[bot]
authored
fix(query-core): allow partial query keys in QueryFilters (#9686)
* fix(query-core): allow partial query keys in `QueryFilters` and `MutationFilters` Add a type to match tuple prefixes to match partial query keys in `QueryFilters` and `MutationFilters` Fixes #9680 * test(query-core): add tests for partial QueryKey matching for `QueryFilters` add tests to verify that the new partial QueryKey matching for `QueryFilters` does match partial query keys, and also doesn't allow invalid query keys. * fix(query-core): move tuple prefix helper types to utils.ts prevent tuple helper types from getting exported, and showing up in the public api * Add changeset * ci: apply automated fixes --------- Co-authored-by: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 7bc9543 commit c0ec9fe

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

.changeset/wise-numbers-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-core': patch
3+
---
4+
5+
fix: allow partial query keys in `QueryFilters`

packages/query-core/src/__tests__/utils.test-d.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expectTypeOf, it } from 'vitest'
1+
import { assertType, describe, expectTypeOf, it } from 'vitest'
22
import { QueryClient } from '../queryClient'
33
import type { QueryFilters } from '../utils'
44
import type { DataTag, QueryKey } from '../types'
@@ -35,11 +35,36 @@ describe('QueryFilters', () => {
3535
}
3636

3737
const queryClient = new QueryClient()
38-
3938
const data = queryClient.getQueryData(a.queryKey!)
4039
expectTypeOf(data).toEqualTypeOf<unknown>()
4140

4241
const error = queryClient.getQueryState(a.queryKey!)?.error
4342
expectTypeOf(error).toEqualTypeOf<Error | null | undefined>()
4443
})
44+
45+
it('should allow a partial query key to be passed', () => {
46+
const filters: QueryFilters<readonly ['key', { a: number; b: string }]> = {
47+
queryKey: ['key'],
48+
}
49+
50+
expectTypeOf(filters.queryKey).toEqualTypeOf<
51+
| undefined
52+
| readonly []
53+
| ['key']
54+
| readonly [
55+
'key',
56+
{
57+
a: number
58+
b: string
59+
},
60+
]
61+
>()
62+
})
63+
64+
it('should error on invalid query keys', () => {
65+
assertType<QueryFilters<readonly ['key', { a: number; b: string }]>>({
66+
// @ts-expect-error cannot pass invalid query key
67+
queryKey: ['invalid', { a: 1, b: '1' }],
68+
})
69+
})
4570
})

packages/query-core/src/utils.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ import type { FetchOptions, Query } from './query'
1616

1717
// TYPES
1818

19+
type DropLast<T extends ReadonlyArray<unknown>> = T extends readonly [
20+
...infer R,
21+
unknown,
22+
]
23+
? R
24+
: never
25+
26+
type TuplePrefixes<T extends ReadonlyArray<unknown>> = T extends readonly []
27+
? readonly []
28+
: TuplePrefixes<DropLast<T>> | T
29+
1930
export interface QueryFilters<TQueryKey extends QueryKey = QueryKey> {
2031
/**
2132
* Filter to active queries, inactive queries or all queries
@@ -32,7 +43,7 @@ export interface QueryFilters<TQueryKey extends QueryKey = QueryKey> {
3243
/**
3344
* Include queries matching this query key
3445
*/
35-
queryKey?: TQueryKey
46+
queryKey?: TuplePrefixes<TQueryKey>
3647
/**
3748
* Include or exclude stale queries
3849
*/
@@ -62,7 +73,7 @@ export interface MutationFilters<
6273
/**
6374
* Include mutations matching this mutation key
6475
*/
65-
mutationKey?: MutationKey
76+
mutationKey?: TuplePrefixes<MutationKey>
6677
/**
6778
* Filter by mutation status
6879
*/

0 commit comments

Comments
 (0)