|
| 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. | |
0 commit comments