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

inject-query.ts

230 lines6.9 KB

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