useMutation
useMutation​
Hook used to execute a mutation in a React component.
import type {FeedbackLikeMutation} from 'FeedbackLikeMutation.graphql';
const React = require('React');
const {graphql, useMutation} = require('react-relay');
function LikeButton() {
const [commit, isInFlight] = useMutation<FeedbackLikeMutation>(graphql`
mutation FeedbackLikeMutation($input: FeedbackLikeData!) {
feedback_like(data: $input) {
feedback {
id
viewer_does_like
like_count
}
}
}
`);
if (isInFlight) {
return <Spinner />;
}
return (
<button
onClick={() => {
commit({
variables: {
input: {
id: '123',
text: 'text',
},
},
onCompleted(data) {
console.log(data);
},
});
}}
/>
);
}
Arguments​
mutation: GraphQL mutation specified using agraphqltemplate literal.
commitMutationFn:<T: MutationParameters>(IEnvironment, MutationConfig<T>): Disposable. [Optional] A function with the same signature ascommitMutation, which will be called in its stead. Defaults tocommitMutation.
Return Value​
Tuple containing the following values:
- [0]
commitMutation: The function that will execute the mutation- Arguments, the syntax signature is almost the same as our
commitMutationAPIvariables: Object containing the variables needed for the mutation. For example, if the mutation defines an$inputvariable, this object should contain aninputkey, whose shape must match the shape of the data expected by the mutation as defined by the GraphQL schema.onCompleted: Callback function executed when the request is completed and the in-memory Relay store is updated with theupdaterfunction. Takes aresponseobject, which is the "raw" server response. Internallyerrorsare not allowed,CRITICALerror will be thrown in theonErrorhandler.onError: Callback function executed if Relay encounters an error during the request. Internally,CRITICALerror during reading the mutation result on the serveroptimisticResponse: Object containing the data to optimistically update the local in-memory store, i.e. immediately, before the mutation request has completed. This object must have the same shape as the mutation's response type, as defined by the GraphQL schema. If provided, Relay will use theoptimisticResponsedata to update the fields on the relevant records in the local data store, beforeoptimisticUpdateris executed. If an error occurs during the mutation request, the optimistic update will be rolled back.optimisticUpdater: Function used to optimistically update the local in-memory store, i.e. immediately, before the mutation request has completed. If an error occurs during the mutation request, the optimistic update will be rolled back. This function takes astore, which is a proxy of the in-memory Relay Store. In this function, the client defines how to update the local data via thestoreinstance. For details on how to use thestore, please refer to our Relay Store API Reference. Please note:- It is usually preferable to just pass an
optimisticResponseoption instead of anoptimisticUpdater, unless you need to perform updates on the local records that are more complicated than just updating fields (e.g. deleting records or adding items to collections). - If you do decide to use an
optimisticUpdater, often times it can be the same function asupdater.
- It is usually preferable to just pass an
updater: Function used to update the local in-memory store based on the real server response from the mutation. Ifupdateris not provided, by default, Relay will know to automatically update the fields on the records referenced in the mutation response; however, you should pass anupdaterif you need to make more complicated updates than just updating fields (e.g. deleting records or adding items to collections). When the server response comes back, Relay first reverts any changes introduced byoptimisticUpdateroroptimisticResponseand will then executeupdater. This function takes astore, which is a proxy of the in-memory Relay Store. In this function, the client defines how to update the local data based on the server response via thestoreinstance. For details on how to use thestore, please refer to our Relay Store APIuploadables: An optional uploadable map, an object representing any number of uploadable items, with one key per item. Each value must be of typeFileorBlob.- No environment argument:
useMutationwill automatically use the current environment provided byRelayEnvironmentProvider
- Return value:
disposable: Object containing adisposefunction. Callingdisposable.dispose()will revert the optimistic update, and Relay won't update the store or call any success/error callback, but the network request is not guaranteed to be cancelled. If thedisposeis called after the mutation has succeeded, it will not rollback the update in Relay store.
- Arguments, the syntax signature is almost the same as our
- [1]
areMutationsInFlight: Will betrueif any mutation triggered by callingcommitMutationis still in flight. If you callcommitMutationmultiple times, there can be multiple mutations in flight at once.
Is this page useful?
Help us make the site even better by answering a few quick questions.