Open History to see the last commit
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, 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: () => DefinedInitialDataOptions<
60 TQueryFnData,
61 TError,
62 TData,
63 TQueryKey
64 >,
65 injector?: Injector,
66): DefinedCreateQueryResult<TData, TError>
67β
68/**
69 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
70 *
71 * **Basic example**
72 * ```ts
73 * class ServiceOrComponent {
74 * query = injectQuery(() => ({
75 * queryKey: ['repoData'],
76 * queryFn: () =>
77 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
78 * }))
79 * }
80 * ```
81 *
82 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
83 * In the example below, the query will be automatically enabled and executed when the filter signal changes
84 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
85 *
86 * **Reactive example**
87 * ```ts
88 * class ServiceOrComponent {
89 * filter = signal('')
90 *
91 * todosQuery = injectQuery(() => ({
92 * queryKey: ['todos', this.filter()],
93 * queryFn: () => fetchTodos(this.filter()),
94 * // Signals can be combined with expressions
95 * enabled: !!this.filter(),
96 * }))
97 * }
98 * ```
99 * @param optionsFn - A function that returns query options.
100 * @param injector - The Angular injector to use.
101 * @returns The query result.
102 * @public
103 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
104 */
105export function injectQuery<
106 TQueryFnData = unknown,
107 TError = DefaultError,
108 TData = TQueryFnData,
109 TQueryKey extends QueryKey = QueryKey,
110>(
111 optionsFn: () => UndefinedInitialDataOptions<
112 TQueryFnData,
113 TError,
114 TData,
115 TQueryKey
116 >,
117 injector?: Injector,
118): CreateQueryResult<TData, TError>
119β
120/**
121 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
122 *
123 * **Basic example**
124 * ```ts
125 * class ServiceOrComponent {
126 * query = injectQuery(() => ({
127 * queryKey: ['repoData'],
128 * queryFn: () =>
129 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
130 * }))
131 * }
132 * ```
133 *
134 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
135 * In the example below, the query will be automatically enabled and executed when the filter signal changes
136 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
137 *
138 * **Reactive example**
139 * ```ts
140 * class ServiceOrComponent {
141 * filter = signal('')
142 *
143 * todosQuery = injectQuery(() => ({
144 * queryKey: ['todos', this.filter()],
145 * queryFn: () => fetchTodos(this.filter()),
146 * // Signals can be combined with expressions
147 * enabled: !!this.filter(),
148 * }))
149 * }
150 * ```
151 * @param optionsFn - A function that returns query options.
152 * @param injector - The Angular injector to use.
153 * @returns The query result.
154 * @public
155 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
156 */
157export function injectQuery<
158 TQueryFnData = unknown,
159 TError = DefaultError,
160 TData = TQueryFnData,
161 TQueryKey extends QueryKey = QueryKey,
162>(
163 optionsFn: () => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
164 injector?: Injector,
165): CreateQueryResult<TData, TError>
166β
167/**
168 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
169 *
170 * **Basic example**
171 * ```ts
172 * class ServiceOrComponent {
173 * query = injectQuery(() => ({
174 * queryKey: ['repoData'],
175 * queryFn: () =>
176 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
177 * }))
178 * }
179 * ```
180 *
181 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
182 * In the example below, the query will be automatically enabled and executed when the filter signal changes
183 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
184 *
185 * **Reactive example**
186 * ```ts
187 * class ServiceOrComponent {
188 * filter = signal('')
189 *
190 * todosQuery = injectQuery(() => ({
191 * queryKey: ['todos', this.filter()],
192 * queryFn: () => fetchTodos(this.filter()),
193 * // Signals can be combined with expressions
194 * enabled: !!this.filter(),
195 * }))
196 * }
197 * ```
198 * @param optionsFn - A function that returns query options.
199 * @param injector - The Angular injector to use.
200 * @returns The query result.
201 * @public
202 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
203 */
204export function injectQuery(
205 optionsFn: () => CreateQueryOptions,
206 injector?: Injector,
207) {
208 return assertInjector(injectQuery, injector, () =>
209 createBaseQuery(optionsFn, QueryObserver),
210 ) as unknown as CreateQueryResult
211}