Good morning, I am tripping on the mutation data flow, for example:
#?(:cljs
;; client-side
(m/defmutation delete-employee [{id :employee/id :as params}] ;
(action [{:keys [app state] :as env}] ;
(swap! state update :employee/id dissoc id))
(remote [env] true) ;
(result-action [{:keys [app state result]}] ;
(println "Did it work?")))) ; can I use result from server here?
:clj
;; server-side
(pc/defmutation delete-employee [env {id :employee/id :as params}] ;
{::pc/params #{:employee/id}}
(db/delete-employee id))
;; the db/delete-employee returning
(defn delete-employee [id]
(comment returns {:delete/result "OK"} if employee id is found)
(comment returns {:delete/result "FAILED"} if employee id is not found))
When the delete-employee!
mutation is triggered, assuming the result on the server can be {:delete/result "OK"}
or {:delete/result "FAILED}
, how does the the result get passed back to the client so that it can be used in the result-action
section?@mroerni , one more question, why the
`delete-employee
instead of just
::delete-employee
?The mutation name is a symbol, not a keyword.
(get-in env [:result :body `delete-employee])
You should read about result-action
though. (https://book.fulcrologic.com/#_result_action)
Then you can decide if you want to use ok-action
, which is (per default) called when the network request has the status 200, or if you want to augment result-action
to call ok-action
only when {:delete/result "OK"}
.
@mroerni, Thank you and honestly, I have a hard time reading the Fulcro book, there are soooo many angles to everything. The pointed answer is like hours and hours of reading.
I understand that. 🙂 @holyjak has written a blog post about error handling. https://blog.jakubholy.net/2020/error-handling-in-fulcro/ But I have to admit that it isn't very beginner-friendly as well.