-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(query-core): add query() and infiniteQuery() methods #9835
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
base: main
Are you sure you want to change the base?
Changes from all commits
6dd8fbf
c824b11
4f5f27a
c7bea26
269ed8e
f0217b4
19b13cf
311b3ba
fd8ea67
058fd5c
a2c989b
a78d0fe
de3f12d
1ed5851
5c40184
bf15113
88d795b
a905c54
f89cc80
fedf7b5
a743447
c7baef7
0c6ea77
3b0207d
984739c
08882ab
c141fe9
8c00297
09c7acd
3e6d373
d9f05cc
ce4dcb6
6c25c3c
4d70a42
21be54a
c44375a
bbdad0a
f6f4df2
43b553a
cf7b252
6ccd459
fff81d9
64547d4
6862fec
d8c3b75
c9f7020
f9fd762
8e7a2a7
f47d9ba
246afbd
a6289bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@tanstack/query-core': minor | ||
| '@tanstack/vue-query': minor | ||
| --- | ||
|
|
||
| add query() and infiniteQuery() imperative methods to QueryClient |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -174,7 +174,7 @@ computed(() => { | |||||||
|
|
||||||||
| ## Typing Query Options | ||||||||
|
|
||||||||
| If you inline query options into `injectQuery`, you'll get automatic type inference. However, you might want to extract the query options into a separate function to share them between `injectQuery` and e.g. `prefetchQuery` or manage them in a service. In that case, you'd lose type inference. To get it back, you can use the `queryOptions` helper: | ||||||||
| If you inline query options into `injectQuery`, you'll get automatic type inference. However, you might want to extract the query options into a separate function to share them between `injectQuery` and imperative calls like `queryClient.query`, or manage them in a service. In that case, you'd lose type inference. To get it back, you can use the `queryOptions` helper: | ||||||||
|
|
||||||||
| ```ts | ||||||||
| @Injectable({ | ||||||||
|
|
@@ -215,11 +215,13 @@ export class Component { | |||||||
| postQuery = injectQuery(this.optionsSignal) | ||||||||
|
|
||||||||
| someMethod() { | ||||||||
| this.queryClient.prefetchQuery(this.queries.post(23)) | ||||||||
| this.queryClient.query(this.queries.post(23)).catch(noop) | ||||||||
| } | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| Because `queryClient.query` preserves `select` and `enabled`, the extracted options behave the same way in both places. The legacy `fetchQuery` and `prefetchQuery` APIs still accept those options at the type level, but they ignore `select` and `enabled` at runtime. | ||||||||
|
|
||||||||
|
Comment on lines
+223
to
+224
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Runtime behavior note is now inaccurate for Line 223 says legacy APIs ignore Suggested docs correction-Because `queryClient.query` preserves `select` and `enabled`, the extracted options behave the same way in both places. The legacy `fetchQuery` and `prefetchQuery` APIs still accept those options at the type level, but they ignore `select` and `enabled` at runtime.
+Because `queryClient.query` preserves `select` and `enabled`, the extracted options behave the same way in both places. `fetchQuery` and `prefetchQuery` are legacy aliases and follow the same runtime behavior.π Committable suggestion
Suggested change
π€ Prompt for AI Agents |
||||||||
| Further, the `queryKey` returned from `queryOptions` knows about the `queryFn` associated with it, and we can leverage that type information to make functions like `queryClient.getQueryData` aware of those types as well: | ||||||||
|
|
||||||||
| ```ts | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,8 +29,6 @@ useIsMutating({ mutationKey, ...filters }) // [!code ++] | |
| ```tsx | ||
| queryClient.isFetching(key, filters) // [!code --] | ||
| queryClient.isFetching({ queryKey, ...filters }) // [!code ++] | ||
| queryClient.ensureQueryData(key, filters) // [!code --] | ||
| queryClient.ensureQueryData({ queryKey, ...filters }) // [!code ++] | ||
| queryClient.getQueriesData(key, filters) // [!code --] | ||
| queryClient.getQueriesData({ queryKey, ...filters }) // [!code ++] | ||
| queryClient.setQueriesData(key, updater, filters, options) // [!code --] | ||
|
|
@@ -45,14 +43,6 @@ queryClient.invalidateQueries(key, filters, options) // [!code --] | |
| queryClient.invalidateQueries({ queryKey, ...filters }, options) // [!code ++] | ||
| queryClient.refetchQueries(key, filters, options) // [!code --] | ||
| queryClient.refetchQueries({ queryKey, ...filters }, options) // [!code ++] | ||
| queryClient.fetchQuery(key, fn, options) // [!code --] | ||
| queryClient.fetchQuery({ queryKey, queryFn, ...options }) // [!code ++] | ||
| queryClient.prefetchQuery(key, fn, options) // [!code --] | ||
| queryClient.prefetchQuery({ queryKey, queryFn, ...options }) // [!code ++] | ||
| queryClient.fetchInfiniteQuery(key, fn, options) // [!code --] | ||
| queryClient.fetchInfiniteQuery({ queryKey, queryFn, ...options }) // [!code ++] | ||
| queryClient.prefetchInfiniteQuery(key, fn, options) // [!code --] | ||
| queryClient.prefetchInfiniteQuery({ queryKey, queryFn, ...options }) // [!code ++] | ||
| ``` | ||
|
|
||
| ```tsx | ||
|
|
@@ -62,6 +52,39 @@ queryCache.findAll(key, filters) // [!code --] | |
| queryCache.findAll({ queryKey, ...filters }) // [!code ++] | ||
| ``` | ||
|
|
||
| ### Imperative QueryClient methods | ||
|
|
||
| These methods are deprecated as of Tanstack Query `INSERT_FUTURE_V5_MINOR` and will be removed in v6. | ||
|
|
||
| If you are coming from v4 or earlier: | ||
|
|
||
| ```tsx | ||
| queryClient.fetchQuery(key, fn, options) // [!code --] | ||
| queryClient.query({ queryKey: key, queryFn: fn, ...options }) // [!code ++] | ||
| queryClient.fetchInfiniteQuery(key, fn, options) // [!code --] | ||
| queryClient.infiniteQuery({ | ||
| queryKey: key, | ||
| queryFn: fn, | ||
| ...options, | ||
| }) // [!code ++] | ||
|
|
||
| queryClient.prefetchQuery(key, fn, options) // [!code --] | ||
| queryClient.query({ queryKey: key, queryFn: fn, ...options }).catch(noop) // [!code ++] | ||
|
|
||
| queryClient.prefetchInfiniteQuery(key, fn, options) // [!code --] | ||
| queryClient | ||
| .infiniteQuery({ queryKey: key, queryFn: fn, ...options }) | ||
| .catch(noop) // [!code ++] | ||
|
|
||
| queryClient.ensureQueryData(key, options) // [!code --] | ||
| queryClient.query({ queryKey: key, ...options, staleTime: 'static' }) // [!code ++] | ||
|
|
||
| queryClient.ensureInfiniteQueryData(key, options) // [!code --] | ||
| queryClient.infiniteQuery({ queryKey: key, ...options, staleTime: 'static' }) // [!code ++] | ||
| ``` | ||
|
|
||
| If you are updating older v5 code, It will be the same as the above except for keeping the single options object | ||
|
|
||
|
Comment on lines
+55
to
+87
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Placeholder version number needs to be filled in. Line 57 contains Additionally, the migration examples are comprehensive and clearly show:
π€ Prompt for AI Agents |
||
| ### `queryClient.getQueryData` now accepts queryKey only as an Argument | ||
|
|
||
| `queryClient.getQueryData` argument is changed to accept only a `queryKey` | ||
|
|
@@ -494,7 +517,9 @@ Note that the infinite list must be bi-directional, which requires both `getNext | |
|
|
||
| ### Infinite Queries can prefetch multiple pages | ||
|
|
||
| Infinite Queries can be prefetched like regular Queries. Per default, only the first page of the Query will be prefetched and will be stored under the given QueryKey. If you want to prefetch more than one page, you can use the `pages` option. Read the [prefetching guide](./prefetching.md) for more information. | ||
| Infinite Queries can be prefetched like regular Queries. Per default, only the first page of the Query will be prefetched and will be stored under the given QueryKey. If you want to prefetch more than one page, you can use the `pages` option. | ||
|
|
||
| If you are updating older v5 examples, prefer `queryClient.infiniteQuery(...)` here instead of `queryClient.prefetchInfiniteQuery(...)`. Like the other legacy imperative methods, `prefetchInfiniteQuery` is deprecated as of `INSERT_FUTURE_V5_MINOR`. Read the [prefetching guide](./prefetching.md) for the current pattern. | ||
|
|
||
| ### New `combine` option for `useQueries` | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π§© Analysis chain
π Script executed:
Repository: TanStack/query
Length of output: 262
π Script executed:
Repository: TanStack/query
Length of output: 1628
π Script executed:
Repository: TanStack/query
Length of output: 113
π Script executed:
Repository: TanStack/query
Length of output: 40
π Script executed:
Repository: TanStack/query
Length of output: 474
noopis undefined in this code sample.The code snippet shows a usage example but does not define or import
noop, making the example non-functional. Replace with an inline error handler:Suggested fix
π Committable suggestion
π€ Prompt for AI Agents