Skip to content

district0x/district-graphql-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

Development

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

About

Set of functions helpful for working with GraphQL

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •