A Clojure library that provides GraphQL implementation.
Clojure
Latest commit 104dc43 Nov 21, 2016 @tendant committed on GitHub Merge pull request #45 from nubank/simplify-base-types
Simplify process of establishing base type for fields, arguments, and variables

README.md

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.