Set of helper functions for working with GraphQL, mostly related to clj->js and js->clj transformations.
For browser usage also add [cljsjs/graphql "0.13.1-0"]
(or latest version) into your project.clj
Include [district.graphql-utils]
in your CLJS file
For browser usage also add [cljsjs.graphql]
in your CLJS file
Converts (namespaced) keyword into GraphQL compatible name (no dots, no slashes, no dashes).
Note, this is opinionated way how to convert namespaced keyword into string.
(graphql-utils/kw->gql-name :profile-picture/image-height)
;; => "profilePicture_imageHeight"
(graphql-utils/kw->gql-name :user.profile-picture/image-height)
;; => "user_profilePicture_imageHeight"
(graphql-utils/kw->gql-name :user/active?)
;; => "user_active_"
Converts GraphQL compatible name into keyword.
(graphql-utils/gql-name->kw "profilePicture_imageHeight")
;; => :profile-picture/image-height
(graphql-utils/gql-name->kw "user_profilePicture_imageHeight")
;; => :user.profile-picture/image-height
(graphql-utils/gql-name->kw "user_active_")
;; => :user/active?
Converts root value data structure into proper JS format, so it can be passed into a graphql-js library. It also converts field arguments passed to each resolver function into clj.
Optionally as a seconds arg you can pass map with :gql-name->kw
& :kw->gql-name
for custom name conversion functions.
(def root-value (graphql-utils/clj->js-root-value {:a (fn [] {:b 1})}))
(aget ((aget root-value "a")) "b")
;; => 1
Converts GraphQL request or response object into clj data structures, without keyword naming conversion.
(graphql-utils/js->clj-objects (clj->js {"data" {"profilePicture_imageHeight" 100}}))
;; => {:data {:profilePicture_imageHeight 100}}
Converts GraphQL response into clj data structures, since GraphQL returns each object in a tree as an instance of a class.
Optionally as a seconds arg you can pass map with :gql-name->kw
for custom name conversion function.
(graphql-utils/js->clj-response (clj->js {"data" {"profilePicture_imageHeight" 100}}))
;; => {:data {:profile-picture/image-height 100}}
Will add given fields to user defined types in schema AST.
(let [schema-ast (js/GraphQL.buildSchema "type User {name: String}")]
(graphql-utils/add-fields-to-schema-types schema-ast [{:type js/GraphQL.GraphQLID
:name "userId"
:args []}])
(object? (aget (.getFields (aget (.getTypeMap schema-ast) "User")) "userId")))
;; => true
Parse GraphQL Date type as CLJS DateTime object ready to be formatted
(cljs-time.core/equal? (cljs-time.core/date-time 2018 05 05) (graphql-utils/gql-date->date 1525478400))
;; => true
lein deps
# To run tests and rerun on changes
lein doo chrome tests