Open History to see the last commit
1---
2id: query-options
3title: Query Options
4ref: docs/framework/react/guides/query-options.md
5---
6β
7[//]: # 'Example1'
8β
9```ts
10import { noop, queryOptions } from '@tanstack/angular-query'
11β
12@Injectable({
13 providedIn: 'root',
14})
15export class QueriesService {
16 private readonly http = inject(HttpClient)
17β
18 post(postId: number) {
19 return queryOptions({
20 queryKey: ['post', postId],
21 queryFn: () => {
22 return lastValueFrom(
23 this.http.get<Post>(
24 `https://jsonplaceholder.typicode.com/posts/${postId}`,
25 ),
26 )
27 },
28 })
29 }
30}
31β
32// usage:
33β
34postId = input.required({
35 transform: numberAttribute,
36})
37queries = inject(QueriesService)
38β
39postQuery = injectQuery(() => this.queries.post(this.postId()))
40β
41void queryClient.query(this.queries.post(23)).catch(noop)
42queryClient.setQueryData(this.queries.post(42).queryKey, newPost)
43```
44β
45[//]: # 'Example1'
46[//]: # 'Example2'
47β
48```ts
49// Type inference still works, so postQuery.data will be the return type of select instead of queryFn
50queries = inject(QueriesService)
51β
52postQuery = injectQuery(() => ({
53 ...this.queries.post(1),
54 select: (data) => data.title,
55}))
56```
57β
58[//]: # 'Example2'