Fork me on GitHub
#pathom
<
2021-03-27
>
dehli11:03:18

Hello! I have a question about pathom2's cache. I have a resolver that returns a vector of items. How can I associate those items with an ident so that I don’t have to look them up again if they’ve already been returned? The basic code looks like:

(pc/defresolver items-resolver
  [_]
  {::items [{::item {::id 0 ::name "foo" ::depends-on nil}}
            {::item {::id 1 ::name "bar" ::depends-on {::id 0}}}]})

(pc/defresolver item-resolver
  [{id ::id}]
  ;; Don't want this to have to be called for the `depends-on` key in my query below
  {::item (get-from-db)})

(pc/defresolver item-normalizer
  [{item ::item}]
  {::id ...
   ::name ...
   ::depends-on ...})

;; Query
[{::items [::name {::depends-on [::name]}]}]

;; Expected result
{::items [{::name "foo" ::depends-on nil}
          {::name "bar" ::depends-on {::name "foo"}}]}

wilkerlucio17:03:44

Pathom doesn't have the concept of normalization like that

wilkerlucio17:03:01

one thing you can do is manually pre-fill the cache for a resolver you expect is going to be called

wilkerlucio17:03:44

the batch works like this too, you have to add a cache entry on the resolver with the exact input and output you want (the input must have only the keys required by the resolver, otherwise the cache will miss)

dehli01:03:42

Thanks! I think pathom caching can solve what I'm trying to accomplish. Will give that a go!

dehli20:03:25

What do the e and p stand for in the snippet you pasted? I’m running into some issues so my guess is that I’m not exactly supplying the data properly. Currently I have:

(p/cached env ['my.resolver/symbol {:user/id "foo"} {}]
  {:user/output "bar"})
where {:user/id "foo"} is the input into the supplied symbol and {:user/output "bar"} is what it returns. If I don’t wrap the output in a channel I get an error with take!

dehli20:03:56

I see that e is basically input which matches what I’m doing and p are params so it must be my output that is formatted incorrectly which also matches the behavior I’m seeing with a take! error

dehli20:03:19

I think I got it! I needed to use p/cached-async instead! 🎉