Chris
Chris2w ago

Action Triggers from Convex Queries

I’m trying to figure out how to trigger an action from a query in Convex. Currently, the only option available is a mutation, which seems limiting. I understand why and I also understand that it's an anti pattern to even want to do it. That said, there are situations where you want to trigger an action when a user fetches data that isn't available. In those cases, you would want to initiate an action to fetch that data from an external service, populate it, and let sync do its thing. The only alternative I can think of is to have the client code manage this. The client would run a query, and if it returns undefined or an empty dataset, it would then trigger a mutation or action to fetch the data. However, I’m concerned about this approach. What if the client code doesn't handle it properly? How can we ensure they do it correctly?
4 Replies
Convex Bot
Convex Bot2w ago
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!
Clever Tagline
One option is to schedule the action. The action function can be designed to handle the data retrieval from the external service as well as updating any Convex tables with what's returned. Would that work in your use case?
erquhart
erquhart2w ago
Queries can't schedule, either. They can't do anything but read. If you think of a query as a read from the database, rather than just a way to pull any kind of data (such as external via fetch), then it makes more sense to get this data you're trying to access via mutation or action from the client. Generally you don't want to call actions from the client, so you'd use a mutation to schedule the action as Clever recommended. The mutation can serve the purpose of capturing intent or status, eg., you can write state to the db to capture that the user attempted to access x data, and then update when the data is successfully fetched for state management purposes.
Chris
ChrisOP5d ago
Got it. Thank you

Did you find this page helpful?