-
-
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.
feat(angular-query): add mutationOptions
- Loading branch information
Showing
8 changed files
with
201 additions
and
20 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
id: query-options | ||
title: Mutation Options | ||
--- | ||
|
||
One of the best ways to share mutation options between multiple places, | ||
is to use the `mutationOptions` helper. At runtime, this helper just returns whatever you pass into it, | ||
but it has a lot of advantages when using it [with TypeScript](../../typescript#typing-query-options). | ||
You can define all possible options for a mutation in one place, | ||
and you'll also get type inference and type safety for all of them. | ||
|
||
```ts | ||
export class QueriesService { | ||
private http = inject(HttpClient) | ||
|
||
updatePost(id: number) { | ||
return mutationOptions({ | ||
mutationFn: (post: Post) => Promise.resolve(post), | ||
mutationKey: ['updatePost', id], | ||
onSuccess: (newPost) => { | ||
// ^? newPost: Post | ||
this.queryClient.setQueryData(['posts', id], newPost) | ||
}, | ||
}) | ||
} | ||
} | ||
``` |
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
22 changes: 22 additions & 0 deletions
22
packages/angular-query-experimental/src/__tests__/mutation-options.test-d.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { mutationOptions } from '../mutation-options' | ||
|
||
describe('mutationOptions', () => { | ||
test('should not allow excess properties', () => { | ||
return mutationOptions({ | ||
mutationFn: () => Promise.resolve(5), | ||
mutationKey: ['key'], | ||
// @ts-expect-error this is a good error, because onMutates does not exist! | ||
onMutates: 1000, | ||
}) | ||
}) | ||
|
||
test('should infer types for callbacks', () => { | ||
return mutationOptions({ | ||
mutationFn: () => Promise.resolve(5), | ||
mutationKey: ['key'], | ||
onSuccess: (data) => { | ||
expectTypeOf(data).toEqualTypeOf<number>() | ||
}, | ||
}) | ||
}) | ||
}) |
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
60 changes: 60 additions & 0 deletions
60
packages/angular-query-experimental/src/mutation-options.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { | ||
DefaultError, | ||
MutationObserverOptions, | ||
OmitKeyof, | ||
} from '@tanstack/query-core' | ||
|
||
/** | ||
* Allows to share and re-use mutation options in a type-safe way. | ||
* | ||
* **Example** | ||
* | ||
* ```ts | ||
* export class QueriesService { | ||
* private http = inject(HttpClient); | ||
* | ||
* updatePost(id: number) { | ||
* return mutationOptions({ | ||
* mutationFn: (post: Post) => Promise.resolve(post), | ||
* mutationKey: ["updatePost", id], | ||
* onSuccess: (newPost) => { | ||
* // ^? newPost: Post | ||
* this.queryClient.setQueryData(["posts", id], newPost); | ||
* }, | ||
* }); | ||
* } | ||
* } | ||
* | ||
* queries = inject(QueriesService) | ||
* idSignal = new Signal(0); | ||
* mutation = injectMutation(() => this.queries.updatePost(this.idSignal())) | ||
* | ||
* mutation.mutate({ title: 'New Title' }) | ||
* ``` | ||
* @param options - The mutation options. | ||
* @returns Mutation options. | ||
* @public | ||
*/ | ||
export function mutationOptions< | ||
TData = unknown, | ||
TError = DefaultError, | ||
TVariables = void, | ||
TContext = unknown, | ||
>( | ||
options: MutationObserverOptions<TData, TError, TVariables, TContext>, | ||
): CreateMutationOptions<TData, TError, TVariables, TContext> { | ||
return options | ||
} | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface CreateMutationOptions< | ||
TData = unknown, | ||
TError = DefaultError, | ||
TVariables = void, | ||
TContext = unknown, | ||
> extends OmitKeyof< | ||
MutationObserverOptions<TData, TError, TVariables, TContext>, | ||
'_defaulted' | ||
> {} |
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