Liking cljdoc? Tell your friends :D

district-graphql-utils

Build Status

Set of helper functions for working with GraphQL, mostly related to clj->js and js->clj transformations.

Installation

Add
Clojars Project
into your project.clj

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

API Overview

district.graphql-utils

kw->gql-name [kw]

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_"

gql-name->kw [gql-name]

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?

clj->js-root-value [root-value]

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

js->clj-objects [res]

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}}

js->clj-response [res]

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}}

add-fields-to-schema-types [schema-ast fields]

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    

gql-date->date [gql-date]

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    

Development

lein deps
# To run tests and rerun on changes
lein doo chrome tests

Can you improve this documentation? These fine people already did:
madvas, ginesdt & fbielejec
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close