Skip to content

Commit

Permalink
Experimenting with returning tempids
Browse files Browse the repository at this point in the history
  • Loading branch information
holyjak committed Sep 11, 2021
1 parent 5e50114 commit aed3d93
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/com/example/app.cljs
Expand Up @@ -6,6 +6,7 @@

(defn global-eql-transform
[ast]
(println "global-eql-transform: AST = " ast)
(-> ast
(app/default-global-eql-transform)
;; Make sure that if Pathom sends ::p/errors, Fulcro does not remove it:
Expand Down
7 changes: 4 additions & 3 deletions src/com/example/mock_server.cljs
Expand Up @@ -14,12 +14,13 @@
transmit! (:transmit! (mock-http-server {:parser (fn [req] (parser env req))}))]
{:transmit! (fn [this send-node]
(js/setTimeout ; simulate some network delay, for fun
#(transmit! this send-node
#(transmit! this
(update send-node
::txn/result-handler
(fn [handler]
(fn logging-wrapper [res] (println "MOCK SERVER RESULT>" res)
(handler res)))))
(fn logging-wrapper [res]
(println "MOCK SERVER RESULT> " res)
(when handler (handler res))))))
100))}))
([]
(mock-remote {})))
8 changes: 7 additions & 1 deletion src/com/example/mutations.cljs
Expand Up @@ -5,9 +5,15 @@
`#?(:clj ...)` but here even the 'server' runs in the browser so we must
define them in another ns, which we do in `...pathom`."
(:require
[com.fulcrologic.fulcro.mutations :refer [defmutation]]))
[com.fulcrologic.fulcro.mutations :as m :refer [defmutation]]
[com.fulcrologic.fulcro.raw.components :as rc]))

(defmutation create-random-thing [{:keys [tmpid]}]
(action [{:keys [state] :as env}]
(swap! state assoc-in [:new-thing tmpid] {:id tmpid, :txt "A new thing!"}))
;(remote [_] true)
(remote [env] true (m/returning env (rc/nc '[:fake]))))


(defmutation failing-mut [_]
(remote [_] true))
11 changes: 9 additions & 2 deletions src/com/example/pathom.cljs
Expand Up @@ -33,14 +33,21 @@
(println "SERVER: Simulate creating a new thing with real DB id 123" tmpid)
{:tempids {tmpid 123}})

(pc/defmutation failing-mut [_ _]
{::pc/sym 'com.example.mutations/failing-mut}
(throw (ex-info "The failing mutation always fails" {:doomed? true})))

(def my-resolvers-and-mutations
"Add any resolvers you make to this list (and reload to re-create the parser)"
[index-explorer create-random-thing i-fail])
[index-explorer i-fail
create-random-thing failing-mut])

(defn new-parser
"Create a new Pathom parser with the necessary settings"
[]
(p/parallel-parser
;; NOTE: By default use `parser` in Clojure and `async-parser` in the 1-threaded JS
;; (so that you can call e.g. js/fetch and return its result via core.async)
(p/async-parser
{::p/env {::p/reader [p/map-reader
pc/parallel-reader
pc/open-ident-reader]
Expand Down
40 changes: 26 additions & 14 deletions src/com/example/ui.cljs
Expand Up @@ -2,6 +2,7 @@
(:require
[com.example.mutations :as mut]
[com.fulcrologic.fulcro.algorithms.merge :as merge]
[com.fulcrologic.fulcro.mutations :as m]
[com.fulcrologic.fulcro.algorithms.tempid :as tempid]
[com.fulcrologic.fulcro.algorithms.data-targeting :as targeting]
[com.fulcrologic.fulcro.algorithms.normalized-state :as norm]
Expand All @@ -11,19 +12,30 @@
[com.fulcrologic.fulcro.dom :as dom :refer [button div form h1 h2 h3 input label li ol p ul]]))

(defsc Root [this props]
{:query [[df/marker-table :load-progress] :new-thing]}
{:query [[df/marker-table :load-progress] :new-thing ::m/mutation-error]}
(div
(p "Hello from the ui/Root component!")
(div {:style {:border "1px dashed", :margin "1em", :padding "1em"}}
(p "Invoke a load! that fails and display the error:")
(when-let [m (get props [df/marker-table :load-progress])]
(dom/p "Progress marker: " (str m)))
(button {:onClick #(df/load! this :i-fail (rc/nc '[*]) {:marker :load-progress})} "I fail!"))
(div {:style {:border "1px dashed", :margin "1em", :padding "1em"}}
(p "Simulate creating a new thing with server-assigned ID, leveraging Fulcro's tempid support:")
(button {:onClick #(let [tmpid (tempid/tempid)]
(comp/transact! this [(mut/create-random-thing {:tmpid tmpid})]))}
"I create!")
(when-let [things (:new-thing props)]
(p (str "Created a thing with the ID: " (first (keys things))))))))
(p "Hello from the ui/Root component!")
;; Load fail
(div {:style {:border "1px dashed", :margin "1em", :padding "1em"}}
(p "Invoke a load! that fails and display the error:")
(when-let [m (get props [df/marker-table :load-progress])]
(dom/p "Progress marker: " (str m)))
(button {:onClick #(df/load! this :i-fail (rc/nc '[*])
{:marker :load-progress})}
"I fail to load!"))
;; Mutate: fail
(div {:style {:border "1px dashed", :margin "1em", :padding "1em"}}
(p "Invoke a *mutation* that fails:")
(button {:onClick #(comp/transact! this [(mut/failing-mut)])}
"I fail to mutate!"))
;; Mutate: create new
(div {:style {:border "1px dashed", :margin "1em", :padding "1em"}}
(p "Simulate creating a new thing with server-assigned ID, leveraging Fulcro's tempid support:")
(button {:onClick #(let [tmpid (tempid/tempid)]
(comp/transact! this [(mut/create-random-thing {:tmpid tmpid})]))}
"I create!")
(when-let [err (::m/mutation-error props)] ; FIXME must fix :remote-error? to mark the err as err
(p {:style {:color :red}} (str "The mutation failed: " err)))
(when-let [things (:new-thing props)]
(p (str "Created a thing with the ID: " (first (keys things))))))))

0 comments on commit aed3d93

Please sign in to comment.