Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Last active November 30, 2020 22:56
Show Gist options
  • Save souenzzo/5eb8387425212450519d0b754cc34445 to your computer and use it in GitHub Desktop.
Save souenzzo/5eb8387425212450519d0b754cc34445 to your computer and use it in GitHub Desktop.
;pallet-cost, pallet-count, book-cost, book-counttotal-book-cost:
; if present (pallet-cost, pallet-count) (* pallet-cost pallet-count)
; if present (book-cost, book-count) (* book-cost, book-count)
; else err
; (total-book-cost {:pallet-cost 1 :pallet-count 2}) => 2
; (total-book-cost {:book-cost 4 :book-count 4}) => 16
(let [;; Relevant part: the resolvers
registers [(pc/resolver `total-book-cost-from-book
{::pc/input #{:book-cost
:book-count}
::pc/output [:total-book-cost]}
(fn [_ {:keys [book-cost book-count]}]
{:total-book-cost (* book-cost book-count)}))
(pc/resolver `total-book-cost-from-pallet
{::pc/input #{:pallet-count
:pallet-cost}
::pc/output [:total-book-cost]}
(fn [_ {:keys [pallet-count pallet-cost]}]
{:total-book-cost (* pallet-count pallet-cost)}))]
;; pathom2 parser. Pathom3 is simpler
parser (p/parser {::p/plugins [(pc/connect-plugin)]})
env {::p/reader [p/map-reader
pc/reader2
pc/open-ident-reader
env-placeholder-reader-v2] ;; I backported pathom3 placeholders to pathom2
::pc/indexes (pc/register {} registers)
::p/placeholder-prefixes #{">"}}]
(parser env `[{(:>/pallet {:pallet-cost 1 :pallet-count 2})
[:total-book-cost]}])
=> {:>/pallet {:total-book-cost 2}}
(parser env `[{(:>/book {:book-cost 4 :book-count 4})
[:total-book-cost]}])]
=> {:>/book {:total-book-cost 16}})
;; pathom3 example
(pco/defresolver total-book-cost-from-book
[{:keys [book-cost book-count]}]
{:total-book-cost (* book-cost book-count)})
;; => #'user/total-book-cost-from-book
(pco/defresolver total-book-cost-from-pallet
[{:keys [pallet-count pallet-cost]}]
{:total-book-cost (* pallet-count pallet-cost)})
;; => #'user/total-book-cost-from-pallet
(def env (pci/register [total-book-cost-from-book total-book-cost-from-pallet]))
;; => #'user/env
(p.eql/process env `[{(:>/pallet {:pallet-cost 1 :pallet-count 2})
[:total-book-cost]}
{(:>/book {:book-cost 4 :book-count 4})
[:total-book-cost]}])
;; => {:>/pallet {:total-book-cost 2}, :>/book {:total-book-cost 16}}
;;
;; Solution by Wilker
(defn attr-total-cost-resolver [attr]
(let [cost-kw (keyword (str attr "-cost"))
count-kw (keyword (str attr "-cost"))
total-cost-kw (keyword (str "total-" attr "-cost"))
sym (symbol (str "total-book-cost-from-" attr))]
[(pc/resolver sym
{::pc/input #{cost-kw
count-kw}
::pc/output [total-cost-kw]}
(fn [_ input]
{total-cost-kw (* (cost-kw input) (count-kw input))}))
(pc/alias-resolver total-cost-kw :total-book-cost)]))
(let [;; Relevant part: the resolvers
registers [(attr-total-cost-resolver "book")
(attr-total-cost-resolver "pallet")]
;; pathom2 parser. Pathom3 is simpler
parser (p/parser {::p/plugins [(pc/connect-plugin)]})
env {::p/reader [p/map-reader
pc/reader2
pc/open-ident-reader
env-placeholder-reader-v2] ;; I backported pathom3 placeholders to pathom2
::pc/indexes (pc/register {} registers)
::p/placeholder-prefixes #{">"}}]
(parser env `[{(:>/pallet {:pallet-cost 1 :pallet-count 2})
[:total-book-cost]}]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment