Skip to content

Commit 07eaf6c

Browse files
authored
docs: working mutationOptions example (#2305)
* docs: updated mutationOptions example * style(docs): prettier
1 parent 6cde558 commit 07eaf6c

File tree

1 file changed

+28
-10
lines changed
  • docs/src/pages/reference/configuration

1 file changed

+28
-10
lines changed

docs/src/pages/reference/configuration/output.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,18 +1219,36 @@ module.exports = {
12191219
```ts
12201220
// custom-mutator-options.ts
12211221

1222-
export const useCustomMutatorOptions = <T, TError, TData, TContext>(
1223-
options: UseMutationOptions<T, TError, TData, TContext> &
1224-
Required<
1225-
Pick<UseMutationOptions<T, TError, TData, TContext>, 'mutationFn'>
1226-
>,
1227-
/* Optional */ path: { url: string },
1228-
/* Optional */ operation: { operationId: string; operationName: string },
1229-
) => {
1222+
// custom-mutator-options.ts
1223+
type OptionsWithMutationFn<
1224+
TData = unknown,
1225+
TError = Error,
1226+
TVariables = void,
1227+
TContext = unknown,
1228+
> = UseMutationOptions<T, TError, TData, TContext> &
1229+
Required<Pick<UseMutationOptions<T, TError, TData, TContext>, 'mutationFn'>>;
1230+
1231+
export const useCustomMutatorOptions = <
1232+
TData = unknown,
1233+
TError = Error,
1234+
TVariables = void,
1235+
TContext = unknown,
1236+
>(
1237+
options: OptionsWithMutationFn<T, TError, TData, TContext>,
1238+
): OptionsWithMutationFn<T, TError, TData, TContext> => {
12301239
const queryClient = useQueryClient();
1231-
if (operation.operationId === 'createPet') {
1232-
queryClient.invalidateQueries({ queryKey: getGetPetsQueryKey() });
1240+
if (options.mutationKey?.[0] === 'petDestroy') {
1241+
// Note: `options.mutationKey?.[0]` is untyped.
1242+
options.onSuccess = (_data, variables, _context) => {
1243+
// Note: `variables` is untyped.
1244+
options.onSuccess?.(data, variables, context);
1245+
// Note: `queryKey` is hardcoded, can't use `getGetPetQueryKey()` as it would introduce circular dependencies.
1246+
queryClient.invalidateQueries({
1247+
queryKey: ['api', 'v2', 'pet', variables.id],
1248+
});
1249+
};
12331250
}
1251+
// TODO: add more ifs for each mutation.
12341252
return options;
12351253
};
12361254
```

0 commit comments

Comments
 (0)