# Mutation (https://effetch-query.monarcode.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 with its
docs. Your editor shows the same docs, with an example, when you hover the
name.

## Define a mutation

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

### Mutation.make

Defines a mutation. Nothing runs until it's used, with [`run`](https://effetch-query.monarcode.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://effetch-query.monarcode.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://effetch-query.monarcode.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://effetch-query.monarcode.dev/docs/reference/mutation#make), and run it with [`run`](https://effetch-query.monarcode.dev/docs/reference/mutation#run) or `useMutation`.

A definition holds no state. Each use (each component's `useMutation`) keeps its own, so components never share a waiting or failed state.

| Prop                                 | Type     | Description                              |
| ------------------------------------ | -------- | ---------------------------------------- |
| `~@monarcode/effetch-query/Mutation` | `object` | Marks a mutation, and carries its types. |

## Mutation options

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

### Mutation.MakeOptions

Options for [`make`](https://effetch-query.monarcode.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`.

The run completes after all of them. If one of the callbacks here dies, the run dies with its defect and the steps after it don't happen, except that a run whose `optimistic` dies still has its writes rolled back.

An interrupted run (with `concurrent: false`, replaced by a newer one; or its client disposed, which also interrupts runs `concurrent: "queue"` holds back) calls nothing after the interruption. If `run` hadn't succeeded yet, its optimistic writes are rolled back, unless the client is being disposed: then there's no cache left to put back.

| 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 cache writes (Query.set, Query.update, and `Entity.update`, `set` and `remove`) 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://effetch-query.monarcode.dev/docs/mutations/run).

### Mutation.run

Runs a mutation from Effect code, with the steps [`MakeOptions`](https://effetch-query.monarcode.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. It dies with a `Query.KeyHoldsTargetError` before it starts if a key in `invalidates` holds a query, a family or a group.