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