Skip to content

Instantly share code, notes, and snippets.

@bwalex
Created July 18, 2018 17:49
Show Gist options
  • Save bwalex/ee0351addb6d9e2c1d318b72c78446ac to your computer and use it in GitHub Desktop.
Save bwalex/ee0351addb6d9e2c1d318b72c78446ac to your computer and use it in GitHub Desktop.
(ns dlt-backend.pathom-trial
(:require
[com.wsscode.pathom.core :as p]
[com.wsscode.pathom.connect :as pc]
[camel-snake-kebab.core :as csk]
[clojure.set :as set]))
(def indexes (atom {}))
(defmulti resolve-fn pc/resolver-dispatch)
(def defresolver (pc/resolver-factory resolve-fn indexes))
(defn namespace-keys [x ns]
(into {} (map (fn [[k v]] [(keyword ns (csk/->kebab-case (name k))) v])) x))
(defresolver `task-by-id
{::pc/input #{:dlt.task/id}
::pc/output [:dlt.task/id
:dlt.task/summary
:dlt.task/tags [:dlt.tag/id]
:dlt.project/id]}
(fn [_ {:keys [dlt.task/id]}]
(println "task-by-id" id)
{:dlt.task/id id
:dlt.task/summary "Eat dinner"
:dlt.task/something "Something else"
:dlt.task/tags [{:dlt.tag/id "tag1"}
{:dlt.tag/id "tag2"}]
:dlt.project/id "project1"}))
(defresolver `tag-by-id
{::pc/input #{:dlt.tag/id}
::pc/output [:dlt.tag/id
:dlt.tag/name
:dlt.tag/color
:dlt.project/id]}
(fn [v {:keys [dlt.tag/id]}]
(println "tag-by-id resolver called with id" id " and v" v)
{:dlt.tag/id id
:dlt.tag/name (str "name-" id)
:dlt.tag/color (str "color-" id)}))
;; (defresolver `tag-by-id-batch
;; {::pc/input #{:dlt.tag/id}
;; ::pc/output [:dlt.tag/id
;; :dlt.tag/name
;; :dlt.tag/color
;; :dlt.project/id]
;; ::pc/batch? true}
;; (fn [_ input]
;; (println "tag-by-id resolver called with input" input)
;; (if (sequential? input)
;; (for [{:keys [dlt.tag/id]} input]
;; {:dlt.tag/id id
;; :dlt.tag/name (str "batch-name-" id)
;; :dlt.tag/color (str "batch-color-" id)})
;; (let [{:keys [dlt.tag/id]} input]
;; {:dlt.tag/id id
;; :dlt.tag/name (str "name-" id)
;; :dlt.tag/color (str "color-" id)}))))
(defresolver `project-by-id
{::pc/input #{:dlt.project/id}
::pc/output [:dlt.project/id
:dlt.project/name
:dlt.namespace/id]}
(fn [_ {:keys [dlt.project/id]}]
{:dlt.project/id id
:dlt.project/name "Project #1"
:dlt.namespace/id "ns1"}))
(defresolver `namespace-by-id
{::pc/input #{:dlt.namespace/id}
::pc/output [:dlt.namespace/id
:dlt.namespace/name]}
(fn [_ {:keys [dlt.namespace/id]}]
{:dlt.namespace/id "ns1"
:dlt.namespace/name "Namespace #1"}))
(def parser
(p/parser {::p/plugins
[(p/env-plugin
{::p/reader [p/map-reader
pc/all-readers]
::pc/resolver-dispatch resolve-fn
::pc/indexes @indexes})]}))
(parser {} [{[:dlt.task/id "task1"] [:dlt.task/id :dlt.task/summary :dlt.project/name {:dlt.task/tags [:dlt.tag/name :dlt.tag/color]}]}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment