• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

tendant/graphql-clj: A Clojure library that provides GraphQL implementation.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

tendant/graphql-clj

开源软件地址(OpenSource Url):

https://github.com/tendant/graphql-clj

开源编程语言(OpenSource Language):

Clojure 77.7%

开源软件介绍(OpenSource Introduction):

graphql-clj

A Clojure library designed to provide GraphQL implementation.

Build Status

Demo

Demo Project with GraphiQL

What's new in version 0.2

  1. Simplified APIs
  2. Rewrite schema and query validator for simplicity and robostness.
  3. Separate parser and validator for schema and query.
  4. High performance java parser

Installation

Add the following dependency to your project.clj file:

[graphql-clj "0.2.9"]

Usage

Define schema

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

  schema {
    query: QueryRoot
  }")

Define resolver functions

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

Execute query

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

    (executor/execute nil schema-str resolver-fn query-str)
    ;; => {:data {"user" {"name" "test user name", "age" 30}}}

Caching validated schema and query for performance

    (require '[graphql-clj.schema-validator :as schema-validator])
    (require '[graphql-clj.query-validator :as query-validator])
    
    ;; Consider memoizing the result of parsing and validating the query before execution
    (def validated-schema (schema-validator/validate-schema schema-str)) ; throw ex-info with ex-data {:errors errors}
    (def validated-query (query-validator/validate-query validated-schema query-str)) ; return [errors validated-ast]

    (executor/execute nil validated-schema resolver-fn validated-query)
    ;; => {:data {"user" {"name" "test user name", "age" 30}}}

Migrating from 0.1.x to 0.2 version

  1. Separated parser api for schema and query
   parser/parse-schema for schema parsing
   parser/parse-query-document for query parsing
  1. Simplified validator api, it can take query string and schema string now.
   graphql-clj.schema-validator/validate-schema replaces validator/validate-schema
   graphql-clj.query-validator/validate-query replaces validator/validate-statement
  1. executor/execute function can take string and validated result for both schema and query string.

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap