faraway9911
faraway99112w ago

hey how do i execute a mutation? i'm

hey how do i execute a mutation? i'm using convex + svelte and i see a useQuery hook but no docs on how to do the mutation. Do i just call the api.whatever.whatever directly?
1 Reply
Kevin07
Kevin072w ago
From the repo Running a mutation looks like
<script>
const client = useConvexClient();

let toSend = $state('');
let author = $state('me');

function onSubmit(e: SubmitEvent) {
const data = Object.fromEntries(new FormData(e.target as HTMLFormElement).entries());
client.mutation(api.messages.send, {
author: data.author as string,
body: data.body as string
});
}
</script>

<form on:submit|preventDefault={onSubmit}>
<input type="text" id="author" name="author" />
<input type="text" id="body" name="body" bind:value={toSend} />
<button type="submit" disabled={!toSend}>Send</button>
</form>
<script>
const client = useConvexClient();

let toSend = $state('');
let author = $state('me');

function onSubmit(e: SubmitEvent) {
const data = Object.fromEntries(new FormData(e.target as HTMLFormElement).entries());
client.mutation(api.messages.send, {
author: data.author as string,
body: data.body as string
});
}
</script>

<form on:submit|preventDefault={onSubmit}>
<input type="text" id="author" name="author" />
<input type="text" id="body" name="body" bind:value={toSend} />
<button type="submit" disabled={!toSend}>Send</button>
</form>
GitHub
GitHub - get-convex/convex-svelte
Contribute to get-convex/convex-svelte development by creating an account on GitHub.

Did you find this page helpful?