Last active
June 22, 2022 17:57
-
-
Save ampersanda/b0dd66e9f0c7f5d84cccd51a55d4a15e to your computer and use it in GitHub Desktop.
Initializing GraphQL instance on ClojureDart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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