Skip to content

Commit

Permalink
Add support for ::pc/transform in id-resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
tylernisonoff committed Dec 29, 2020
1 parent cbb90f5 commit ae7b98a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 36 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
:extra-deps {dev-local-tu {:mvn/version "0.1.0"}
; N.B. - follow instuctions for installing dev-local
; https://docs.datomic.com/cloud/dev-local.html#using
com.datomic/dev-local {:mvn/version "0.9.203"}
com.datomic/dev-local {:mvn/version "0.9.195"}
com.datomic/client-cloud {:mvn/version "0.8.102"}}}

:provided {:extra-deps {com.datomic/datomic-free {:mvn/version "0.9.5697"
Expand Down
35 changes: 18 additions & 17 deletions src/main/com/fulcrologic/rad/database_adapters/datomic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@
(>defn id-resolver
"Generates a resolver from `id-attribute` to the `output-attributes`."
[all-attributes
{::attr/keys [qualified-key] :keys [::attr/schema ::wrap-resolve] :as id-attribute}
{::attr/keys [qualified-key] :keys [::attr/schema ::wrap-resolve ::pc/transform] :as id-attribute}
output-attributes]
[::attr/attributes ::attr/attribute ::attr/attributes => ::pc/resolver]
(log/info "Building ID resolver for" qualified-key)
Expand All @@ -409,22 +409,23 @@
(r (assoc env ::pc/sym resolve-sym) input)))]
(log/debug "Computed output is" outputs)
(log/debug "Datomic pull query to derive output is" pull-query)
{::pc/sym resolve-sym
::pc/output outputs
::pc/batch? true
::pc/resolve (cond-> (fn [{::attr/keys [key->attribute] :as env} input]
(->> (entity-query
(assoc env
::attr/schema schema
::attr/attributes output-attributes
::id-attribute id-attribute
::default-query pull-query)
input)
(common/datomic-result->pathom-result key->attribute outputs)
(auth/redact env)))
wrap-resolve (wrap-resolve)
:always (with-resolve-sym))
::pc/input #{qualified-key}})
(cond-> {::pc/sym resolve-sym
::pc/output outputs
::pc/batch? true
::pc/resolve (cond-> (fn [{::attr/keys [key->attribute] :as env} input]
(->> (entity-query
(assoc env
::attr/schema schema
::attr/attributes output-attributes
::id-attribute id-attribute
::default-query pull-query)
input)
(common/datomic-result->pathom-result key->attribute outputs)
(auth/redact env)))
wrap-resolve (wrap-resolve)
:always (with-resolve-sym))
::pc/input #{qualified-key}}
transform transform))
(do
(log/error "Unable to generate id-resolver. "
"Attribute was missing schema, or could not be found in the attribute registry: " qualified-key)
Expand Down
38 changes: 21 additions & 17 deletions src/main/com/fulcrologic/rad/database_adapters/datomic_cloud.clj
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
(>defn id-resolver
"Generates a resolver from `id-attribute` to the `output-attributes`."
[all-attributes
{::attr/keys [qualified-key] :keys [::attr/schema ::wrap-resolve] :as id-attribute}
{::attr/keys [qualified-key] :keys [::attr/schema ::wrap-resolve ::pc/transform] :as id-attribute}
output-attributes]
[::attr/attributes ::attr/attribute ::attr/attributes => ::pc/resolver]
(log/info "Building ID resolver for" qualified-key)
Expand All @@ -297,22 +297,24 @@
(r (assoc env ::pc/sym resolve-sym) input)))]
(log/debug "Computed output is" outputs)
(log/debug "Datomic pull query to derive output is" pull-query)
{::pc/sym resolve-sym
::pc/output outputs
::pc/batch? true
::pc/resolve (cond-> (fn [{::attr/keys [key->attribute] :as env} input]
(->> (entity-query
(assoc env
::attr/schema schema
::attr/attributes output-attributes
::id-attribute id-attribute
::default-query pull-query)
input)
(common/datomic-result->pathom-result key->attribute outputs)
(auth/redact env)))
wrap-resolve (wrap-resolve)
:always (with-resolve-sym))
::pc/input #{qualified-key}})
(cond-> {::pc/sym resolve-sym
::pc/output outputs
::pc/batch? true
::pc/resolve (cond-> (fn [{::attr/keys [key->attribute] :as env} input]
(->> (entity-query
(assoc env
::attr/schema schema
::attr/attributes output-attributes
::id-attribute id-attribute
::default-query pull-query)
input)
(common/datomic-result->pathom-result key->attribute outputs)
(auth/redact env)))
wrap-resolve (wrap-resolve)
:always (with-resolve-sym))
::pc/input #{qualified-key}
}
transform transform))
(do
(log/error "Unable to generate id-resolver. "
"Attribute was missing schema, or could not be found in the attribute registry: " qualified-key)
Expand Down Expand Up @@ -394,3 +396,5 @@
([]
(fn [{::form/keys [params] :as pathom-env}]
(delete-entity! pathom-env params))))


Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,16 @@
::person/primary-address {::address/id addr-id
::address/street "A St"}})))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Attr Options Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(specification "Attribute Options"
(let [person-resolver (first (datomic/generate-resolvers person/attributes :production))]
(component "defattr applies ::pc/transform to the resolver map"
(assertions
"person resolver has been transformed by ::pc/transform"
(do
(log/spy :info person-resolver)
(::person/transform-succeeded person-resolver)) => true
))))
13 changes: 13 additions & 0 deletions src/test/com/fulcrologic/rad/database_adapters/datomic_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,16 @@
::person/primary-address {::address/id addr-id
::address/street "A St"}})))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Attr Options Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(specification "Attribute Options"
(let [person-resolver (first (datomic/generate-resolvers person/attributes :production))]
(component "defattr applies ::pc/transform to the resolver map"
(assertions
"person resolver has been transformed by ::pc/transform"
(do
(log/spy :info person-resolver)
(::person/transform-succeeded person-resolver)) => true
))))
7 changes: 6 additions & 1 deletion src/test/com/fulcrologic/rad/test_schema/person.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
[com.fulcrologic.rad.form :as form]
[com.fulcrologic.rad.attributes :as attr :refer [defattr]]
[com.fulcrologic.rad.authorization :as auth]
[com.wsscode.pathom.connect :as pc]
[com.fulcrologic.rad.database-adapters.datomic :as datomic]
[com.fulcrologic.rad.database-adapters.datomic-options :as do]
[taoensso.timbre :as log]))

(defattr id ::id :long
{::attr/identity? true
do/native-id? true
::attr/schema :production})
::attr/schema :production
::pc/transform (fn [resolver]
(assoc resolver ::transform-succeeded true))

})

(defattr full-name ::full-name :string
{::attr/schema :production
Expand Down

0 comments on commit ae7b98a

Please sign in to comment.