/graphql-clj

A Clojure library designed to provide GraphQL implementation.

Primary LanguageClojureEclipse Public License 1.0EPL-1.0

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.16"]

Usage

Define schema

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

(def parsed-schema (parser/parse "type User {
    name: String
    age: Int
  }
  type QueryRoot {
    user: User
  }

  schema {
    query: QueryRoot
  }"))

(def type-schema (type/create-schema parsed-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 "query {user {name age}}")
    (def context nil)
    
    (executor/execute context type-schema resolver-fn query)

    ;; {:data {"user" {"name" "test user name", "age" 30}}}

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.