pathom

2023-09-28T07:33:26.048249Z

Hello everyone! It's my first couple of weeks using Pathom and I got amazing results from it! Really great piece of software metal I've ran into a blocker though, which is probably skills issues ๐Ÿ™‚ Maybe someone can point me to the right direction. So I have a batch resolver that returns a vector of things. Is it possible to create a resolver that groups the output from the batch resolver and returns a map? I've tried the straightforward way of specifying the the input of the grouping resolver is the output of the batch resolver but then the grouping resolver just returns a vector of things as well, which makes sense cause I'm guessing it's triggered per each output of the batch resolver. So maybe I'm just trying to do something that Pathom isn't supposed to do... ๐Ÿคท Appreciate any help. And grateful for Pathom ๐Ÿ™

wilkerlucio 2023-09-29T13:24:58.254789Z

glad to hear you got it figured! another place you can see a similar example is in the docs for resolvers using nested inputs: https://pathom3.wsscode.com/docs/resolvers#nested-inputs

henrik 2023-09-28T08:13:40.436119Z

Are you looking to do something like a reduce step in Pathom? If so, Iโ€™m not aware of any way to do that except by calling Pathom twice, with the reduce step between the calls.

2023-09-28T08:26:02.738539Z

> Are you looking to do something like a reduce step in Pathom? Yep, I guess that's what I'm trying to do ๐Ÿ™‚

henrik 2023-09-28T08:43:22.706129Z

I mean, you could theoretically call Pathom from a Pathom resolver and then reduce in the resolver. I havenโ€™t had a reason to do so, but I donโ€™t see why it wouldnโ€™t be OK. Any caveats @wilkerlucio?

wilkerlucio 2023-09-28T13:57:18.616229Z

hello @countgizmo, thanks for the kind message and welcome, I'm trying to understand the shape of things you are trying to reduce from, when we talk batch we talk different entities grouping their request to optimize the load, so a batch always returns a vector given it has to distribute the results back to the callers

wilkerlucio 2023-09-28T13:57:45.345439Z

so I'm trying to understand where you are trying to inject the "grouped" results of that, can you give some example steps of how you image the data flowing?

henrik 2023-09-28T14:37:32.001839Z

Basically, you have some resolver returning a batch of [A B C], letโ€™s say, and the goal is to take those (collectively) as input to some other resolver and produce a single entity D. Iโ€™m not aware of a way to do this with a single Pathom call, unless you recursively invoke Pathom in the resolver of D.

wilkerlucio 2023-09-28T14:38:53.779829Z

you can do something if you are in some control over the structure, but it really depends, if for instance you have a collection where each item is a value of the batch, you can in the parent ask for all of that and reduce

wilkerlucio 2023-09-28T14:39:43.532129Z

but in Pathom 3, batches go beyond a single sequence, so they might be anywhere in your request, and altough you could gather from a specific list, you can't when they are disparated, that's why I'm wonder what is the kind of shape transformations you look for

wilkerlucio 2023-09-28T16:23:17.548229Z

to illustrate, this is what I mean if they are a shared list in the children you can combine then with a nested input resolver:

wilkerlucio 2023-09-28T16:23:19.295109Z

(ns demos.batch-reduce
  (:require [com.wsscode.pathom3.connect.indexes :as pci]
            [com.wsscode.pathom3.connect.operation :as pco]
            [com.wsscode.pathom3.interface.eql :as p.eql]))

(def user-scores
  {1 {:user/score 10}
   2 {:user/score 10}
   3 {:user/score 10}})

(pco/defresolver all-users []
  {::pco/output
   [{:user.refs/all
     [:user/id]}]}
  {:user.refs/all
   (mapv #(array-map :user/id %) (keys user-scores))})

(pco/defresolver user-score [ids]
  {::pco/batch? true
   ::pco/input  [:user/id]
   ::pco/output [:user/score]}
  (mapv #(get user-scores (:user/id %)) ids))

(pco/defresolver scores-combined [{:keys [user.refs/all]}]
  {::pco/input [{:user.refs/all [:user/score]}]}
  {:user.reductions/scores-sum
   (transduce (map :user/score) + 0 all)})

(def env
  (pci/register
    [all-users
     user-score
     scores-combined]))

(comment
  (p.eql/process env [:user.reductions/scores-sum]))

wilkerlucio 2023-09-28T16:23:46.554689Z

this not really a batch thing (would work the same without batching) but afaik it may be the kind of thing you are looking for

2023-09-29T06:56:58.550359Z

@wilkerlucio Thank you so much for the detailed example! ๐Ÿ™ I've used it as a reference and got my own code to work ๐ŸŽ‰ The example was very useful. I have also noticed that I need to better understand the EQL format for inputs. This line {::pco/input [{:user.refs/all [:user/score]}]} was actually a big revelation for me ๐Ÿ’ก I'll go study https://edn-query-language.org/eql/1.0.0/specification.html#_joins more carefully. And thank you @henrik for your analysis of my issue. It also helped me understand what I'm actually trying to achieve ๐Ÿ™

๐Ÿ‘ 3