Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.ClassCastException when the parent resolver of a batch resolver returns a seq. #16

Closed
jmayaalv opened this issue Feb 3, 2021 · 2 comments

Comments

@jmayaalv
Copy link

jmayaalv commented Feb 3, 2021

If the parent of a batch resolver returns a seq, Pathom raises

   clojure.lang.PersistentList cannot be cast to
   clojure.lang.Associative

                   RT.java:  827  clojure.lang.RT/assoc
                  core.clj:  191  clojure.core/assoc
                  core.clj: 6169  clojure.core/assoc-in
                  core.clj: 6169  clojure.core/assoc-in
                  core.clj: 6169  clojure.core/assoc-in
                  core.clj: 6161  clojure.core/assoc-in
                 Atom.java:   65  clojure.lang.Atom/swap
                  core.clj: 2354  clojure.core/swap!
                  core.clj: 2345  clojure.core/swap!
          entity_tree.cljc:   45  com.wsscode.pathom3.entity_tree$swap_entity_BANG_$f46764__46797/invoke
          entity_tree.cljc:   32  com.wsscode.pathom3.entity_tree$swap_entity_BANG_/invokeStatic
          entity_tree.cljc:   32  com.wsscode.pathom3.entity_tree$swap_entity_BANG_/invoke
               runner.cljc:  631  com.wsscode.pathom3.connect.runner$run_batches_BANG_/invokeStatic

To reproduce:

(def db {:contracts {"contract-1" {::contract/id   1
                               ::contract/code "contract-1"
                               ::contract/name "the name"}}
         ;; if positions below, return a vec everything works as expected.
         :positions {1 '({::position/units  10
                         ::instrument/fund {::fund/id 1}}
                        {::position/units  20
                         ::instrument/fund {::fund/id 2}})}
         :funds     {1 {::fund/code "fund 1"}
                     2 {::fund/code "fund 2"}}})

(pco/defresolver contract-resolver [{::contract/keys [code]}]
  {::pco/output [::contract/id ::contract/code ::contract/name]}
  (get-in db [:contracts code]))

(pco/defresolver position-resolver [{::contract/keys [id]}]
  {::pco/output [{::contract/positions [::position/units {::instrument/fund [::fund/id]}]} ]}
  {::contract/positions (get-in db [:positions id])})

(pco/defresolver fund-resolver [input]
  {::pco/input [::fund/id]
   ::pco/output [::fund/code]
   ::pco/batch? true}
  (map #(get-in db [:funds (::fund/id %)])
       input))

(p.eql/process
  (pci/register [contract-resolver position-resolver fund-resolver])
  [{[::contract/code "contract-1"] [::contract/name {::contract/positions [::position/units {::instrument/fund [::fund/code]}]}]}])
@wilkerlucio
Copy link
Owner

Thanks for this report, I have made some changes around this behavior with batches.

Code for the changes is at 721d142

First I like to talk about the problem. The problem of seqs (or sets), is that they are data structures that don't support indexed update (can't assoc-in or update-in some specific element of the collection). This is a problem for batching, because batch runs kind of "outside" the entity, and then it has to be merged back in. This merge back in is the step in which I need the indexed update. Vectors can do this just fine, but seqs and sets can't.

The error on stack is related to the attempt of doing assoc-in on a seq (or set).

Now I change the behavior, and if you run this same again, you should see the correct response, and some warnings about batch disabled. Now, when I start processing something that's not batch compatible, I mark the env, and if you try to do batch in one location like this, it won't try to batch, it will use the batch resolver as a non batch one (send a list with one element, pick the first result). This way it will work as if you don't have batch, but works. And if this happens you get a warning message to let you know your batch is not happening.

I also updated the docs on batching to reflect this new knowledge, you can it at: https://pathom3.wsscode.com/docs/resolvers/#unsupported-batch

@jmayaalv
Copy link
Author

jmayaalv commented Feb 7, 2021

Thanks a lot for this Wilker. Very happy with your solution 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants