Why calling an action from a client is an anti-pattern?
When then is good to use an action from client?
If we need to write a mutation based on the response of a third party API, that's probably the unique case when we can call the action directly from the client?
Context: A note in the docs
6 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Storing files externally is one case where calling an action direct might be as good as any other option, depending on what you’re doing with the result. But yeah, generally you wouldn’t.
Do you have a use case in mind?
The main reasons it's not ideal are that an action doesn't participate in client optimistic updates, and if you run an action from the client any retrying also has to happen on the client which won't work if the user navigates away. Whereas if you schedule an action from a mutation you can do your own retries etc. on the server.
Actions from the client make sense for some things, but mutation -> schedule action -> listen to query reporting status of action is usually more robust.
Thanks for this extra info!
But then what is the best approach to write a mutation that depends on an API response? I mean a “conditional” mutation. I saw this example https://github.com/get-convex/convex-demos/blob/43e3aad5e62cef16b7f91fd700791b5452c79a96/giphy-action/convex/messages.ts#L24
GitHub
convex-demos/giphy-action/convex/messages.ts at 43e3aad5e62cef16b7f...
Demo apps built on Convex. Contribute to get-convex/convex-demos development by creating an account on GitHub.
The doc you quoted and Tom's comment both loosely describe this, although I'm not sure what the "conditional" part you mentioned means.
See the second code sample in this section for an example: https://docs.convex.dev/functions/actions#calling-actions-from-clients
Actions | Convex Developer Hub
Actions can call third party services to do things such as processing a payment
I realize this is the same thing you linked in your OP, but this is the best approach for writing a mutation that depends on an API response.