Skip to content

Instantly share code, notes, and snippets.

@ampersanda
Last active June 22, 2022 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ampersanda/b0dd66e9f0c7f5d84cccd51a55d4a15e to your computer and use it in GitHub Desktop.
Save ampersanda/b0dd66e9f0c7f5d84cccd51a55d4a15e to your computer and use it in GitHub Desktop.
Initializing GraphQL instance on ClojureDart
(ns project.core.graphql
(:require ["package:graphql/client.dart" :as g]
[project.env :refer [env]]))
(def ^:private http-link (g/HttpLink (:base-url env)))
(def ^:private auth-link
(g/AuthLink :getToken (fn ^:async [])
:headerKey "Authorization"))
(def client
(g/GraphQLClient :cache (g/GraphQLCache)
:link (.concat auth-link http-link)))
(ns project.core.services
(:require ["package:graphql/client.dart" :refer [QueryOptions gql] :as g]
[project.core.graphql :refer [client]]
[cljd.walk :refer [keywordize-keys]]))
(defn- keywordize-blunt
"Make keys of map turn to keyword"
[entry]
(cond
(dart/is? entry Map)
(loop [result {}
entries (seq entry)]
(let [[k v] (first entries)]
(if k
(recur
(assoc result
(keyword k)
(keywordize-blunt v))
(rest entries))
result)))
(dart/is? entry List)
(map (fn [e] (keywordize-blunt e)) entry)
:else entry))
;; Examples from https://github.com/Liverm0r/ClojureDartTeaExample/blob/main/src/tea/api.cljd
(def ^{:private true :const true} random-message-q
"
query organizationLoyaltyMessageRandomMessage {
organizationLoyaltyMessageRandomMessage {
pictureUrl
message
}
}
")
(defn ^:async random-message! []
(let [opts (QueryOptions :document (gql random-message-q))
result (-> client (.query opts) await)]
(if (.-hasException result)
{:error (.exception! result)}
(-> result
.-data
keywordize-blunt
:organizationLoyaltyMessageRandomMessage
keywordize-keys))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment