Skip to content

Commit 18e357c

Browse files
authored
docs(angular-query): document angular httpclient and other data fetching solutions (#8732)
1 parent 5babd11 commit 18e357c

File tree

9 files changed

+68
-17
lines changed

9 files changed

+68
-17
lines changed

docs/config.json

+4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@
145145
"label": "Quick Start",
146146
"to": "framework/angular/quick-start"
147147
},
148+
{
149+
"label": "Angular HttpClient and other data fetching clients",
150+
"to": "framework/angular/angular-httpclient-and-other-data-fetching-clients"
151+
},
148152
{
149153
"label": "Devtools",
150154
"to": "framework/angular/devtools"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
id: Angular-HttpClient-and-other-data-fetching-clients
3+
title: Angular HttpClient and other data fetching clients
4+
---
5+
6+
Because TanStack Query's fetching mechanisms are agnostically built on Promises, you can use literally any asynchronous data fetching client, including the browser native `fetch` API, `graphql-request`, and more.
7+
8+
## Using Angular's `HttpClient` for data fetching
9+
10+
`HttpClient` is a powerful and integrated part of Angular, which gives the following benefits:
11+
12+
- Mock responses in unit tests using [provideHttpClientTesting](https://angular.dev/guide/http/testing).
13+
- [Interceptors](https://angular.dev/guide/http/interceptors) can be used for a wide range of functionality including adding authentication headers, performing logging, etc. While some data fetching libraries have their own interceptor system, `HttpClient` interceptors are integrated with Angular's dependency injection system.
14+
- `HttpClient` automatically informs [`PendingTasks`](https://angular.dev/api/core/PendingTasks#), which enables Angular to be aware of pending requests. Unit tests and SSR can use the resulting application _stableness_ information to wait for pending requests to finish. This makes unit testing much easier for [Zoneless](https://angular.dev/guide/experimental/zoneless) applications.
15+
- When using SSR, `HttpClient` will [cache requests](https://angular.dev/guide/ssr#caching-data-when-using-HttpClient) performed on the server. This will prevent unneeded requests on the client. `HttpClient` SSR caching works out of the box. TanStack Query has its own hydration functionality which may be more powerful but requires some setup. Which one fits your needs best depends on your use case.
16+
17+
### Using observables in `queryFn`
18+
19+
As TanStack Query is a promise based library, observables from `HttpClient` need to be converted to promises. This can be done with the `lastValueFrom` or `firstValueFrom` functions from `rxjs`.
20+
21+
```ts
22+
@Component({
23+
// ...
24+
})
25+
class ExampleComponent {
26+
private readonly http = inject(HttpClient)
27+
28+
readonly query = injectQuery(() => ({
29+
queryKey: ['repoData'],
30+
queryFn: () =>
31+
lastValueFrom(
32+
this.http.get('https://api.github.com/repos/tanstack/query'),
33+
),
34+
}))
35+
}
36+
```
37+
38+
> Since Angular is moving towards RxJS as an optional dependency, it's expected that `HttpClient` will also support promises in the future.
39+
>
40+
> Support for observables in TanStack Query for Angular is planned.
41+
42+
## Comparison table
43+
44+
| Data fetching client | Pros | Cons |
45+
| --------------------------------------------------- | --------------------------------------------------- | -------------------------------------------------------------------------- |
46+
| **Angular HttpClient** | Featureful and very well integrated with Angular. | Observables need to be converted to Promises. |
47+
| **Fetch** | Browser native API, so adds nothing to bundle size. | Barebones API which lacks many features. |
48+
| **Specialized libraries such as `graphql-request`** | Specialized features for specific use cases. | If it's not an Angular library it won't integrate well with the framework. |

docs/framework/angular/guides/background-fetching-indicators.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@ ref: docs/framework/react/guides/background-fetching-indicators.md
88

99
```angular-ts
1010
@Component({
11-
selector: 'posts',
11+
selector: 'todos',
1212
template: `
13-
@switch (query.status()) {
14-
@case ('pending') {
15-
Loading...
13+
@if (todosQuery.isPending()) {
14+
Loading...
15+
} @else if (todosQuery.isError()) {
16+
An error has occurred: {{ todosQuery.error().message }}
17+
} @else if (todosQuery.isSuccess()) {
18+
@if (todosQuery.isFetching()) {
19+
Refreshing...
1620
}
17-
@case ('error') {
18-
An error has occurred: {{ query.error()?.message }}
19-
}
20-
@default {
21-
@if (query.isFetching()) {
22-
Refreshing...
23-
}
24-
@for (todo of query.data()) {
25-
<todo [todo]="todo" />
26-
}
21+
@for (todos of todosQuery.data(); track todo.id) {
22+
<todo [todo]="todo" />
2723
}
2824
}
2925
`,
3026
})
31-
export class TodosComponent {
27+
class TodosComponent {
3228
todosQuery = injectQuery(() => ({
3329
queryKey: ['todos'],
3430
queryFn: fetchTodos,

docs/framework/angular/guides/disabling-queries.md

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export class TodosComponent {
7070
[//]: # 'Example3'
7171

7272
```angular-ts
73+
import { skipToken, injectQuery } from '@tanstack/query-angular'
74+
7375
@Component({
7476
selector: 'todos',
7577
template: `

docs/framework/angular/guides/queries.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ replace:
1010
'custom hooks': 'services',
1111
'the `useQuery` hook': '`injectQuery`',
1212
'`useQuery`': '`injectQuery`',
13+
"TypeScript will also narrow the type of data correctly if you've checked for pending and error before accessing it.": 'TypeScript will only narrow the type when checking boolean signals such as `isPending` and `isError`.',
1314
}
1415
---
1516

docs/framework/angular/guides/window-focus-refetching.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ replace: { '@tanstack/react-query': '@tanstack/angular-query-experimental' }
88
[//]: # 'Example'
99

1010
```ts
11-
bootstrapApplication(AppComponent, {
11+
export const appConfig: ApplicationConfig = {
1212
providers: [
1313
provideTanStackQuery(
1414
new QueryClient({
@@ -20,7 +20,7 @@ bootstrapApplication(AppComponent, {
2020
}),
2121
),
2222
],
23-
})
23+
}
2424
```
2525

2626
[//]: # 'Example'

examples/angular/query-options-from-a-service/src/assets/.gitkeep

Whitespace-only changes.

examples/angular/router/src/assets/.gitkeep

Whitespace-only changes.

examples/angular/simple/src/assets/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)