Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

inject-query.ts

207 lines6.6 KB

1import { QueryObserver } from '@tanstack/query-core'
2import { assertInjector } from './util/assert-injector/assert-injector'
3import { createBaseQuery } from './create-base-query'
4import type { Injector } from '@angular/core'
5import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
6import type {
7 CreateQueryOptions,
8 CreateQueryResult,
9 DefinedCreateQueryResult,
10} from './types'
11import type {
12 DefinedInitialDataOptions,
13 UndefinedInitialDataOptions,
14} from './query-options'
15​
16/**
17 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
18 *
19 * **Basic example**
20 * ```ts
21 * class ServiceOrComponent {
22 * query = injectQuery(() => ({
23 * queryKey: ['repoData'],
24 * queryFn: () =>
25 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
26 * }))
27 * }
28 * ```
29 *
30 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
31 * In the example below, the query will be automatically enabled and executed when the filter signal changes
32 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
33 *
34 * **Reactive example**
35 * ```ts
36 * class ServiceOrComponent {
37 * filter = signal('')
38 *
39 * todosQuery = injectQuery(() => ({
40 * queryKey: ['todos', this.filter()],
41 * queryFn: () => fetchTodos(this.filter()),
42 * // Signals can be combined with expressions
43 * enabled: !!this.filter(),
44 * }))
45 * }
46 * ```
47 * @param optionsFn - A function that returns query options.
48 * @param injector - The Angular injector to use.
49 * @returns The query result.
50 * @public
51 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
52 */
53export function injectQuery<
54 TQueryFnData = unknown,
55 TError = DefaultError,
56 TData = TQueryFnData,
57 TQueryKey extends QueryKey = QueryKey,
58>(
59 optionsFn: (
60 client: QueryClient,
61 ) => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
62 injector?: Injector,
63): DefinedCreateQueryResult<TData, TError>
64​
65/**
66 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
67 *
68 * **Basic example**
69 * ```ts
70 * class ServiceOrComponent {
71 * query = injectQuery(() => ({
72 * queryKey: ['repoData'],
73 * queryFn: () =>
74 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
75 * }))
76 * }
77 * ```
78 *
79 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
80 * In the example below, the query will be automatically enabled and executed when the filter signal changes
81 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
82 *
83 * **Reactive example**
84 * ```ts
85 * class ServiceOrComponent {
86 * filter = signal('')
87 *
88 * todosQuery = injectQuery(() => ({
89 * queryKey: ['todos', this.filter()],
90 * queryFn: () => fetchTodos(this.filter()),
91 * // Signals can be combined with expressions
92 * enabled: !!this.filter(),
93 * }))
94 * }
95 * ```
96 * @param optionsFn - A function that returns query options.
97 * @param injector - The Angular injector to use.
98 * @returns The query result.
99 * @public
100 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
101 */
102export function injectQuery<
103 TQueryFnData = unknown,
104 TError = DefaultError,
105 TData = TQueryFnData,
106 TQueryKey extends QueryKey = QueryKey,
107>(
108 optionsFn: (
109 client: QueryClient,
110 ) => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
111 injector?: Injector,
112): CreateQueryResult<TData, TError>
113​
114/**
115 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
116 *
117 * **Basic example**
118 * ```ts
119 * class ServiceOrComponent {
120 * query = injectQuery(() => ({
121 * queryKey: ['repoData'],
122 * queryFn: () =>
123 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
124 * }))
125 * }
126 * ```
127 *
128 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
129 * In the example below, the query will be automatically enabled and executed when the filter signal changes
130 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
131 *
132 * **Reactive example**
133 * ```ts
134 * class ServiceOrComponent {
135 * filter = signal('')
136 *
137 * todosQuery = injectQuery(() => ({
138 * queryKey: ['todos', this.filter()],
139 * queryFn: () => fetchTodos(this.filter()),
140 * // Signals can be combined with expressions
141 * enabled: !!this.filter(),
142 * }))
143 * }
144 * ```
145 * @param optionsFn - A function that returns query options.
146 * @param injector - The Angular injector to use.
147 * @returns The query result.
148 * @public
149 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
150 */
151export function injectQuery<
152 TQueryFnData = unknown,
153 TError = DefaultError,
154 TData = TQueryFnData,
155 TQueryKey extends QueryKey = QueryKey,
156>(
157 optionsFn: (
158 client: QueryClient,
159 ) => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
160 injector?: Injector,
161): CreateQueryResult<TData, TError>
162​
163/**
164 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
165 *
166 * **Basic example**
167 * ```ts
168 * class ServiceOrComponent {
169 * query = injectQuery(() => ({
170 * queryKey: ['repoData'],
171 * queryFn: () =>
172 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
173 * }))
174 * }
175 * ```
176 *
177 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
178 * In the example below, the query will be automatically enabled and executed when the filter signal changes
179 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
180 *
181 * **Reactive example**
182 * ```ts
183 * class ServiceOrComponent {
184 * filter = signal('')
185 *
186 * todosQuery = injectQuery(() => ({
187 * queryKey: ['todos', this.filter()],
188 * queryFn: () => fetchTodos(this.filter()),
189 * // Signals can be combined with expressions
190 * enabled: !!this.filter(),
191 * }))
192 * }
193 * ```
194 * @param optionsFn - A function that returns query options.
195 * @param injector - The Angular injector to use.
196 * @returns The query result.
197 * @public
198 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
199 */
200export function injectQuery(
201 optionsFn: (client: QueryClient) => CreateQueryOptions,
202 injector?: Injector,
203) {
204 return assertInjector(injectQuery, injector, () =>
205 createBaseQuery(optionsFn, QueryObserver),
206 )