Skip to main content
The MutationCache is responsible for storing and managing all Mutation instances. It’s the underlying storage mechanism used by QueryClient for mutations.

Constructor

Creates a new MutationCache instance.
MutationCacheConfig
Configuration options for the MutationCache

Example

Methods

build

Builds a new mutation instance.
QueryClient
required
The QueryClient instance.
MutationOptions
required
Options for the mutation.
MutationState
Optional initial state for the mutation.
Mutation
Returns a new Mutation instance.

add

Adds a mutation to the cache.
Mutation
required
The mutation instance to add.

remove

Removes a mutation from the cache.
Mutation
required
The mutation instance to remove.

clear

Clears all mutations from the cache.

Example

getAll

Returns all mutations in the cache.
Array
Returns an array of all Mutation instances in the cache.

find

Finds a single mutation matching the provided filters.
MutationFilters
required
Filters to match mutations.
Mutation
Returns the first matching Mutation instance, or undefined if not found.

findAll

Finds all mutations matching the provided filters.
MutationFilters
Filters to match mutations. If not provided, returns all mutations.
Array
Returns an array of all matching Mutation instances.

Example

notify

Notifies all cache listeners of an event.
MutationCacheNotifyEvent
required
The event to notify listeners about.

subscribe

Subscribes to cache events.
(event: MutationCacheNotifyEvent) => void
required
Function called when cache events occur.
function
Returns an unsubscribe function.

Example

resumePausedMutations

Resumes all paused mutations.
Promise
Returns a promise that resolves when all paused mutations have been resumed.

Example

Events

The MutationCache emits the following events:

added

Fired when a mutation is added to the cache.

removed

Fired when a mutation is removed from the cache.

updated

Fired when a mutation is updated.

observerAdded

Fired when an observer is added to a mutation.

observerRemoved

Fired when an observer is removed from a mutation.

observerOptionsUpdated

Fired when observer options are updated.

Mutation Scopes

The MutationCache supports mutation scoping, which allows you to control the order of mutation execution. Mutations with the same scope ID will execute sequentially.

canRun

Checks if a mutation can run based on its scope.
Mutation
required
The mutation to check.
boolean
Returns true if the mutation can run, false if it must wait for another mutation in the same scope.

runNext

Runs the next pending mutation in the same scope.
Mutation
required
The mutation that just completed.
Promise
Returns a promise that resolves when the next mutation continues.

Usage with QueryClient

While you can use MutationCache directly, it’s typically used through a QueryClient: