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

README.md

18 lines1.5 KB

1# TanStack Angular Query toResource Signal Forms Example
2​
3A registration form that validates the username and invite codes with queries, through Signal Forms' `validateAsync`.
4​
5## How it works
6​
7- `src/app/validation-queries.service.ts` is an `@Injectable` `ValidationQueriesService` whose `usernameAvailability(username)` and `inviteCodeValidation(code)` return `queryOptions`, and whose `submitRegistration()` returns `mutationOptions`. All three use mock requests.
8- `src/app/app.component.ts` passes a `factory` to `validateAsync` that creates a query with `injectQuery` and returns `toResource(query)`. Results are cached by query key, and the query is `enabled` only when the field has a value.
9- The invite-code validator is added with `applyEach`, so its query is created only when an invite code is added to the form array.
10- The fields use `debounce(path, 300)` from `@angular/forms/signals` because the `debounce` option of `validateAsync` does not work with `toResource(query)` ([angular/angular#70884](https://github.com/angular/angular/issues/70884)). Blurring a field or submitting the form applies a pending update immediately.
11- Submitting calls `registrationMutation.mutateAsync` from `injectMutation` inside Signal Forms' `submit`, and maps the errors the mutation returns to their fields. The "Request validation again" button calls `reloadValidation()`.
12​
13Signal Forms' `validateAsync` is stable from Angular 22, so this example needs Angular 22.
14​
15To run this example:
16​
17- `npm install`
18- `npm start`