taoensso / carmine

Redis client + message queue for Clojure
https://www.taoensso.com/carmine
Eclipse Public License 1.0
1.15k stars 130 forks source link

How to use pub/sub-feature for subscriptions in lacinia (graphql)? #248

Closed hivesy closed 3 years ago

hivesy commented 3 years ago

We're using lacinia for the subscriptions, and currently, we're using an atom for updating the changes where the streamer keeps an eye on it and streams the changes whenever the atom is updated. Now to how to replace the atom with pub/sub feature?

defonce mid (atom nil))

(defn update-app-data
  [_ _ {:keys [id bio]} _]
  (update-data  id {:bio bio})
  (reset! mid[:bio bio]) ;;updates the atom with bio
 )

(defn app-streamer
  [_ _ {:keys [id title]} source-stream]
  (let [generated-key (gensym "app streamer")]
    (add-watch mid generated-key ;;checks the atom for changes in title
      (fn [_ _ _ [a bio :as new-state]]
        (when (= :bio a)
                   (source-stream (get-app-data id)))
    (fn [] (remove-watch mid generated-key))))))