-
Notifications
You must be signed in to change notification settings - Fork 0
Clone feature/react query mutation options #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| id: mutationOptions | ||
| title: mutationOptions | ||
| --- | ||
|
|
||
| ```tsx | ||
| mutationOptions({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect XML Tag in OffsetSymbol SerializationXML tag uses 'start_sym' instead of 'offset_sym' for OffsetSymbol serialization. This creates incorrect XML structure causing deserialization failures when loading symbol tables and compromises system reliability through symbol table corruption during persistence operations. Standards
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect XML Tag in OffsetSymbol SerializationXML tag uses 'start_sym' instead of 'offset_sym' for OffsetSymbol serialization. This creates incorrect XML structure causing deserialization failures when loading symbol tables and compromises system reliability through symbol table corruption during persistence operations. Standards
Comment on lines
+5
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect XML Tag in OffsetSymbol SerializationXML tag uses 'start_sym' instead of 'offset_sym' for OffsetSymbol serialization. This creates incorrect XML structure causing deserialization failures when loading symbol tables and compromises system reliability through symbol table corruption during persistence operations. Standards
Comment on lines
+5
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect XML Tag in OffsetSymbol SerializationXML tag uses 'start_sym' instead of 'offset_sym' for OffsetSymbol serialization. This creates incorrect XML structure causing deserialization failures when loading symbol tables and compromises system reliability through symbol table corruption during persistence operations. Standards
|
||
| mutationFn, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect XML Tag in OffsetSymbol SerializationXML tag uses 'start_sym' instead of 'offset_sym' for OffsetSymbol serialization. This creates incorrect XML structure causing deserialization failures when loading symbol tables and compromises system reliability through symbol table corruption during persistence operations. Standards
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect XML Tag in OffsetSymbol SerializationXML tag uses 'start_sym' instead of 'offset_sym' for OffsetSymbol serialization. This creates incorrect XML structure causing deserialization failures when loading symbol tables and compromises system reliability through symbol table corruption during persistence operations. Standards
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect XML Tag in OffsetSymbol SerializationXML tag uses 'start_sym' instead of 'offset_sym' for OffsetSymbol serialization. This creates incorrect XML structure causing deserialization failures when loading symbol tables and compromises system reliability through symbol table corruption during persistence operations. Standards
|
||
| ...options, | ||
| }) | ||
| ``` | ||
|
|
||
| **Options** | ||
|
|
||
| You can generally pass everything to `mutationOptions` that you can also pass to [`useMutation`](./useMutation.md). | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -239,6 +239,24 @@ const data = queryClient.getQueryData<Group[]>(['groups']) | |||||
| [//]: # 'TypingQueryOptions' | ||||||
| [//]: # 'Materials' | ||||||
|
|
||||||
| ## Typing Mutation Options | ||||||
|
|
||||||
| Similarly to `queryOptions`, you can use `mutationOptions` to extract mutation options into a separate function: | ||||||
|
|
||||||
| ```ts | ||||||
| function useGroupPostMutation() { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function name To improve clarity and align with conventions, consider renaming it to
Suggested change
|
||||||
| const queryClient = useQueryClient() | ||||||
|
|
||||||
| return mutationOptions({ | ||||||
| mutationKey: ['groups'], | ||||||
| mutationFn: executeGroups, | ||||||
| onSuccess: () => { | ||||||
| queryClient.invalidateQueries({ queryKey: ['posts'] }) | ||||||
|
Comment on lines
+248
to
+254
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect Return TypeFunction useGroupPostMutation returns mutationOptions result but should return useMutation hook. Documentation example shows incorrect pattern that would not work as a React hook. Business logic violation: hook must return mutation object with mutate function, not just options configuration. Standards
|
||||||
| }, | ||||||
| }) | ||||||
| } | ||||||
|
Comment on lines
+247
to
+257
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the hook naming convention. The function Apply one of these fixes: Option 1 (recommended): Remove the "use" prefix to match the -function useGroupPostMutation() {
+function groupPostMutationOptions() {
const queryClient = useQueryClient()
return mutationOptions({
mutationKey: ['groups'],
mutationFn: executeGroups,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['posts'] })
},
})
}
+
+// Usage:
+useMutation(groupPostMutationOptions())Option 2: Make it a proper hook: function useGroupPostMutation() {
const queryClient = useQueryClient()
- return mutationOptions({
+ return useMutation(mutationOptions({
mutationKey: ['groups'],
mutationFn: executeGroups,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['posts'] })
},
- })
+ }))
}
🤖 Prompt for AI Agents |
||||||
| ``` | ||||||
|
|
||||||
| ## Further Reading | ||||||
|
|
||||||
| For tips and tricks around type inference, have a look at [React Query and TypeScript](./community/tkdodos-blog.md#6-react-query-and-typescript) from | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { describe, expectTypeOf, it } from 'vitest' | ||
| import { dataTagSymbol } from '@tanstack/query-core' | ||
| import { mutationOptions } from '../mutationOptions' | ||
|
|
||
| describe('mutationOptions', () => { | ||
| it('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, | ||
| }) | ||
| }) | ||
|
|
||
| it('should infer types for callbacks', () => { | ||
| return mutationOptions({ | ||
| mutationFn: () => Promise.resolve(5), | ||
| mutationKey: ['key'], | ||
| onSuccess: (data) => { | ||
| expectTypeOf(data).toEqualTypeOf<number>() | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('should tag the mutationKey with the result type of the MutationFn', () => { | ||
| const { mutationKey } = mutationOptions({ | ||
| mutationKey: ['key'], | ||
| mutationFn: () => Promise.resolve(5), | ||
| }) | ||
|
|
||
| expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<number>() | ||
| }) | ||
|
|
||
| it('should tag the mutationKey with unknown if there is no mutationFn', () => { | ||
| const { mutationKey } = mutationOptions({ | ||
| mutationKey: ['key'], | ||
| }) | ||
|
|
||
| expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<unknown>() | ||
| }) | ||
|
|
||
| it('should tag the mutationKey with the result type of the MutationFn if onSuccess is used', () => { | ||
| const { mutationKey } = mutationOptions({ | ||
| mutationKey: ['key'], | ||
| mutationFn: () => Promise.resolve(5), | ||
| onSuccess: () => {}, | ||
| }) | ||
|
|
||
| expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<number>() | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { mutationOptions } from '../mutationOptions' | ||
| import type { UseMutationOptions } from '../types' | ||
|
|
||
| describe('mutationOptions', () => { | ||
| it('should return the object received as a parameter without any modification.', () => { | ||
| const object: UseMutationOptions = { | ||
| mutationKey: ['key'], | ||
| mutationFn: () => Promise.resolve(5), | ||
| } as const | ||
|
|
||
| expect(mutationOptions(object)).toStrictEqual(object) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import type { | ||
| DataTag, | ||
| DefaultError, | ||
| InitialDataFunction, | ||
| MutationFunction, | ||
| OmitKeyof, | ||
| SkipToken, | ||
| } from '@tanstack/query-core' | ||
| import type { UseMutationOptions } from './types' | ||
|
|
||
| export type UndefinedInitialDataOptions< | ||
| TMutationFnData = unknown, | ||
| TError = DefaultError, | ||
| TData = void, | ||
| TMutationKey = unknown, | ||
| > = UseMutationOptions<TMutationFnData, TError, TData, TMutationKey> & { | ||
| initialData?: | ||
| | undefined | ||
| | InitialDataFunction<NonUndefinedGuard<TMutationFnData>> | ||
| | NonUndefinedGuard<TMutationFnData> | ||
| } | ||
|
|
||
| export type UnusedSkipTokenOptions< | ||
| TMutationFnData = unknown, | ||
| TError = DefaultError, | ||
| TData = void, | ||
| TMutationKey = unknown, | ||
| > = OmitKeyof< | ||
| UseMutationOptions<TMutationFnData, TError, TData, TMutationKey>, | ||
| 'mutationFn' | ||
| > & { | ||
| mutationFn?: Exclude< | ||
| UseMutationOptions< | ||
| TMutationFnData, | ||
| TError, | ||
| TData, | ||
| TMutationKey | ||
| >['mutationFn'], | ||
| SkipToken | undefined | ||
| > | ||
| } | ||
|
|
||
| type NonUndefinedGuard<T> = T extends undefined ? never : T | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Constraint LogicNonUndefinedGuard type logic excludes undefined but allows null values to pass through. Type constraint incomplete for proper data validation. Mathematical logic flaw: constraint should handle both undefined and null for complete type safety. Standards
|
||
|
|
||
| export type DefinedInitialDataOptions< | ||
| TMutationFnData = unknown, | ||
| TError = DefaultError, | ||
| TData = void, | ||
| TMutationKey = unknown, | ||
| > = Omit< | ||
| UseMutationOptions<TMutationFnData, TError, TData, TMutationKey>, | ||
| 'mutationFn' | ||
| > & { | ||
| initialData: | ||
| | NonUndefinedGuard<TMutationFnData> | ||
| | (() => NonUndefinedGuard<TMutationFnData>) | ||
| mutationFn?: MutationFunction<TMutationFnData, TMutationKey> | ||
| } | ||
|
|
||
| export function mutationOptions< | ||
| TMutationFnData = unknown, | ||
| TError = DefaultError, | ||
| TData = void, | ||
| TMutationKey = unknown, | ||
| >( | ||
| options: DefinedInitialDataOptions< | ||
| TMutationFnData, | ||
| TError, | ||
| TData, | ||
| TMutationKey | ||
| >, | ||
| ): DefinedInitialDataOptions<TMutationFnData, TError, TData, TMutationKey> & { | ||
| mutationKey: DataTag<TMutationKey, TMutationFnData, TError> | ||
| } | ||
|
Comment on lines
+60
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complex Type OverloadsMultiple function overloads with complex generic type parameters create high cognitive load and maintenance burden. Three separate overloads handling different option types increases complexity for developers understanding the API. Consider consolidating overloads or extracting type resolution logic to reduce interface complexity. Standards
|
||
|
|
||
| export function mutationOptions< | ||
| TMutationFnData = unknown, | ||
| TError = DefaultError, | ||
| TData = void, | ||
| TMutationKey = unknown, | ||
| >( | ||
| options: UnusedSkipTokenOptions<TMutationFnData, TError, TData, TMutationKey>, | ||
| ): UnusedSkipTokenOptions<TMutationFnData, TError, TData, TMutationKey> & { | ||
| mutationKey: DataTag<TMutationKey, TMutationFnData, TError> | ||
| } | ||
|
|
||
| export function mutationOptions< | ||
| TMutationFnData = unknown, | ||
| TError = DefaultError, | ||
| TData = void, | ||
| TMutationKey = unknown, | ||
| >( | ||
| options: UndefinedInitialDataOptions< | ||
| TMutationFnData, | ||
| TError, | ||
| TData, | ||
| TMutationKey | ||
| >, | ||
| ): UndefinedInitialDataOptions<TMutationFnData, TError, TData, TMutationKey> & { | ||
| mutationKey: DataTag<TMutationKey, TMutationFnData, TError> | ||
| } | ||
|
|
||
| export function mutationOptions(options: unknown) { | ||
| return options | ||
| } | ||
|
Comment on lines
+1
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file seems to be a direct copy of
The file should be rewritten to correctly type |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect XML Tag in OffsetSymbol Serialization
XML tag uses 'start_sym' instead of 'offset_sym' for OffsetSymbol serialization. This creates incorrect XML structure causing deserialization failures when loading symbol tables and compromises system reliability through symbol table corruption during persistence operations.
Standards