Skip to content

Instantly share code, notes, and snippets.

@souenzzo

souenzzo/pathom.clj

Last active May 20, 2020
Embed
What would you like to do?
(let [conn (doto (ds/create-conn {:user/addresses {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
:user/id {:db/unique :db.unique/identity}})
(ds/transact! [{:user/id 1
:user/addresses [-1 -2]}
{:user/id 2
:user/addresses [-2 -3]}
{:user/id 3
:user/addresses [-4]}
{:db/id -1
:address/id 1
:address/street "one"}
{:db/id -2
:address/id 2
:address/street "two"}
{:db/id -3
:address/id 3
:address/street "3"}
{:db/id -4
:address/id 4
:address/street "four"}]))
register [(pc/resolver `street
{::pc/input #{:address/id}
::pc/batch? true
::pc/output [:address/street]}
(fn [{::keys [db]} addresses]
(prn [:street addresses])
(let [ids (map :address/id addresses)
id->user (into {}
(ds/q '[:find ?id (pull ?e [:address/street])
:in $ [?id ...]
:where
[?e :address/id ?id]]
db))]
(map id->user ids))))
(pc/resolver `addresses
{::pc/input #{:user/id}
::pc/batch? true
::pc/output [:user/addresses]}
(fn [{::keys [db]} users]
(prn [:addresses users])
(let [ids (map :user/id users)
id->user (into {}
(ds/q '[:find ?id (pull ?e [:user/id
{:user/addresses [:address/id]}])
:in $ [?id ...]
:where
[?e :user/id ?id]]
db))]
(map id->user ids))))
(pc/resolver `all-users
{::pc/output [:app/users]}
(fn [{::keys [db]} _]
(prn [:all-users _])
{:app/users (ds/q '[:find [(pull ?e [:user/id]) ...]
:where
[?e :user/id ?id]]
db)}))]
parser (p/parser {::p/plugins [(pc/connect-plugin {::pc/register register})]})
env {::p/reader [p/map-reader
pc/reader3
pc/open-ident-reader
p/env-placeholder-reader]
::db (ds/db conn)
::p/placeholder-prefixes #{">"}}]
(parser env [{:app/users
[:user/id
{:user/addresses [:address/id
:address/street]}]}]))
(comment
[:all-users {}]
[:addresses ({:user/id 2} {:user/id 3} {:user/id 1})]
[:street ({:address/id 2} {:address/id 3})]
[:street ({:address/id 4})]
[:street ({:address/id 1} {:address/id 2})]
=>
{:app/users [{:user/id 2, :user/addresses [{:address/id 2, :address/street "two"} {:address/id 3, :address/street "3"}]}
{:user/id 3, :user/addresses [{:address/id 4, :address/street "four"}]}
{:user/id 1,
:user/addresses [{:address/id 1, :address/street "one"} {:address/id 2, :address/street "two"}]}]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment