/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 an alpha version. It is still under active development. All functions are subject to change.

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

  • GraphQL parser
    • Query
    • Mutation
    • Type System
    • Variables
  • Transformation
  • Execution
    • Query
    • Mutation
    • Support List Type
    • Support Non-Null Type
    • Arguments
    • Variables
      • List type variable
    • Union
    • Interface
    • Safe parallel execution
    • Coerce
  • Fragment execution
    • Fragment Type
  • Support Context
  • Type Introspect, IN PROGRESS
  • Directives
  • Testing
    • Use clojure.spec to generate test resolver automatically
  • Schema validation
  • Query validation
  • Arguments validation
    • Argument Coerce
  • Variables validation
    • Variable Coerce
  • Parser error handling
  • Execution error handling
  • Batch data loading
  • Comment as meta data

Installation

Add the following dependency to your project.clj file:

[graphql-clj "0.1.12"]

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.