Fork me on GitHub
#graphql
<
2019-05-12
>
Bravi09:05:14

hi everyone. does anyone know how to dispatch mutations in re-graph?

gklijs10:05:46

I have no mutations in my example, but should be about the same as query or subscription.

(re-frame/reg-event-fx
 ::mutate
 interceptors
 (fn [{:keys [db dispatchable-event]} [query variables callback-event :as event]]
   (let [query (str "mutation " (string/replace query #"^mutation\s?" ""))]
     (cond
       (get-in db [:websocket :ready?])
       (let [query-id (internals/generate-query-id)]
         {:db (assoc-in db [:subscriptions query-id] {:callback callback-event})
          ::internals/send-ws [(get-in db [:websocket :connection])
                               {:id query-id
                                :type "start"
                                :payload {:query query
                                          :variables variables}}]})

       (:websocket db)
       {:db (update-in db [:websocket :queue] conj dispatchable-event)}

       :else
       {::internals/send-http [(:http-url db)
                               {:request (:http-parameters db)
                                :payload {:query query
                                          :variables variables}}
                               (fn [payload]
                                 (re-frame/dispatch (conj callback-event payload)))]}))))

Bravi10:05:20

thank you, that’s helpful!