Server mutations
Sending a server-side create, update, or delete request from an RTK Query API slice.
Redux Toolkit · Type Alias
MutationDefinition is the TypeScript shape of an RTK Query mutation endpoint. It types the request argument, result, base query, tags, reducer path, context, and endpoint behavior.
MutationDefinition is the type-level shape of an RTK Query mutation endpoint. It captures the argument, base-query, tag, result, reducer-path, and context types, plus endpoint callbacks and lifecycle options used to send updates, manage cache invalidation, and observe mutation state.
export type MutationDefinition<QueryArg, BaseQuery extends BaseQueryFn, TagTypes extends string, ResultType, ReducerPath extends string = string, Context = Record<string, any>> = { ... }| Name | Type | Description |
|---|---|---|
QueryArg | type | Type of the mutation argument passed to query or queryFn. |
BaseQuery | BaseQueryFn | Base query implementation used by the API. |
TagTypes | string | String union of tag names available to the API. |
ResultType | type | Type returned by the mutation. |
ReducerPath | string = string | API reducer path; defaults to string. |
Context | Record<string, any> = Record<string, any> | Context type available to endpoint callbacks; defaults to Record<string, any>. |
A type describing the configuration and lifecycle behavior of an RTK Query mutation endpoint.
build.mutation() inside createApi.query, or custom asynchronous logic via queryFn.query or queryFn; defining both is invalid.import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
type Post = { id: number; title: string }
const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: '/api' }),
endpoints: (build) => ({
updatePost: build.mutation<Post, { id: number; title: string }>({
query: ({ id, title }) => ({
url: `posts/${id}`,
method: 'PUT',
body: { title },
}),
}),
}),
})
export const { useUpdatePostMutation } = api
// In a React component:
// const [updatePost] = useUpdatePostMutation()
// await updatePost({ id: 1, title: 'Updated' }).unwrap()Defines a mutation endpoint that sends a PUT request and exposes a React mutation hook.
Sending a server-side create, update, or delete request from an RTK Query API slice.
Invalidating cached query data after a successful write so related data can be fetched again.
Typing generated mutation hooks so their trigger arguments and returned data are checked by TypeScript.
MutationDefinition is the generic type describing an RTK Query mutation endpoint. Its generics include the query argument, base-query type, tag names, result type, reducer path, and endpoint context.
Follow-up: Why would a mutation definition include tag information? Tags let the mutation invalidate or affect cached query data, which can cause related queries to refetch.
A mutation definition must describe how the request is performed through either query or queryFn. query commonly returns the URL, HTTP method, and body, while queryFn supports arbitrary asynchronous logic.
Follow-up: When is queryFn preferable? Use it when request behavior cannot be expressed by the configured base query, such as combining multiple operations or using custom async handling.
Mutations do not run automatically like queries. The generated mutation hook returns a trigger function plus mutation-result state, and calling the trigger starts the request.
Follow-up: How can a caller obtain the raw result or error? The trigger promise provides unwrap, which exposes the fulfilled value or rejects with the mutation error.
Mutation endpoint definitionbuild.mutation()createApiuseMutationqueryFninvalidatesTags