W
Wasp5d ago
cooluke.

Can I kick off an `action` from within a query?

If one of my queries finds that there are no results in the DB its search, I want to asynchronously kick off an "action" that I have to populate the DB. Am I allowed (in WASP) to send an action from within a query?
2 Replies
kapa.ai
kapa.ai5d ago
Hi @cooluke.. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the provided knowledge sources, there isn't explicit information about kicking off an action from within a query in Wasp. However, I can provide some relevant information about the nature of queries and actions in Wasp that might help address your question: 1. Queries and actions have distinct purposes in Wasp: - Queries are meant for reading data and should not modify the server's state. - Actions are designed to modify and add data. Wasp Documentation - Operations Overview states: "Queries are meant for reading data, and Actions are meant for changing it (either by updating existing entries or creating new ones)." 2. There's a clear separation between queries and actions: Wasp Documentation - Differences Between Queries and Actions mentions: "Actions can (and often should) modify the server's state, while Queries are only permitted to read it. Wasp relies on you adhering to this convention when performing cache invalidations, so it's crucial to follow it." Given this information, it appears that calling an action from within a query would go against the design principles of Wasp. Queries are intended to be read-only operations, and initiating an action (which modifies data) from within a query could lead to unexpected behavior and potentially break the cache invalidation mechanisms. Instead, you might want to consider alternative approaches: 1. Handle the "no results" case in your client-side code and trigger the action from there. 2. Create a separate action that combines the query and the population logic, which you can call instead of the query when you need this behavior. However, to get a definitive answer about whether this is allowed or if there's a recommended way to achieve your specific use case, you might want to consult the Wasp documentation directly or reach out to the Wasp community for more specific guidance. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cooluke., I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho2d ago
Actions | Wasp
We'll explain what Actions are and how to use them. If you're looking for a detailed API specification, skip ahead to the API Reference.

Did you find this page helpful?