-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into react-query/test/queryOptions
- Loading branch information
Showing
7 changed files
with
57 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import { provideHttpClient, withFetch } from '@angular/common/http' | ||
|
||
import { provideRouter } from '@angular/router' | ||
import { | ||
QueryClient, | ||
provideTanStackQuery, | ||
} from '@tanstack/angular-query-experimental' | ||
import { routes } from './app.routes' | ||
import type { ApplicationConfig } from '@angular/core' | ||
|
||
export const appConfig: ApplicationConfig = { | ||
// In this example, `provideTanStackQuery` is used in the router | ||
providers: [provideHttpClient(withFetch()), provideRouter(routes)], | ||
providers: [ | ||
provideHttpClient(withFetch()), | ||
provideRouter(routes), | ||
provideTanStackQuery(new QueryClient()), | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
examples/angular/query-options-from-a-service/src/app/services/posts-service.ts
This file was deleted.
Oops, something went wrong.
23 changes: 19 additions & 4 deletions
23
examples/angular/query-options-from-a-service/src/app/services/queries-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
import { Injectable, inject } from '@angular/core' | ||
import { lastValueFrom } from 'rxjs' | ||
import { queryOptions } from '@tanstack/angular-query-experimental' | ||
import { PostsService } from './posts-service' | ||
import { HttpClient } from '@angular/common/http' | ||
|
||
export interface Post { | ||
id: number | ||
title: string | ||
body: string | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class QueriesService { | ||
private postsService = inject(PostsService) | ||
private http = inject(HttpClient) | ||
|
||
post(postId: number) { | ||
return queryOptions({ | ||
queryKey: ['post', postId], | ||
queryFn: () => { | ||
return lastValueFrom(this.postsService.postById$(postId)) | ||
return lastValueFrom( | ||
this.http.get<Post>( | ||
`https://jsonplaceholder.typicode.com/posts/${postId}`, | ||
), | ||
) | ||
}, | ||
}) | ||
} | ||
|
||
posts() { | ||
return queryOptions({ | ||
queryKey: ['posts'], | ||
queryFn: () => lastValueFrom(this.postsService.allPosts$()), | ||
queryFn: () => | ||
lastValueFrom( | ||
this.http.get<Array<Post>>( | ||
'https://jsonplaceholder.typicode.com/posts', | ||
), | ||
), | ||
}) | ||
} | ||
} |