Skip to content

Instantly share code, notes, and snippets.

@bshepherdson
Created May 2, 2024 02:51
Show Gist options
  • Save bshepherdson/b5667894a37220a25627c18811cb1e63 to your computer and use it in GitHub Desktop.
Save bshepherdson/b5667894a37220a25627c18811cb1e63 to your computer and use it in GitHub Desktop.
Repro of my issues with Pathom 3 nested data structures
(ns repro
(:require
[clojure.test :refer [deftest is testing]]
[com.wsscode.pathom3.connect.built-in.resolvers :as pbir]
[com.wsscode.pathom3.connect.indexes :as pci]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.interface.eql :as p.eql]))
(deftest more-input-more-problems
(let [map-data {123 {:data/name "Alice"}
456 {:data/name "Bob"}
789 {:data/name "Carol"}}
map-guts {::pco/op-name `big-map
::pco/output [::big-map]
::pco/resolve (fn [_env _input] {::big-map map-data})}
map-no-input (pco/resolver map-guts)
map-input (-> map-guts
(assoc ::pco/input [::problem-input])
pco/resolver)
;; Attribute table resolver to break down the big map.
atr (pbir/attribute-table-resolver
::big-map :data/id [:data/id :data/name])
;; Just the odd IDs - 123 and 789.
customers (pco/resolver
`customers
{::pco/input [::big-map]
::pco/output [::customers]}
(fn [_env {m ::big-map}]
{::customers (for [id (keys m)
:when (odd? id)]
{:data/id id})}))
no-input (pci/register [map-no-input customers atr])
input (pci/register [map-input customers atr])
magic (pci/register [map-input customers atr
(pbir/constantly-resolver ::problem-input 1)])]
(testing "top-level things work"
(testing "without an input"
(is (= {::big-map map-data}
(p.eql/process no-input [::big-map])))
(is (= {:data/name "Alice"}
(p.eql/process no-input {:data/id 123} [:data/name]))))
(testing "with an input"
(is (= {::big-map map-data}
(p.eql/process input {::problem-input 1} [::big-map])))
(is (= {:data/name "Alice"}
(p.eql/process
input
{::problem-input 1, :data/id 123}
[:data/name]))))
(testing "magic too"
(is (= {::big-map map-data}
(p.eql/process magic [::big-map])))
(is (= {:data/name "Alice"}
(p.eql/process magic {:data/id 123} [:data/name])))))
(testing "nested query"
(testing "works without an input"
(is (= {::customers [{:data/name "Alice"} {:data/name "Carol"}]}
(p.eql/process no-input [{::customers [:data/name]}]))))
(testing "fails to plan with an input"
(is (thrown-with-msg?
Exception #"Pathom can't find a path for the following elements"
(p.eql/process input {::problem-input 1} [{::customers [:data/name]}]))))
(testing "weirdly it works with constantly-resolver and I don't know why"
(is (= {::customers [{:data/name "Alice"} {:data/name "Carol"}]}
(p.eql/process magic [{::customers [:data/name]}])))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment