pathom

:pathom: https://github.com/wilkerlucio/pathom/ & https://pathom3.wsscode.com & https://roamresearch.com/#/app/wsscode
dehli 2021-02-08T02:50:11.191Z

With pathom3, is there a way to access the options map that I pass into an operation inside of a plugin? Iโ€™d like to write a plugin that detects if a spec is passed in the options map and if so, validate the params before invoking the mutate. My thoughts are it could be used like below, and then the plugin would be responsible for validating the spec. If this isnโ€™t possible, I could always wrap the defmutation with my own custom macro to accomplish this. Thanks!

(pco/defmutation my-mutation
  [params]
  {::pco/spec ::my-mutation-spec}
  ;; If this is executed, I know that params fulfills the ::my-mutation-spec
  {::success true})

wilkerlucio 2021-02-08T02:58:50.191100Z

yes you can, its a bit different for mutations and resolvers, but both are accessible

wilkerlucio 2021-02-08T02:59:53.191300Z

in the wrap-mutate you get the AST, you can use the :key there to know the mutation name, use that to lookup into ::pci/index-mutations

wilkerlucio 2021-02-08T03:00:11.191500Z

a mutations is a record, you can get the options map with pco/operation-config

wilkerlucio 2021-02-08T03:00:53.191700Z

in the case of a resolver what you get is the node, the node has an ::pco/op-name, you can use that to find the resolver in the ::pci/index-resolvers

wilkerlucio 2021-02-08T03:01:27.191900Z

for convenience, use the helpers pci/mutation-config and pci/resolver-config

dehli 2021-02-08T03:03:24.192100Z

awesome, that helps a lot (and is awesome)! thanks for your guidance!

wilkerlucio 2021-02-08T03:16:18.192500Z

I would suggest name that something like param-spec to make it cleaner, and better to use your own namespace for it (to make the inference of ownership easier)

dehli 2021-02-08T03:23:21.192700Z

good call! i agree that name is cleaner. got it working as well! thanks again!

(p.plugin/defplugin params-spec
  {::p.plugin/id `params-spec
   :com.wsscode.pathom3.connect.runner/wrap-mutate
   (fn [mutate]
     (fn [env {:keys [key params] :as ast}]
       (let [{:keys [params-spec]} (pci/mutation-config env key)]
         (when (and (some? params-spec) (not (s/valid? params-spec params)))
           (throw (ex-info "Invalid params" params
                           {:message (s/explain-str params-spec params)})))
         (mutate env (cond-> ast
                       (some? params-spec)
                       (update :params #(st/select-spec params-spec %)))))))})

henrik 2021-02-08T16:46:14.196Z

In Pathom (2), is there anyway to reconstruct env after (or during) a mutation? This is primarily because we need to use a new Datomic DB value after the mutation has run. The pathom-datomic plugin also seems to use an old DB value after mutation is run.

henrik 2021-02-09T11:05:02.203300Z

> works on reads Whether intentionally or not, it works on mutations too!

henrik 2021-02-09T11:05:50.203500Z

We're currently doing this after a TX to Datomic,

{โ€ฆ
 ::p/env (refresh-env! env db-after)
 :tempids tempids}

henrik 2021-02-09T11:06:38.203900Z

Inserting the updated DB, extracted from the response from Datomic (both for pathom-datomic and our on part of env). It works well.

henrik 2021-02-09T11:09:24.204200Z

> you could also keep db as something mutable in env (as an atom) and update it anytime you see fit (for sync process this should be fine, for parallel not so much) We did this at first, and it was not great. Particularly, pathom-datomic didn't follow suit, but also it's fairly unergonomic. It's easy to get wrong.

souenzzo 2021-02-09T11:09:41.204400Z

with some cache issues, but it really work

(let [register [(pc/resolver `b {::pc/output [:b]}
                             (fn [{:keys [b]} _]
                               {:b b}))
                (pc/mutation `a {}
                             (fn [env _]
                               {::p/env (update env :b inc)}))]
      parser (p/parser {::p/plugins [(pc/connect-plugin {::pc/register register})]
                        ::p/mutate  pc/mutate})
      env {:b         0
           ::p/reader [p/map-reader
                       pc/reader2
                       pc/open-ident-reader
                       p/env-placeholder-reader]}]
  (parser env `[{(a {}) [:b]}]))

henrik 2021-02-09T11:13:12.204600Z

In our case, we only needed it on mutations, as queries are the opposite: we absolutely don't want different parts of the query to use different DBs (or, the DB at different times). That can return some real frankensteinish results.

henrik 2021-02-09T11:14:45.204800Z

And for pagination, we want to be able to keep the DB timestamp stable until the user decides to refresh the query.

eoliphant 2021-02-09T20:01:12.213700Z

did something similar for one of my first plugin attempts, went the atom route

wilkerlucio 2021-02-08T16:56:01.196300Z

trying to remember if that works, but try returning a modified env in the key ::p/env in the return of the mutation data

henrik 2021-02-08T16:58:18.196500Z

Just saw that here: https://blog.wsscode.com/pathom/#updating-env Thanks! Going to check if this also works for mutations.

avocade 2021-02-08T17:02:44.196800Z

Thanks @wilkerlucio that worked :i_love_you_hand_sign::skin-tone-2:

henrik 2021-02-08T17:03:29.197Z

Yep, that worked!

paul931224 2021-02-08T17:31:43.198800Z

Hello everyone! I am trying to setup Pathom3 with Pathom Viz. I started here: https://pathom3.wsscode.com/docs/debugging#connect-the-app but I ran in the stacktrace: (short version)

Caused by: java.io.FileNotFoundException: Could not locate com/wsscode/promesa/macros__init.class, com/wsscode/promesa/macros.clj or com/wsscode/promesa/macros.cljc on classpath.
Do I forget to import something?

wilkerlucio 2021-02-08T17:32:35.199500Z

@paul931224 seems like you are in an outdated version of pathom3, can you try the latest commit? (currently: d9a950c96f3fe3115a54679228dbf92c2ca06c9b)

paul931224 2021-02-08T17:35:06.200700Z

My mistake, I had the good version a while ago, but somewhere in the process I must have overwritten it. Thank you very much! ๐Ÿ™‚

๐Ÿ‘ 1
souenzzo 2021-02-08T21:21:22.201100Z

::p/env only work on reads or in mutations too?

wilkerlucio 2021-02-08T21:27:15.201300Z

works on reads

wilkerlucio 2021-02-08T21:44:39.201500Z

@souenzzo https://blog.wsscode.com/pathom/#updating-env

wilkerlucio 2021-02-08T21:45:18.201700Z

no feature for that in pathom 3, maybe that will get add, but I remember having problems of reliability with it, gotta put some hammock time on it

wilkerlucio 2021-02-08T21:46:20.201900Z

like in some situations the env could leak out, I wanna Pathom 3 to be safer on that