Skip to main content

injectMutation

Injects a mutation: an imperative function that can be invoked which typically performs server side effects. Unlike queries, mutations are not run automatically.

Signature

Parameters

() => CreateMutationOptions
required
A function that returns mutation options. The function is run in a reactive context using Angular’s computed, allowing signals to be reactive.CreateMutationOptions properties:
  • mutationFn - The function that performs the mutation
  • onSuccess - Callback fired when the mutation succeeds
  • onError - Callback fired when the mutation fails
  • onSettled - Callback fired when the mutation completes (success or error)
  • onMutate - Callback fired before the mutation function is executed
  • throwOnError - Whether to throw errors to the nearest error boundary
  • And other standard mutation options
InjectMutationOptions
Additional configuration for the mutation injection.

Returns

object
A signal-based mutation result object with the following properties and methods:Properties:
  • data - The data returned by the mutation function
  • error - The error object if the mutation failed
  • status - The status of the mutation (idle, pending, error, success)
  • isPending - Boolean indicating if the mutation is currently executing
  • isError - Boolean indicating if the mutation failed
  • isSuccess - Boolean indicating if the mutation succeeded
  • isIdle - Boolean indicating if the mutation is idle
  • variables - The variables object passed to the mutation function
Methods:
  • mutate(variables, options) - Function to trigger the mutation (fire-and-forget)
  • mutateAsync(variables, options) - Function to trigger the mutation and return a promise
  • reset() - Function to reset the mutation state

Usage

Basic example

Using mutateAsync with async/await

Optimistic updates

Reactive mutation with signals

Using with custom injector

Implementation Details

  • The mutation uses Angular’s computed signal for reactive options
  • Mutations are executed outside the Angular zone for performance, then results are brought back into the zone
  • Pending tasks are tracked during mutation execution for server-side rendering compatibility
  • Errors can be thrown to NgZone.onError if throwOnError is configured
  • The mutation observer is automatically cleaned up when the component/service is destroyed

Notes

  • Must be called within an injection context (constructor, field initializer, or factory function) unless the injector option is provided
  • Unlike queries, mutations are not executed automatically - you must call mutate or mutateAsync
  • The mutate function is fire-and-forget and will catch errors internally
  • The mutateAsync function returns a promise and will throw errors if not caught
  • The mutation is automatically managed and cleaned up when the component/service is destroyed

See Also