Skip to content
/ re-graph Public
forked from oliyh/re-graph

A graphql client for re-frame and clojurescript applications

Notifications You must be signed in to change notification settings

r0man/re-graph

 
 

Repository files navigation

re-graph

re-graph is a graphql client for re-frame applications.

Notes

This library behaves like the popular Apollo client for graphql and as such is compatible with lacinia-pedestal.

Features include:

  • Subscriptions and queries
  • Websocket and HTTP transports
  • Apollo-compatible servers like lacinia-pedestal
  • Queueing websocket messages until ready
  • Websocket reconnection on disconnect

Usage

Add re-graph to your project's dependencies:

Clojars Project

Dispatch the init event to bootstrap it and then use the :subscribe, :unsubscribe and :query events:

(require [re-graph.core :as re-graph]
         [re-frame.core :as re-frame])

;; initialise re-graph, possibly including configuration options (see below)
(re-frame/dispatch [::re-graph/init {}])

(re-frame/reg-event-db
  ::on-thing
  (fn [db [_ payload]]
    ;; do things with payload e.g. write it into the re-frame database
    ))

;; start a subscription, with responses sent to the callback event provided
(re-frame/dispatch [::re-graph/subscribe
                    :my-subscription-id  ;; this id should uniquely identify this subscription
                    "{ things { id } }"  ;; your graphql query
                    {:some "variable"}   ;; arguments map
                    [::on-thing]])       ;; callback event when messages are recieved

;; stop the subscription
(re-frame/dispatch [::re-graph/unsubscribe :my-subscription-id])

;; perform a query, with the response sent to the callback event provided
(re-frame/dispatch [::re-graph/query
                    "{ things { id } }"  ;; your graphql query
                    {:some "variable"}   ;; arguments map
                    [::on-thing]])       ;; callback event when response is recieved

Options

Options can be passed to the init event, with the following possibilities:

(re-frame/dispatch
  [::re-graph/init
    {:ws-url               "wss://foo.io/graphql-ws" ;; override the websocket url (defaults to /graphql-ws, nil to disable)
     :http-url             "http://bar.io/graphql"   ;; override the http url (defaults to /graphql)
     :ws-reconnect-timeout 2000                      ;; attempt reconnect n milliseconds after disconnect (default 5000, nil to disable)
  }])

Development

user> (dev)
dev> (start)
;; visit http://localhost:3449/devcards/index.html
dev> (cljs)
cljs.user>

CircleCI

License

Copyright © 2017 oliyh

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

About

A graphql client for re-frame and clojurescript applications

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Clojure 98.5%
  • HTML 1.5%