useMutation

Apollo Client API reference


Signature

TypeScript
1useMutation<TData, TVariables>(
2  mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,
3  options?: Options<TData, TVariables>
4): ResultTuple<TData, TVariables>

Parameters

Name / Type
Description
mutation
DocumentNode | TypedDocumentNode<TData, TVariables>
options (optional)
Options<TData, TVariables>

Result

TypeScript
1[
2  mutate: (options?: MutationFunctionOptions<TData, TVariables>) => Promise<FetchResult<TData>>,
3  result: MutationResult<TData>
4]
A tuple of two values:
Name / Type
Description
mutate
( options?: MutationFunctionOptions<TData, TVariables> ) => Promise<FetchResult<TData>>
A function to trigger the mutation from your UI. You can optionally pass this function any of the following options:
  • awaitRefetchQueries
  • context
  • fetchPolicy
  • onCompleted
  • onError
  • optimisticResponse
  • refetchQueries
  • onQueryUpdated
  • update
  • variables
  • client
Any option you pass here overrides any existing value for that option that you passed to useMutation.The mutate function returns a promise that fulfills with your mutation result.
result
MutationResult<TData>
The result of the mutation.
Show/hide child attributes
boolean

If true, the mutation's mutate function has been called.

ApolloClient

The instance of Apollo Client that executed the mutation.

Can be useful for manually executing followup operations or writing data to the cache.

MaybeMasked<TData> | null | undefined

The data returned from your mutation. Can be undefined if the errorPolicy is all or ignore and the server returns a GraphQL response with errors but not data or a network error is returned.

ErrorLike | undefined

If the mutation produces one or more errors, this object contains either an array of graphQLErrors or a single networkError. Otherwise, this value is undefined.

For more information, see Handling operation errors.

boolean

If true, the mutation is currently in flight.

() => void

A function that you can call to reset the mutation's result to its initial, uncalled state.