can a pathom mutation utilize resolvers to load data, or can it only utilize parameters explicitly passed to it?
for example, if I pass a user/id
to the mutation, can i use my already written resolves to load other fields, or would i have to load them from explicitly from whatever data-source Iām using?
@tylernisonoff you can do
(pc/defmutation do-thing [{:keys [parser]
:as env} {:user/keys [id]}]
{::pc/params [:user/id]}
(let [eid [:user/id id]
user-with-address (-> env
(parser [{eid [:user/id
:user/address]}])
(get eid))]
...))
Yeah, I agree with you that pc
could implement something like ::pc/resolved-params
that take ::pc/params
as input and resolve it.Using ::pc/transform
(let [connect-params (fn [{::pc/keys [mutate]
::keys [connected-params]
:as mutation}]
(if connected-params
(assoc mutation
::pc/mutate (fn [{:keys [parser] :as env}
[eid & {:as others}]]
(let [result (parser env `[{(~eid ~{:pathom/context others})
~connected-params}])]
(mutate env (get result eid)))))
mutation))
parser (ps/connect-serial-parser
[(pc/mutation
`op {::pc/params [:a]
::connected-params [:a :b]
::pc/transform connect-params}
(fn [_ params] params))
(pc/resolver
`b {::pc/input #{:a} ::pc/output [:b]}
(fn [_ {:keys [a]}]
{:b (str [:b a])}))])]
(parser {} `[(op {:a 42})]))
awesome! trying that now
perfect ā thanks @souenzzo
I use this small helper in my codebase. I don't like the name of this function but it's useful.
(defn get-from-context
[{:keys [parser] :as env} context query]
(let [[eid & others] context]
(-> (parser env `[{(~eid {:pathom/context ~(into {} others)})
~query}])
(get eid))))
(pc/defmutation do-thing [env params]
{::pc/params [:user/id]}
(let [user-with-address (get-from-context env params [:user/id
:user/address])]
...))
this is possible to implement with a plugin, the gist of it is to get the source params and run a query using that as the initial context
you can do a recursive call to the parser (available in the env), setting ::p/entity
to be a (atom params)
, then send the response of that to the mutation, makes sense?
ups, just realized @souenzzo already replied :)
good to know about the plugin idea too š thanks!
as a suggestion, creating a custom ::pc/transform
can make this look nicer
plugin, or a function to be used as ::pc/transform
that way you can cherry pick which mutations should "auto-resolve" params