Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queries.ts

39 lines1004 Bytes

1import { Injectable, inject } from '@angular/core'
2import { mutationOptions, queryOptions } from '@tanstack/angular-query'
3import { lastValueFrom } from 'rxjs'
4import { TasksAPI } from './api'
5​
6@Injectable({
7 providedIn: 'root',
8})
9export class TasksQueries {
10 readonly #api = inject(TasksAPI)
11​
12 all = () => ['tasks'] as const
13​
14 list = () =>
15 queryOptions({
16 queryKey: [...this.all(), 'list'] as const,
17 queryFn: () => lastValueFrom(this.#api.getTasks()),
18 })
20​
21@Injectable({
22 providedIn: 'root',
23})
24export class TasksMutations {
25 readonly #api = inject(TasksAPI)
26 readonly #queries = inject(TasksQueries)
27​
28 create = () =>
29 mutationOptions({
30 mutationKey: [...this.#queries.all(), 'create'] as const,
31 mutationFn: (task: string) => lastValueFrom(this.#api.createTask(task)),
32 })
33​
34 deleteAll = () =>
35 mutationOptions({
36 mutationKey: [...this.#queries.all(), 'delete-all'] as const,
37 mutationFn: () => lastValueFrom(this.#api.deleteTasks()),
38 })