commitMutation
Promise-based wrapper around relay-runtime's commitMutation. Lets you
use Relay mutations with async/await instead of orchestrating onCompleted /
onError.
When to use
| ✅ Use when… | 🚫 Avoid when… |
|---|---|
|
|
Example
import { commitMutation } from '@apollion-dsi/relay/commitMutation';
import { Environment } from './environment';
import { MyMutation } from './__generated__/MyMutation.graphql';
async function save(input: MyMutation['variables']['input']) {
try {
const response = await commitMutation<MyMutation>(Environment, {
mutation: graphql`
mutation MyMutation($input: MyInput!) {
myMutation(input: $input) {
ok
}
}
`,
variables: { input },
});
return response.myMutation.ok;
} catch (error) {
console.error('mutation failed', error);
throw error;
}
}Granular imports
This module can be imported in two ways:
// Granular — recommended when the consumer only needs this function.
import { commitMutation } from '@apollion-dsi/relay/commitMutation';
// Via the root barrel — more convenient, brings in more code.
import { commitMutation } from '@apollion-dsi/relay';See also
- Full API:
commitMutation.ts(type reference generated from TSDoc). - Official documentation: Relay Mutations (opens in a new tab).