# Mutation (https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation)

`Mutation` defines mutations, and runs them from Effect. Import it from the core entry point:

```ts
import { Mutation } from "@monarcode/effetch-query";
```

Its exports follow, grouped by the guide that covers them. Each one has
the first paragraph of its docs, and what it needs and how it fails; your
editor shows the rest, with an example, when you hover the name.

## Define a mutation

Guide: [Define a mutation](https://0758e0c7.effetch-query.pages.dev/docs/mutations).

### Mutation.make

Defines a mutation. Nothing runs until it's used, with [`run`](https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation#run) or `useMutation`. Its types come from `run`: annotate `run`'s parameter to type the variables. Explicit type arguments must give all three, since the value and error types left out are `never`.

### Mutation.isMutation

Checks whether a value is a [`Mutation`](https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation#mutation). A mutation's variables can't be known from the value, so it narrows to `Mutation<never, unknown, unknown>`, which every mutation is.

### Mutation.TypeId

The property that marks a [`Mutation`](https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation#mutation).

### Mutation.Mutation

A mutation's definition: what a run does with its `Variables`, succeeding with `A` or failing with `E`. Create one with [`make`](https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation#make), and run it with [`run`](https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation#run) or `useMutation`.

## Mutation options

Guide: [Mutation options](https://0758e0c7.effetch-query.pages.dev/docs/mutations/options).

### Mutation.MakeOptions

Options for [`make`](https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation#make). A run calls them in this order:

1. `optimistic`, whose cache writes are recorded;
2. the client's `mutations.onMutate`;
3. `run`, retried on `retry`;
4. on success: `onSuccess`, `invalidates`, then the client's `mutations.onSuccess`;
5. on failure: the optimistic writes are rolled back, then `onFailure`, then the client's `mutations.onFailure`;
6. `onSettled`, then the client's `mutations.onSettled`.

| Prop           | Type       | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| -------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `run`          | `function` | Performs the mutation, with your services and the client.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `optimistic?`  | `function` | Updates the cache before `run`, so the change shows at once. Its Query.set and Query.update calls are recorded, and if the run fails, the queries they wrote are put back, newest write first. A query written again in the meantime, even by a fetch, is put back all the same. Only writes made while it runs count: not those of a fiber it forks that write after it's done, nor those of a mutation it runs. A fetch of the same query already in progress still settles over the optimistic value, as it does over any Query.set, and there's no way to cancel it: the query then shows what that fetch returned.                                                                                                                                                                                                                                          |
| `onSuccess?`   | `function` | Runs after a successful `run`, before `invalidates`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `onFailure?`   | `function` | Runs after a failed `run`, once the optimistic writes are rolled back.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `onSettled?`   | `function` | Runs after `run` succeeds or fails, after `onSuccess` or `onFailure`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `invalidates?` | `union`    | Queries to invalidate after `onSuccess`, as Query.invalidate does: a list of targets, or a function of the run's value and variables that returns one. Default: none. A failed run invalidates nothing. A key that holds a query, a family or a group matches nothing: in a list, the run dies with a Query.KeyHoldsTargetError before it starts; from the function, the change is already saved, so the target is logged and skipped. Combine targets with Query.group.                                                                                                                                                                                                                                                                                                                                                                                         |
| `retry?`       | `object`   | Retries a failed run on this schedule, which gets each failure. Defects aren't retried. Default: no retries.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `concurrent?`  | `union`    | The policy for how one use's runs (one component's `useMutation`, say) overlap. Separate uses, and `Mutation.run` calls, never affect each other. Default: `true`. - `true`: runs overlap. - `false`: a new run interrupts the one in progress, whose optimistic writes are rolled back first. - `"queue"`: a new run waits until the ones before it have settled, so runs go one at a time, in order. A queued run counts as in progress (`useIsMutating`, `useMutationState`), but its `optimistic` runs when it starts, so a run that fails rolls back only its own writes. - `"drop"`: a new run is ignored while the one the use shows is in progress, such as a second click on a submit button. It never starts or calls anything, and its `run` resolves at once with an interrupted `Exit`. After `reset`, which stops showing a run, a new run starts. |

## Run a mutation

Guide: [Run a mutation](https://0758e0c7.effetch-query.pages.dev/docs/mutations/run).

### Mutation.run

Runs a mutation from Effect code, with the steps [`MakeOptions`](https://0758e0c7.effetch-query.pages.dev/docs/reference/mutation#makeoptions) lists, and succeeds or fails as its `run` did. A mutation whose `run` takes no variables runs without them. Each call is a run of its own: `concurrent` applies to one use's runs, such as a component's `useMutation`, so calls never interrupt each other.

## Related pages

- [Entry points](https://0758e0c7.effetch-query.pages.dev/docs/reference): what each entry point exports.
- [`@monarcode/effetch-query`](https://0758e0c7.effetch-query.pages.dev/docs/reference/core): the core entry point's namespaces and top-level names.
- [`Query`](https://0758e0c7.effetch-query.pages.dev/docs/reference/query): defining queries, keys and targets, and the cache from Effect.
- [`InfiniteQuery`](https://0758e0c7.effetch-query.pages.dev/docs/reference/infinite-query): queries that load a list one page at a time.
- [`Entity`](https://0758e0c7.effetch-query.pages.dev/docs/reference/entity): records whose every cached copy a write updates.
- [`QueryClient`](https://0758e0c7.effetch-query.pages.dev/docs/reference/query-client): the client, its options and hooks, and server rendering.
- [`Signals`](https://0758e0c7.effetch-query.pages.dev/docs/reference/signals): where a client hears about focus and reconnects.
- [`@monarcode/effetch-query/react`](https://0758e0c7.effetch-query.pages.dev/docs/reference/react): the provider, the hooks and the boundaries.
- [`@monarcode/effetch-query/effetch`](https://0758e0c7.effetch-query.pages.dev/docs/reference/effetch): queries and mutations from Effetch endpoints.
- [`@monarcode/effetch-query/react-native`](https://0758e0c7.effetch-query.pages.dev/docs/reference/react-native): focus and online signals for React Native.
- [`@monarcode/effetch-query/react/tanstack-router`](https://0758e0c7.effetch-query.pages.dev/docs/reference/tanstack-router): connecting a client to TanStack Router and Start.