W
Wasp3mo ago
Sven

what is in essence the difference between a query and action

I have both queries and actions. I dont really understand the difference. Sometimes an action of mine checks if something exists in database and if it does it returns the already existing value and otherwhise it calculates it and appends it. Is this proper?
4 Replies
kapa.ai
kapa.ai3mo ago
Hi @Sven. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Wasp, Queries and Actions are two closely related concepts, but they are treated differently and each represents a different thing. Here are the key differences between Queries and Actions: 1. State Modification: 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 [source (https://wasp-lang.dev/docs/data-model/operations/actions#differences-between-queries-and-actions)]. 2. Reactivity: Actions don't need to be reactive, so you can call them directly. However, Wasp does provide a useAction React hook for adding extra behavior to the Action (like optimistic updates) [source (https://wasp-lang.dev/docs/data-model/operations/actions#differences-between-queries-and-actions)]. 3. Declaration: action declarations in Wasp are mostly identical to query declarations. The only difference lies in the declaration's name [source (https://wasp-lang.dev/docs/data-model/operations/actions#differences-between-queries-and-actions)]. In your case, if an operation is checking if something exists in the database and returning the existing value or calculating and appending it, it sounds more like an Action because it has the potential to modify the server's state. However, if the operation is only reading data and not modifying it, then it should be a Query. Remember, the main purpose of Actions is to modify and add data, while Queries are solely for reading data [source (https://wasp-lang.dev/docs/data-model/operations/actions)]. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sven, 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?
Sven
SvenOP3mo ago
hmm some indepth explaination would help
Fecony
Fecony3mo ago
In the end of the day it’s just method call on server Ofc “properly” would be something you can mock and test or replace easily in future Like one query to check if something exists, and action to create would be easier to understand, action and query don’t have side effects(creating when you did just check something) Ofc having side effects like that depends on use case and how aware other developers are, in solo project it might be acceptable, if you won’t forget what that action did, and won’t wonder why it does return some default blank record that didn’t exist moment ago
martinsos
martinsos3mo ago
Yeah, so what you want is queries to not have side effects, and actions can have them. So what you did should be an action, yes, because there is a case when you append. What happens in Wasp if you don't obey this? Nothing super terrible, but what can happen is that if Query let's say adds a new Task to list of Tasks, that list of Tasks won't be automatically refreshed on frontend. If you do it via Action, we know you likely changed Tasks so we can refresh, this way we don't know.
Want results from more Discord servers?
Add your server