Fork me on GitHub
#pathom
<
2020-05-05
>
sergey.shvets05:05:19

@wilkerlucio Thanks for your suggestions yesterday. Both worked like a charm.

sergey.shvets05:05:16

Here is my pass-down params script for reference:

(def params-down-pathom-plugin
  {::p/wrap-read (fn [reader]
                   (fn [env]
                     (if-let [params (get-in env [:ast :params])]
                       (reader (as-> 
                                (map (fn [child]
                                       [(:dispatch-key child) params])
                                     (get-in env [:ast :children])) $
                                 (into {} $)
                                 (update env :parent-params merge $)))
                       (reader env))))})

(defn parse-query-params
  "Parses query and ast to see if there any params related to the current query. Returns map with params or empty map."
  [env]
  (let [dispatch-key (get-in env [:ast :dispatch-key])
        params       (get-in env [:parent-params dispatch-key] {})]
    params))

ouvasam13:05:05

Hi do you have an example of query to use with it ? Thanks

sergey.shvets15:05:09

[{([:hand-and-foot.games/id "TKSbTbE6O5Q0U89JhuHc"] {:with "params"})
  [:hand-and-foot.games/name
   :hand-and-foot.games/players
   :hand-and-foot.games/status 
   ({:hand-and-foot.games/rounds 
      [:hand-and-foot.game-rounds/id
       :hand-and-foot.game-rounds/number]} {:limit 2 :order-by :hand-and-foot.game-rounds/number})]}]

sergey.shvets15:05:36

Inside a resolver you need to call parse-query-params and it will figure out if there are any params that are related to this resolver. It rolls down params only one level to the immediate children of a node where params have been applied.

sergey.shvets15:05:12

It shouldn't be too hard to roll it down all the way, but I think it will do more harm than good.

wilkerlucio18:05:15

another option is roll it down via :env instead of changing the AST, this way you can have "accumulated" (could go deep merging all the way down) and the current

sergey.shvets18:05:53

I haven't changed ast, I just added a new parameter to env

wilkerlucio21:05:08

oh, right, I misread it 馃憤

ouvasam12:05:36

Many thanks !

sergey.shvets05:05:46

Unless you have same dispatch keys with different params in your query it will support multiple params in different places of a query.

sergey.shvets15:05:15

Can query have multiple idents for one join? Or you need to always create some "join node" that will accept multiple values as ident param and set them to the env?

sergey.shvets15:05:58

I had a thought that probably the requirement for Idents can be loosened a bit from "only 2 arguments" to even number of arguments and I feel like this will open more opportunities, without changing parser/ast too much

kszabo15:05:10

I proposed this before, but was rejected: https://github.com/edn-query-language/eql/pull/9

kszabo15:05:38

I have since wrote support for it in Pathom, so it works with an extra reader

kszabo15:05:03

but it brings up a lot of questions on the client side, especially data normalization

kszabo15:05:19

but with this syntax you can write quries like:

[{{:latitude  42
   :longitude 42.3}
 [:location/name]}]

kszabo15:05:34

without having to think up of an ident beforehand

kszabo15:05:15

In my mind this is still way cleaner than the pathom parser specific :pathom/context hack

kszabo15:05:38

but I understand the concerns, as this diverges greatly from the status quo.

sergey.shvets16:05:58

Thanks for sharing. I haven't seen those before, since I'm very new to pathom and eql in general. I was planning to implement something similar to what Wilker suggesting here https://github.com/wilkerlucio/pathom/issues/140 with expand-thing-compound and that should cover my problem. But on the other hand, allowing longer vectors in idents doesn't seem like a huge change to me. Of course, there might be nuances that I don't grasp yet. Just wanted to share an idea. The workaround with expand-thing-compound is very simple one and pretty much need one extra resolver per project.

kszabo17:05:36

one extra resolver per compound ident, I think

kszabo17:05:52

in my case I have 26 of these, of varying combinations 馃檪

kszabo17:05:06

so this blows up quickly

sergey.shvets17:05:58

Oh, I get what you're saying. We can't specify outputs for resolver without knowing what will be inside the ident param

馃憤 4
sergey.shvets17:05:24

so, the best way for now is to use this:

[{([:init-context/a 42] {:pathom/context {:init-context/b "foo"}}) [:want/this]}]

sergey.shvets17:05:09

I like your suggestion,

[{{:init-context/a 42 :init-context/b "foo"} [:want/this]}]
but to minimize changes to spec I think it would be better to keep it as this
[{[:init-context/a 42 :init-context/b "foo"] [:want/this]}]

sergey.shvets17:05:58

If I'm not missing something then you just need an extra reader and loosen the spec if it has a validation for exactly two to the even?

sergey.shvets17:05:36

How did you write reader for map ident in Pathom? Have you changed eql specs or pathom doesn't check for validity of the query beforehand?

kszabo18:05:41

yup, the whole ordeal

kszabo18:05:34

I dislike the a 2+ vector idea as this join context as I call it is an associative ideas which we already have a perfectly fine representation for

kszabo18:05:48

if I diverge from EQL, might as well use the best datastructure for it

kszabo18:05:25

of course the idea of how to determine the dispatch key came up, which is just currently ffirst, so it depends on the hashmap in-memory ordering guarantees, but this hasn鈥檛 bit me yet

sergey.shvets18:05:50

Isn't dispatch key for ident the whole vector? (or map in your case)

sergey.shvets18:05:49

Actually, disregard. I see that it isn't. It is :first

sergey.shvets18:05:01

I guess for now, I'll have to bite the bullet and use :pathom/context 馃檪

馃憤 4
kszabo18:05:19

yeah, the coming foreign parser support also (ab)uses that

sergey.shvets18:05:44

what is foreign parser?

kszabo18:05:57

馃か 馃檪

sergey.shvets18:05:54

Actually, it seems like pathom context doesn't work for my case or I'm doing it totally wrong.

sergey.shvets18:05:12

I have a resolver that has input #{:parent/id child/id} and trying to call it: [{'([:child/id "a2coh2mnsIyd35CL8PVv"] {:pathom/context {:parent/id "TKSbTbE6O5Q0U89JhuHc"}}) [:child/data]}]

sergey.shvets18:05:25

and it isn't finding the proper resolver

souenzzo16:05:09

When I connect my app into pathom-viz, autocomplete works but index explorer don't How can I debug it? public API URL: public code: https://github.com/souenzzo/caderninho/blob/master/src/br/com/souenzzo/caderninho.clj#L24

souenzzo17:05:10

Solution it's something with transit/unknown tags (sp/setval (sp/walker fn?) sp/NONE response)

souenzzo17:05:12

Fixed by removing any function from response

sergey.shvets18:05:54

I have a resolver that has input #{:parent/id :child/id} and trying to call it: [{'([:child/id "a2coh2mnsIyd35CL8PVv"] {:pathom/context {:parent/id "TKSbTbE6O5Q0U89JhuHc"}}) [:child/data]}] Get not-found in response. Is this supposed to work? Because I can't see what am I doing wrong.

wilkerlucio21:05:15

@bear-z this should work, can you also try adding '* to your query? so you can see all data available

wilkerlucio21:05:24

[{'([:child/id "a2coh2mnsIyd35CL8PVv"] {:pathom/context {:parent/id "TKSbTbE6O5Q0U89JhuHc"}}) [:child/data '*]}]

sergey.shvets21:05:20

No luck: Here is my query: https://take.ms/05ecK And here is the resolver it supposed to call: https://take.ms/6IOp1

wilkerlucio21:05:13

are you using the open-ident-reader?

sergey.shvets21:05:57

ident-reader :

(p/async-parser
                               {::p/env     {::p/reader               [p/map-reader
                                                                       pc/async-reader2
                                                                       pc/ident-reader
                                                                       p/env-placeholder-reader]
                                             ::p/placeholder-prefixes #{">"}}
                                ::p/mutate  pc/mutate-async
                                ::p/plugins [(pc/connect-plugin {::pc/register pathom-resolvers})
                                             p/error-handler-plugin
                                             p/trace-plugin
                                             params-down-pathom-plugin]}))

sergey.shvets21:05:29

open-ident-reader does solve it.