Skip to main content

Overview

The MutationCache is responsible for storing and managing all Mutation instances. It handles mutation scoping, execution ordering, and provides methods to access and manipulate mutations.

Constructor

MutationCacheConfig
Configuration options for the MutationCache
Example:

Methods

build()

Builds a new mutation instance.
QueryClient
required
The QueryClient instance
MutationOptions
required
Mutation options
MutationState
Initial state for the 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()

Removes all mutations from the cache.
Example:

getAll()

Returns all mutations in the cache.
Returns: Array of all Mutation instances Example:

find()

Finds a single mutation that matches the filters.
MutationFilters
required
Filters to match mutations
Returns: The first matching Mutation or undefined Example:

findAll()

Finds all mutations that match the filters.
MutationFilters
Filters to match mutations
Returns: Array of matching Mutation instances Example:

canRun()

Determines if a mutation can run based on scoping rules.
Mutation
required
The mutation to check
Returns: true if the mutation can run, false otherwise
Mutations with the same scope ID execute sequentially. A mutation can run if there are no other pending mutations in its scope, or if it is the first pending mutation in the scope.

runNext()

Runs the next paused mutation in the same scope.
Mutation
required
The mutation that just completed
Returns: Promise that resolves when the next mutation continues

notify()

Notifies all subscribers of a cache event.
MutationCacheNotifyEvent
required
The event to notify subscribers about

subscribe()

Subscribes to cache events.
(event: MutationCacheNotifyEvent) => void
required
Callback function to handle cache events
Returns: Function to unsubscribe Example:

resumePausedMutations()

Resumes all paused mutations.
Returns: Promise that resolves when all paused mutations have continued Example:
This is typically called automatically when the network comes back online.

Mutation Scoping

Mutations can be scoped using the scope option. Mutations with the same scope ID will execute sequentially rather than concurrently. Example:

Events

The MutationCache emits the following events:
  • added - When a mutation is added to the cache
  • removed - When a mutation is removed from the cache
  • updated - When a mutation’s state changes
  • observerAdded - When an observer subscribes to a mutation
  • observerRemoved - When an observer unsubscribes from a mutation
  • observerOptionsUpdated - When observer options are updated

Usage with QueryClient