Mutation
Learn about SoulCache's mutation system for data modifications
Mutation System
The Mutation System handles data modifications with automatic cache invalidation and optimistic updates.
Core Concepts
Mutations are operations that modify data on the server:
- Create
- Update
- Delete
- Any side-effect operation
Usage
Basic Mutation
With Callbacks
Callback errors
Mutation callbacks are isolated from the mutation outcome:
- An
onSuccess/onSettledthat throws cannot turn a success into an error — the mutation resolves with its data and the state stayssuccess. - An
onErrorthat throws cannot replace the original mutation error and cannot preventonSettledfrom running. - An
onMutatethat throws is treated as a mutation error (onErrorandonSettledstill run). onSettledruns on every success/error, even when other callbacks throw. (Cancelled mutations are the exception and do not fireonSettled.)- Callbacks are expected to be synchronous (they are typed to return
void). Anasynccallback that rejects rather than throwing synchronously is not isolated and its rejection is not handled.
With Retry
Retry
The retry and retryDelay parameters are accepted by QueryClient.mutate()
but are not applied — mutate() performs a single attempt. Use
MutationEntry.mutateWithRetry() directly for retry behavior.
Optimistic Updates
Optimistically update cache before server confirms:
Mutation States
Mutations go through these states:
| State | Description |
|---|---|
idle | Not active |
pending | Currently executing |
success | Completed successfully |
error | Failed |
MutationCache
Access the mutation cache directly:
Best Practices
Mutation Guidelines
- Always invalidate related queries — Keep cache consistent
- Use optimistic updates carefully — Rollback on errors
- Handle loading states — Show feedback to users
- Clean up side effects — Use onSettled for cleanup