- Angular Reference
- Functions / defaultShouldDehydrateMutation
- Functions / defaultShouldDehydrateQuery
- Functions / dehydrate
- Functions / dehydrateQuery
- Functions / experimental_streamedQuery
- Functions / hashKey
- Functions / hydrate
- Functions / infiniteQueryOptions
- Functions / injectInfiniteQuery
- Functions / injectIsFetching
- Functions / injectIsMutating
- Functions / injectIsRestoring
- Functions / injectMutation
- Functions / injectMutationState
- Functions / injectQueries
- Functions / injectQuery
- Functions / isCancelledError
- Functions / keepPreviousData
- Functions / matchMutation
- Functions / matchQuery
- Functions / mutationOptions
- Functions / noop
- Functions / partialMatchKey
- Functions / provideTanStackQuery
- Functions / queryOptions
- Functions / replaceEqualDeep
- Functions / shouldThrowError
- Functions / toResource
- Functions / withHydrationKey
- Functions / withNoQueryHydration
- Classes / CancelledError
- Classes / InfiniteQueryObserver
- Classes / Mutation
- Classes / MutationCache
- Classes / MutationObserver
- Classes / QueriesObserver
- Classes / Query
- Classes / QueryCache
- Classes / QueryClient
- Classes / QueryObserver
- Interfaces / BaseInfiniteQueryNarrowing
- Interfaces / BaseMutationNarrowing
- Interfaces / BaseQueryNarrowing
- Interfaces / CancelOptions
- Interfaces / CreateInfiniteQueryOptions
- Interfaces / CreateMutationOptions
- Interfaces / DefaultOptions
- Interfaces / DefinedInfiniteQueryNarrowing
- Interfaces / DefinedQueryNarrowing
- Interfaces / DehydrateOptions
- Interfaces / DehydratedState
- Interfaces / EnsureQueryDataOptions
- Interfaces / FetchNextPageOptions
- Interfaces / FetchPreviousPageOptions
- Interfaces / FetchQueryOptions
- Interfaces / FocusManager
- Interfaces / HydrateOptions
- Interfaces / InfiniteData
- Interfaces / InfiniteQueryObserverBaseResult
- Interfaces / InfiniteQueryObserverLoadingErrorResult
- Interfaces / InfiniteQueryObserverLoadingResult
- Interfaces / InfiniteQueryObserverOptions
- Interfaces / InfiniteQueryObserverPendingResult
- Interfaces / InfiniteQueryObserverPlaceholderResult
- Interfaces / InfiniteQueryObserverRefetchErrorResult
- Interfaces / InfiniteQueryObserverSuccessResult
- Interfaces / InfiniteQueryPageParamsOptions
- Interfaces / InitialPageParam
- Interfaces / InjectQueriesOptions
- Interfaces / InvalidateOptions
- Interfaces / InvalidateQueryFilters
- Interfaces / MutateOptions
- Interfaces / MutationCacheConfig
- Interfaces / MutationFilters
- Interfaces / MutationObserverBaseResult
- Interfaces / MutationObserverErrorResult
- Interfaces / MutationObserverIdleResult
- Interfaces / MutationObserverLoadingResult
- Interfaces / MutationObserverOptions
- Interfaces / MutationObserverSuccessResult
- Interfaces / MutationOptions
- Interfaces / MutationState
- Interfaces / NotifyEvent
- Interfaces / OnlineManager
- Interfaces / QueriesObserverOptions
- Interfaces / QueryCacheConfig
- Interfaces / QueryClientConfig
- Interfaces / QueryExecuteOptions
- Interfaces / QueryFeature
- Interfaces / QueryFilters
- Interfaces / QueryObserverBaseResult
- Interfaces / QueryObserverLoadingErrorResult
- Interfaces / QueryObserverLoadingResult
- Interfaces / QueryObserverOptions
- Interfaces / QueryObserverPendingResult
- Interfaces / QueryObserverPlaceholderResult
- Interfaces / QueryObserverRefetchErrorResult
- Interfaces / QueryObserverSuccessResult
- Interfaces / QueryOptions
- Interfaces / QueryResource
- Interfaces / QueryState
- Interfaces / RefetchOptions
- Interfaces / RefetchQueryFilters
- Interfaces / Register
- Interfaces / ResetOptions
- Interfaces / ResultOptions
- Interfaces / SetDataOptions
- Interfaces / TimeoutManager
- Types / AnyDataTag
- Types / CreateBaseMutationResult
- Types / CreateBaseQueryResult
- Types / CreateInfiniteQueryResult
- Types / CreateMutateAsyncFunction
- Types / CreateMutateFunction
- Types / CreateMutationResult
- Types / CreateQueryOptions
- Types / CreateQueryResult
- Types / DataTag
- Types / DefaultError
- Types / DefaultedInfiniteQueryObserverOptions
- Types / DefaultedQueryObserverOptions
- Types / DefinedCreateInfiniteQueryResult
- Types / DefinedCreateQueryResult
- Types / DefinedInfiniteQueryObserverResult
- Types / DefinedInitialDataInfiniteOptions
- Types / DefinedInitialDataOptions
- Types / DefinedQueryObserverResult
- Types / DistributiveOmit
- Types / EnsureInfiniteQueryDataOptions
- Types / FetchInfiniteQueryOptions
- Types / FetchStatus
- Types / GetNextPageParamFunction
- Types / GetPreviousPageParamFunction
- Types / InferDataFromTag
- Types / InferErrorFromTag
- Types / InfiniteQueryExecuteOptions
- Types / InfiniteQueryObserverResult
- Types / InitialDataFunction
- Types / ManagedTimerId
- Types / MutateFunction
- Types / MutateFunctionRest
- Types / MutationCacheNotifyEvent
- Types / MutationFunction
- Types / MutationFunctionContext
- Types / MutationKey
- Types / MutationMeta
- Types / MutationObserverResult
- Types / MutationScope
- Types / MutationStateOptions
- Types / MutationStatus
- Types / NetworkMode
- Types / NonUndefinedGuard
- Types / NotifyEventType
- Types / NotifyOnChangeProps
- Types / OmitKeyof
- Types / Override
- Types / PlaceholderDataFunction
- Types / QueriesOptions
- Types / QueriesPlaceholderDataFunction
- Types / QueriesResults
- Types / QueryBooleanOption
- Types / QueryCacheNotifyEvent
- Types / QueryFunction
- Types / QueryFunctionContext
- Types / QueryKey
- Types / QueryKeyHashFunction
- Types / QueryKeyWithDataTag
- Types / QueryMeta
- Types / QueryObserverResult
- Types / QueryPersister
- Types / QueryStatus
- Types / SkipToken
- Types / StaleTime
- Types / StaleTimeFunction
- Types / ThrowOnError
- Types / TimeoutCallback
- Types / TimeoutProvider
- Types / UndefinedInitialDataInfiniteOptions
- Types / UndefinedInitialDataOptions
- Types / UnsetMarker
- Types / UnusedSkipTokenInfiniteOptions
- Types / UnusedSkipTokenOptions
- Types / Updater
- Types / WithRequired
- Types / dataTagErrorSymbol
- Types / dataTagSymbol
- Variables / dataTagErrorSymbol
- Variables / dataTagSymbol
- Variables / defaultScheduler
- Variables / environmentManager
- Variables / focusManager
- Variables / isServer
- Variables / notifyManager
- Variables / onlineManager
- Variables / skipToken
- Variables / timeoutManager
- Variables / unsetMarker
injectQuery
Overview#
function injectQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(optionsFn: () => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedCreateQueryResult<TData, TError>;
function injectQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(optionsFn: () => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): CreateQueryResult<TData, TError>;
function injectQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(optionsFn: () => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>): CreateQueryResult<TData, TError>;- () => DefinedInitialDataOptions → DefinedCreateQueryResult: This overload is selected when initialData is known to be defined on the options returned by optionsFn, so the resulting data signal is never undefined (unless a select changes TData to include undefined).
- () => UndefinedInitialDataOptions → CreateQueryResult: Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
- () => CreateQueryOptions → CreateQueryResult: This overload accepts the general CreateQueryOptions shape rather than the initialData-aware overloads above, so whether data is defined can't be inferred from the call site — useful when wrapping injectQuery in your own helper function that forwards caller-provided options.
See also: Parameters · Returns
Call Signature#
function injectQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(optionsFn: () => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedCreateQueryResult<TData, TError>;Defined in: packages/angular-query/src/inject-query.ts:59
This overload is selected when initialData is known to be defined on the options returned by optionsFn, so the resulting data signal is never undefined (unless a select changes TData to include undefined).
Type Parameters#
TQueryFnData#
TQueryFnData = unknown
TError#
TError = Error
TData#
TData = TQueryFnData
TQueryKey#
TQueryKey extends readonly unknown[] = readonly unknown[]
Parameters#
optionsFn#
() => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
A function returning the DefinedInitialDataOptions to use — everything you can pass to injectQuery, with initialData known to be defined. Similar to computed from Angular, this function runs in the reactive context, so signals read inside it (in queryKey, enabled, etc.) drive the query.
Returns#
DefinedCreateQueryResult<TData, TError>
The query result, typed so that data is never undefined (unless a select changes TData to include undefined). At runtime, data is still undefined if select throws on its first call.
See#
- https://tanstack.com/query/latest/docs/framework/angular/guides/queries
- queryOptions to share these options between injectQuery and imperative APIs like queryClient.query.
Example#
@Component({
selector: 'posts',
template: `
<!-- `postsQuery.data()` is `Post[]`, never `undefined`, thanks to `initialData` — even if a
refetch fails, so the list stays visible alongside the error. -->
@if (postsQuery.isError()) {
<span>Error: {{ postsQuery.error()?.message }}</span>
}
<ul>
@for (post of postsQuery.data(); track post.id) {
<li>{{ post.title }}</li>
}
</ul>
`,
})
export class Posts {
readonly postsQuery = injectQuery(() => ({
queryKey: ['posts'],
queryFn: fetchPosts,
initialData: [],
}))
}Call Signature#
function injectQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(optionsFn: () => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): CreateQueryResult<TData, TError>;Defined in: packages/angular-query/src/inject-query.ts:144
Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
Type Parameters#
TQueryFnData#
TQueryFnData = unknown
TError#
TError = Error
TData#
TData = TQueryFnData
TQueryKey#
TQueryKey extends readonly unknown[] = readonly unknown[]
Parameters#
optionsFn#
() => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
A function returning the UndefinedInitialDataOptions to use — everything you can pass to injectQuery. Similar to computed from Angular, this function runs in the reactive context, so signals read inside it (in queryKey, enabled, etc.) drive the query.
Returns#
CreateQueryResult<TData, TError>
The query result. status() is 'error' if the query has an error from a fetch that failed after its retries or if select threw, 'success' if the query has cached data (even if select returns undefined) or data from placeholderData, and 'pending' otherwise. isPending/isSuccess/isError are type-guard methods for convenience.
See#
- https://tanstack.com/query/latest/docs/framework/angular/guides/queries
- queryOptions to share these options between injectQuery and imperative APIs like queryClient.query.
Examples#
@Component({
selector: 'posts',
template: `
@if (postsQuery.isPending()) {
Loading...
} @else if (postsQuery.isError()) {
<span>Error: {{ postsQuery.error()?.message }}</span>
} @else {
<ul>
@for (post of postsQuery.data(); track post.id) {
<li>{{ post.title }}</li>
}
</ul>
}
`,
})
export class Posts {
readonly postsQuery = injectQuery(() => ({
queryKey: ['posts'],
queryFn: fetchPosts,
}))
}Similar to computed from Angular, the function passed to injectQuery runs in the reactive context. In the example below, the query is automatically enabled and executed when the filter signal changes to a truthy value. When the filter signal changes back to a falsy value, the query is disabled.
@Component({
selector: 'posts',
template: `
<input [ngModel]="filter()" (ngModelChange)="filter.set($event)" />
@if (postsQuery.isPending()) {
Loading...
} @else if (postsQuery.isError()) {
<span>Error: {{ postsQuery.error()?.message }}</span>
} @else {
<ul>
@for (post of postsQuery.data(); track post.id) {
<li>{{ post.title }}</li>
}
</ul>
}
`,
})
export class Posts {
readonly filter = signal('')
readonly postsQuery = injectQuery(() => ({
queryKey: ['posts', this.filter()],
queryFn: () => fetchPosts(this.filter()),
// Signals can be combined with expressions
enabled: !!this.filter(),
}))
}Call Signature#
function injectQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(optionsFn: () => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>): CreateQueryResult<TData, TError>;Defined in: packages/angular-query/src/inject-query.ts:168
This overload accepts the general CreateQueryOptions shape rather than the initialData-aware overloads above, so whether data is defined can't be inferred from the call site — useful when wrapping injectQuery in your own helper function that forwards caller-provided options.
Type Parameters#
TQueryFnData#
TQueryFnData = unknown
TError#
TError = Error
TData#
TData = TQueryFnData
TQueryKey#
TQueryKey extends readonly unknown[] = readonly unknown[]
Parameters#
optionsFn#
() => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
A function that returns query options. Similar to computed from Angular, this function runs in the reactive context, so signals read inside it (in queryKey, enabled, etc.) drive the query.
Returns#
CreateQueryResult<TData, TError>
The query result.
See#
https://tanstack.com/query/latest/docs/framework/angular/guides/queries
Parameters#
optionsFn#
() => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
A function that returns query options. Similar to computed from Angular, this function runs in the reactive context, so signals read inside it (in queryKey, enabled, etc.) drive the query.
optionsFn properties#
Built from QueryObserverOptions. See the type above for what it changes.
Returns#
CreateQueryResult<TData, TError>
The query result.