wilkerlucio / pathom

Pathom is a Clojure(script) engine for processing EQL requests.
https://wilkerlucio.github.io/pathom/v2
MIT License
621 stars 62 forks source link

Provide a `::pc/mutation-data` in mutation context #148

Open souenzzo opened 4 years ago

souenzzo commented 4 years ago

In resolver context, pathom provide a pc/resolver-data that contains all "metadata" from resolver

This feature isn't present in mutation.

(let [register [(pc/mutation `mutation
                             {::pc/output [::mutation]}
                             (fn [env params]
                               {::mutation (-> env ::pc/mutation-data)}))
                (pc/resolver `resolver
                             {::pc/output [::resolver]}
                             (fn [env params]
                               {::resolver (-> env ::pc/resolver-data)}))]
      parser (p/parser {::p/plugins [(pc/connect-plugin {::pc/register register})]
                        ::p/mutate  pc/mutate})
      env {::p/reader [p/map-reader
                       pc/reader2
                       pc/open-ident-reader]}]
  (parser env
          `[{(mutation {})
             [::resolver
              ::mutation]}]))
=>
{user/mutation {:user/resolver {:com.wsscode.pathom.connect/sym user/resolver,
                                :com.wsscode.pathom.connect/input #{},
                                :com.wsscode.pathom.connect/resolve #object[user$eval21712$fn__21715
                                                                            0x700e1383
                                                                            "user$eval21712$fn__21715@700e1383"],
                                :com.wsscode.pathom.connect/output [:user/resolver]},
               ;; expected: {::pc/sym user/mutation ....}
               ;; actual:
                :user/mutation nil}}
souenzzo commented 4 years ago

in here https://github.com/wilkerlucio/pathom/blob/master/src/com/wsscode/pathom/connect.cljc#L1508 and https://github.com/wilkerlucio/pathom/blob/master/src/com/wsscode/pathom/connect.cljc#L1530

  (if-let [{::keys [sym] :as mutation-data} (get-in indexes [::index-mutations sym'])]
    (let [env (-> env 
                 (assoc-in [:ast :key] sym)
                 (assoc ::pc/mutation-data mutation-data))]

Consider mutation-data helper function (line 124 ATM)

test

(is (every? number? (for [[mutate parser-gen] [[pc/mutate p/parser]
                                               [pc/mutate-async p/parallel-parser]]]
                      (let [register [(pc/mutation `mutation
                                                   {::my-var 42}
                                                   (fn [env params]
                                                     {::mutation (-> env ::pc/mutation-data))}))]
                            parser (parser-gen {::p/plugins [(pc/connect-plugin {::pc/register register})]
                                                ::p/mutate  mutate})]
                        (-> (parser {} `[(mutation)])
                            (wasync/<!!maybe)
                            (get-in [`mutation ::mutation ::my-var]))))))