--- id: createMutation title: createMutation --- ```ts function createMutation(options, queryClient?): CreateMutationResult; ``` Defined in: [packages/svelte-query/src/createMutation.svelte.ts:171](https://github.com/TanStack/query/blob/main/packages/svelte-query/src/createMutation.svelte.ts#L171) Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. `createMutation` is the function for that. ## Type Parameters ### TData `TData` = `unknown` ### TError `TError` = `Error` ### TVariables `TVariables` = `void` ### TContext `TContext` = `unknown` ## Parameters ### options [`Accessor`](../type-aliases/Accessor.md)\<[`CreateMutationOptions`](../type-aliases/CreateMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TContext`\>\> The [CreateMutationOptions](../type-aliases/CreateMutationOptions.md) to use, wrapped in an [Accessor](../type-aliases/Accessor.md) so options can be reactive. ### queryClient? [`Accessor`](../type-aliases/Accessor.md)\<[`QueryClient`](../classes/QueryClient.md)\> Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will be used. ## Returns [`CreateMutationResult`](../type-aliases/CreateMutationResult.md)\<`TData`, `TError`, `TVariables`, `TContext`\> `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared mutation definition. If you make multiple requests, `onSuccess` will fire only after the latest call you've made. ## See [mutationOptions](mutationOptions.md) to share these options across multiple `createMutation` call sites, or to look the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`). ## Examples ```svelte ``` Rendering the mutation's own state, rather than just firing it off: ```svelte {#if addMutation.isPending} Adding todo... {:else} {#if addMutation.isError}
An error occurred: {addMutation.error.message}
{/if} {/if} ``` Optimistic update via `onMutate`, rolling back on `onError`: ```svelte ``` Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a promise per call instead, so you can wait for all of them when they succeed: ```svelte ``` If some of the mutations above can fail independently of the others, and you want to know which ones did — rather than losing that information the moment the first one rejects — swap `Promise.all` for `Promise.allSettled`: ```svelte ```