Skip to content

Commit ef4de5e

Browse files
committed
docs: add docs for simplfied impertive query methods
1 parent 59cacda commit ef4de5e

18 files changed

Lines changed: 239 additions & 254 deletions

docs/eslint/stable-query-client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function App() {
5151
```tsx
5252
async function App() {
5353
const queryClient = new QueryClient()
54-
await queryClient.prefetchQuery(options)
54+
await queryClient.query(options)
5555
}
5656
```
5757

docs/framework/angular/guides/paginated-queries.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ export class PaginationExampleComponent {
8383
effect(() => {
8484
// Prefetch the next page!
8585
if (!this.query.isPlaceholderData() && this.query.data()?.hasMore) {
86-
this.#queryClient.prefetchQuery({
87-
queryKey: ['projects', this.page() + 1],
88-
queryFn: () => lastValueFrom(fetchProjects(this.page() + 1)),
89-
})
86+
void this.#queryClient
87+
.query({
88+
queryKey: ['projects', this.page() + 1],
89+
queryFn: () => lastValueFrom(fetchProjects(this.page() + 1)),
90+
})
91+
.catch(noop)
9092
}
9193
})
9294
}

docs/framework/angular/guides/query-options.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ref: docs/framework/react/guides/query-options.md
77
[//]: # 'Example1'
88

99
```ts
10-
import { queryOptions } from '@tanstack/angular-query-experimental'
10+
import { queryOptions, noop } from '@tanstack/angular-query-experimental'
1111

1212
@Injectable({
1313
providedIn: 'root',
@@ -38,7 +38,7 @@ queries = inject(QueriesService)
3838

3939
postQuery = injectQuery(() => this.queries.post(this.postId()))
4040

41-
queryClient.prefetchQuery(this.queries.post(23))
41+
queryClient.query(this.queries.post(23)).catch(noop)
4242
queryClient.setQueryData(this.queries.post(42).queryKey, newPost)
4343
```
4444

docs/framework/angular/typescript.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,11 @@ computed(() => {
174174

175175
## Typing Query Options
176176

177-
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:
177+
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. `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:
178178

179179
```ts
180+
import { noop } from '@tanstack/angular-query-experimental'
181+
180182
@Injectable({
181183
providedIn: 'root',
182184
})
@@ -215,7 +217,7 @@ export class Component {
215217
postQuery = injectQuery(this.optionsSignal)
216218

217219
someMethod() {
218-
this.queryClient.prefetchQuery(this.queries.post(23))
220+
void this.queryClient.query(this.queries.post(23)).catch(noop)
219221
}
220222
}
221223
```

docs/framework/react/guides/advanced-ssr.md

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ import {
116116
export async function getStaticProps() {
117117
const queryClient = new QueryClient()
118118

119-
await queryClient.prefetchQuery({
120-
queryKey: ['posts'],
121-
queryFn: getPosts,
122-
})
119+
await queryClient
120+
.query({
121+
queryKey: ['posts'],
122+
queryFn: getPosts,
123+
})
124+
.catch(noop)
123125

124126
return {
125127
props: {
@@ -172,10 +174,12 @@ import Posts from './posts'
172174
export default async function PostsPage() {
173175
const queryClient = new QueryClient()
174176

175-
await queryClient.prefetchQuery({
176-
queryKey: ['posts'],
177-
queryFn: getPosts,
178-
})
177+
await queryClient
178+
.query({
179+
queryKey: ['posts'],
180+
queryFn: getPosts,
181+
})
182+
.catch(noop)
179183

180184
return (
181185
// Neat! Serialization is now as easy as passing props.
@@ -237,10 +241,12 @@ import CommentsServerComponent from './comments-server'
237241
export default async function PostsPage() {
238242
const queryClient = new QueryClient()
239243

240-
await queryClient.prefetchQuery({
241-
queryKey: ['posts'],
242-
queryFn: getPosts,
243-
})
244+
await queryClient
245+
.query({
246+
queryKey: ['posts'],
247+
queryFn: getPosts,
248+
})
249+
.catch(noop)
244250

245251
return (
246252
<HydrationBoundary state={dehydrate(queryClient)}>
@@ -261,10 +267,12 @@ import Comments from './comments'
261267
export default async function CommentsServerComponent() {
262268
const queryClient = new QueryClient()
263269

264-
await queryClient.prefetchQuery({
265-
queryKey: ['posts-comments'],
266-
queryFn: getComments,
267-
})
270+
await queryClient
271+
.query({
272+
queryKey: ['posts-comments'],
273+
queryFn: getComments,
274+
})
275+
.catch(noop)
268276

269277
return (
270278
<HydrationBoundary state={dehydrate(queryClient)}>
@@ -325,8 +333,8 @@ import Posts from './posts'
325333
export default async function PostsPage() {
326334
const queryClient = new QueryClient()
327335

328-
// Note we are now using fetchQuery()
329-
const posts = await queryClient.fetchQuery({
336+
// Note we are getting the result from query
337+
const posts = await queryClient.query({
330338
queryKey: ['posts'],
331339
queryFn: getPosts,
332340
})
@@ -355,7 +363,7 @@ Using React Query with Server Components makes most sense if:
355363

356364
It's hard to give general advice on when it makes sense to pair React Query with Server Components and not. **If you are just starting out with a new Server Components app, we suggest you start out with any tools for data fetching your framework provides you with and avoid bringing in React Query until you actually need it.** This might be never, and that's fine, use the right tool for the job!
357365

358-
If you do use it, a good rule of thumb is to avoid `queryClient.fetchQuery` unless you need to catch errors. If you do use it, don't render its result on the server or pass the result to another component, even a Client Component one.
366+
If you do use it, a good rule of thumb is to avoid rendering the result of `queryClient.query` on the server or passing it to another component, even a Client Component one.
359367

360368
From the React Query perspective, treat Server Components as a place to prefetch data, nothing more.
361369

@@ -424,7 +432,7 @@ export function getQueryClient() {
424432

425433
> Note: This works in NextJs and Server Components because React can serialize Promises over the wire when you pass them down to Client Components.
426434
427-
Then, all we need to do is provide a `HydrationBoundary`, but we don't need to `await` prefetches anymore:
435+
Then, all we need to do is provide a `HydrationBoundary`, but we don't need to `await` these prefetches anymore:
428436

429437
```tsx
430438
// app/posts/page.tsx
@@ -437,10 +445,12 @@ export default function PostsPage() {
437445
const queryClient = getQueryClient()
438446

439447
// look ma, no await
440-
queryClient.prefetchQuery({
441-
queryKey: ['posts'],
442-
queryFn: getPosts,
443-
})
448+
void queryClient
449+
.query({
450+
queryKey: ['posts'],
451+
queryFn: getPosts,
452+
})
453+
.catch(noop)
444454

445455
return (
446456
<HydrationBoundary state={dehydrate(queryClient)}>
@@ -504,10 +514,12 @@ export default function PostsPage() {
504514
const queryClient = getQueryClient()
505515

506516
// look ma, no await
507-
queryClient.prefetchQuery({
508-
queryKey: ['posts'],
509-
queryFn: () => getPosts().then(serialize), // <-- serialize the data on the server
510-
})
517+
void queryClient
518+
.query({
519+
queryKey: ['posts'],
520+
queryFn: () => getPosts().then(serialize), // <-- serialize the data on the server
521+
})
522+
.catch(noop)
511523

512524
return (
513525
<HydrationBoundary state={dehydrate(queryClient)}>

docs/framework/react/guides/initial-query-data.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ There are many ways to supply initial data for a query to the cache before you n
88
- Declaratively:
99
- Provide `initialData` to a query to prepopulate its cache if empty
1010
- Imperatively:
11-
- [Prefetch the data using `queryClient.prefetchQuery`](./prefetching.md)
11+
- [Prefetch the data using `queryClient.query`](./prefetching.md)
1212
- [Manually place the data into the cache using `queryClient.setQueryData`](./prefetching.md)
1313

1414
## Using `initialData` to prepopulate a query
@@ -84,7 +84,7 @@ By default, `initialData` is treated as totally fresh, as if it were just fetche
8484

8585
This option allows the staleTime to be used for its original purpose, determining how fresh the data needs to be, while also allowing the data to be refetched on mount if the `initialData` is older than the `staleTime`. In the example above, our data needs to be fresh within 1 minute, and we can hint to the query when the initialData was last updated so the query can decide for itself whether the data needs to be refetched again or not.
8686

87-
> If you would rather treat your data as **prefetched data**, we recommend that you use the `prefetchQuery` or `fetchQuery` APIs to populate the cache beforehand, thus letting you configure your `staleTime` independently from your initialData
87+
> If you would rather treat your data as **prefetched data**, we recommend that you use the `query` api to populate the cache beforehand, thus letting you configure your `staleTime` independently from your `initialData`.
8888
8989
### Initial Data Function
9090

docs/framework/react/guides/migrating-to-v5.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ useIsMutating({ mutationKey, ...filters }) // [!code ++]
2929
```tsx
3030
queryClient.isFetching(key, filters) // [!code --]
3131
queryClient.isFetching({ queryKey, ...filters }) // [!code ++]
32-
queryClient.ensureQueryData(key, filters) // [!code --]
33-
queryClient.ensureQueryData({ queryKey, ...filters }) // [!code ++]
3432
queryClient.getQueriesData(key, filters) // [!code --]
3533
queryClient.getQueriesData({ queryKey, ...filters }) // [!code ++]
3634
queryClient.setQueriesData(key, updater, filters, options) // [!code --]
@@ -45,14 +43,6 @@ queryClient.invalidateQueries(key, filters, options) // [!code --]
4543
queryClient.invalidateQueries({ queryKey, ...filters }, options) // [!code ++]
4644
queryClient.refetchQueries(key, filters, options) // [!code --]
4745
queryClient.refetchQueries({ queryKey, ...filters }, options) // [!code ++]
48-
queryClient.fetchQuery(key, fn, options) // [!code --]
49-
queryClient.fetchQuery({ queryKey, queryFn, ...options }) // [!code ++]
50-
queryClient.prefetchQuery(key, fn, options) // [!code --]
51-
queryClient.prefetchQuery({ queryKey, queryFn, ...options }) // [!code ++]
52-
queryClient.fetchInfiniteQuery(key, fn, options) // [!code --]
53-
queryClient.fetchInfiniteQuery({ queryKey, queryFn, ...options }) // [!code ++]
54-
queryClient.prefetchInfiniteQuery(key, fn, options) // [!code --]
55-
queryClient.prefetchInfiniteQuery({ queryKey, queryFn, ...options }) // [!code ++]
5646
```
5747

5848
```tsx
@@ -62,6 +52,39 @@ queryCache.findAll(key, filters) // [!code --]
6252
queryCache.findAll({ queryKey, ...filters }) // [!code ++]
6353
```
6454

55+
### Imperative QueryClient methods
56+
57+
These methods are deprecated as of Tanstack Query `INSERT_FUTURE_V5_MINOR` and will be removed in v6.
58+
59+
If you are coming from v4 or earlier:
60+
61+
```tsx
62+
queryClient.fetchQuery(key, fn, options) // [!code --]
63+
queryClient.query({ queryKey: key, queryFn: fn, ...options }) // [!code ++]
64+
queryClient.fetchInfiniteQuery(key, fn, options) // [!code --]
65+
queryClient.infiniteQuery({
66+
queryKey: key,
67+
queryFn: fn,
68+
...options,
69+
}) // [!code ++]
70+
71+
queryClient.prefetchQuery(key, fn, options) // [!code --]
72+
queryClient.query({ queryKey: key, queryFn: fn, ...options }).catch(noop) // [!code ++]
73+
74+
queryClient.prefetchInfiniteQuery(key, fn, options) // [!code --]
75+
queryClient
76+
.infiniteQuery({ queryKey: key, queryFn: fn, ...options })
77+
.catch(noop) // [!code ++]
78+
79+
queryClient.ensureQueryData(key, options) // [!code --]
80+
queryClient.query({ queryKey: key, ...options, staleTime: 'static' }) // [!code ++]
81+
82+
queryClient.ensureInfiniteQueryData(key, options) // [!code --]
83+
queryClient.infiniteQuery({ queryKey: key, ...options, staleTime: 'static' }) // [!code ++]
84+
```
85+
86+
If you are updating older v5 code, It will be the same as the above except for keeping the single options object
87+
6588
### `queryClient.getQueryData` now accepts queryKey only as an Argument
6689

6790
`queryClient.getQueryData` argument is changed to accept only a `queryKey`

0 commit comments

Comments
 (0)