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
With Retry
Experimental
The retry and retryDelay parameters are accepted by QueryClient.mutate() but are not currently passed through to MutationEntry. 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