Liking cljdoc? Tell your friends :D

graphql-clj

A Clojure library designed to provide GraphQL implementation.

Build Status

Demo

Demo Project with GraphiQL

Project Status

This library is in the early stage. It is still under active development. All functions are subject to change. Please file an issue or submit a PR if you have suggestions!

The implementation of the library follow closely to the GraphQL Draft RFC Specification (https://facebook.github.io/graphql/).

Installation

Add the following dependency to your project.clj file:

[graphql-clj "0.1.20"]

This library uses clojure.spec for validation. If you are not yet using clojure 1.9:

:dependencies [[org.clojure/clojure "1.8.0"]
               [graphql-clj "0.1.20" :exclusions [org.clojure/clojure]]
               [clojure-future-spec "1.9.0-alpha14"]]

Usage

Define schema

(require '[graphql-clj.parser :as parser])
(require '[graphql-clj.type :as type])
(require '[graphql-clj.validator :as validator])

(def schema-str "type User {
    name: String
    age: Int
  }
  type QueryRoot {
    user: User
  }

  schema {
    query: QueryRoot
  }")

(def type-schema (-> schema-str parser/parse validator/validate-schema))

Define resolver functions

    (defn resolver-fn [type-name field-name]
      (cond
        (and (= "QueryRoot" type-name) (= "user" field-name)) (fn [context parent args]
                                                                {:name "test user name"
                                                                 :age 30})))

Execute query

    (require '[graphql-clj.executor :as executor])
    (def query-str "query {user {name age}}")
    (def context nil)

    ;; Consider memoizing the result of parsing and validating the query before execution
    (def query (-> query-str parser/parse (validator/validate-statement type-schema)))
    (executor/execute context type-schema resolver-fn query)
    ;; => {:data {"user" {"name" "test user name", "age" 30}}}

    ;; Alternatively, you can still pass the query string (slower, for backward compatibility)
    (executor/execute context type-schema resolver-fn query-str)

Deploy to local for development

$ lein install

Release to Clojars

$ lein deploy clojars

Test

$ lein test

License

Copyright © 2016 Lei Wang

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Can you improve this documentation? These fine people already did:
Lei Wang, Edward Wible & Feifan Zhou
Edit on GitHub

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

× close