Skip to main content
This guide will help you build your first Angular Query application. You’ll learn how to fetch data, handle loading states, and perform mutations.

What You’ll Build

We’ll create a simple blog post viewer with:
  • Fetching and displaying a list of posts
  • Loading and error states
  • Creating new posts with mutations
  • Automatic cache updates

Prerequisites

Make sure you’ve completed the installation steps:
  • Installed @tanstack/angular-query-experimental
  • Configured provideTanStackQuery in your app

Step 1: Create a Data Service

First, let’s create a service to fetch data. We’ll use Angular’s HttpClient:

Step 2: Fetch and Display Posts

Now let’s create a component that fetches and displays posts:
components/posts-list.component.ts

Understanding the Code

Key concepts:
  • queryKey: A unique identifier for this query. Used for caching and refetching.
  • queryFn: An async function that fetches the data.
  • injectQuery: Returns a signal-based object with query state and data.

Step 3: Add a Single Post View

Let’s create a component to view a single post with reactive parameters:
components/post-detail.component.ts
The query function runs in a reactive context. When postId() changes, Angular Query automatically detects the change and refetches with the new ID.

Step 4: Create Posts with Mutations

Now let’s add the ability to create new posts:
components/create-post.component.ts

Understanding Mutations

  • mutationFn: The function that performs the mutation
  • onSuccess: Callback called when mutation succeeds
  • invalidateQueries: Marks queries as stale to trigger refetch

Step 5: Put It All Together

Create a main app component that uses all the pieces:
app.component.ts

Next Steps

Queries Guide

Learn advanced query patterns

Mutations Guide

Master mutations and optimistic updates

DevTools

Debug your queries visually

API Reference

Explore the complete API

Common Patterns

Dependent Queries

Fetch data that depends on other data:

Optimistic Updates

Update UI immediately before server responds:

Pagination