Skip to content
Source
Sign inSign up
TanStack/query

packages/angular-query

README.md

TanStack Angular Query

npm version npm license bundle size npm

Angular Query

Functions for fetching, caching and updating asynchronous data in Angular

Documentation

Visit https://tanstack.com/query/latest/docs/framework/angular/overview

Quick Features

  • Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
  • Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
  • Parallel + Dependent Queries
  • Mutations + Reactive Query Refetching
  • Multi-layer Cache + Automatic Garbage Collection
  • Paginated + Cursor-based Queries
  • Load-More + Infinite Scroll Queries w/ Scroll Recovery
  • Request Cancellation
  • Dedicated Devtools

Quick Start

The Angular adapter for TanStack Query requires Angular 20.1 or higher.

  1. Install angular-query
$ npm i @tanstack/angular-query

or

$ pnpm add @tanstack/angular-query

or

$ yarn add @tanstack/angular-query

or

$ bun add @tanstack/angular-query
  1. Initialize TanStack Query by adding provideTanStackQuery to your application

Pass a factory; Angular runs it once per injector and in an injection context.

import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query'

bootstrapApplication(AppComponent, {
  providers: [provideTanStackQuery(() => new QueryClient())],
})
  1. Inject query
import { injectQuery } from '@tanstack/angular-query'
import { Component } from '@angular/core'

@Component({...})
export class TodosComponent {
  info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
}
  1. If you need to update options on your query dynamically, make sure to pass them as signals. The query will refetch automatically if data for an updated query key is stale or not present.

Open in StackBlitz

@Component({})
export class PostComponent {
  #postsService = inject(PostsService)
  postId = input.required({
    transform: numberAttribute,
  })

  postQuery = injectQuery(() => ({
    queryKey: ['post', this.postId()],
    queryFn: () => {
      return lastValueFrom(this.#postsService.postById$(this.postId()))
    },
  }))
}

@Injectable({
  providedIn: 'root',
})
export class PostsService {
  #http = inject(HttpClient)

  postById$ = (postId: number) =>
    this.#http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
}

export interface Post {
  id: number
  title: string
  body: string
}