Skip to main content

Mutation

The Mutation class represents a single mutation instance. It manages the lifecycle of a mutation execution, including state, retries, and callbacks.

Import

Constructor

Type Parameters

type
default:"unknown"
The type of data returned by the mutation function
type
default:"DefaultError"
The type of error that can be thrown
type
default:"unknown"
The type of variables passed to the mutation function
type
default:"unknown"
The type of context returned by onMutate

Properties

state

The current state of the mutation.

options

The mutation configuration options.

mutationId

Unique identifier for this mutation instance.

Methods

execute

Execute the mutation with the given variables.

pause

Pause the mutation execution.

continue

Continue a paused mutation.

cancel

Cancel the mutation.

setState

Update the mutation state.

Examples

Accessing Mutation State

Filtering Mutations

Accessing Mutation Options

Checking Mutation Status

Subscribing to Mutations

Manually Creating Mutations

Pausing and Resuming

Canceling Mutations

Mutation State

The mutation state object contains:
'idle' | 'pending' | 'success' | 'error'
The current status of the mutation
TData | undefined
The data returned by the mutation function (only set on success)
TError | null
The error thrown by the mutation function (only set on error)
TVariables | undefined
The variables passed to the mutation function
TContext | undefined
The context returned by the onMutate callback
number
The number of times the mutation has failed
TError | null
The reason for the last failure
boolean
Whether the mutation is currently paused
number
Timestamp when the mutation was submitted

Notes

  • Mutation is a low-level class typically created and managed by MutationCache
  • Each call to mutate() creates a new Mutation instance
  • Mutations are kept in the cache even after completion for state tracking
  • The mutationId is unique and auto-incremented
  • Mutations support pause/resume for handling network state changes
  • Failed mutations can be retried based on the retry configuration
  • State changes trigger notifications to observers and cache subscribers
  • Mutations are automatically removed from cache based on gcTime setting