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

walmartlabs/lacinia-pedestal: Expose Lacinia GraphQL as Pedestal endpoints

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

开源软件名称(OpenSource Name):

walmartlabs/lacinia-pedestal

开源软件地址(OpenSource Url):

https://github.com/walmartlabs/lacinia-pedestal

开源编程语言(OpenSource Language):

Clojure 97.0%

开源软件介绍(OpenSource Introduction):

com.walmartlabs/lacinia-pedestal

Clojars Project

CircleCI

A library that adds the Pedestal underpinnings needed when exposing Lacinia as an HTTP endpoint.

Lacinia-Pedestal also supports GraphQL subscriptions, using the same protocol as Apollo GraphQL.

Lacinia-Pedestal Manual | API Documentation

Usage

For a basic Pedestal server, simply supply a compiled Lacinia schema to the com.walmartlabs.lacinia.pedestal2/default-service function to generate a service, then invoke io.pedestal.http/create-server and /start.

;; This example is based off of the code generated from the template
;;  `lein new pedestal-service graphql-demo`

(ns graphql-demo.server
  (:require [io.pedestal.http :as http]
            [com.walmartlabs.lacinia.pedestal2 :as lp]
            [com.walmartlabs.lacinia.schema :as schema]))

(def hello-schema 
  (schema/compile
    {:queries 
      {:hello
        ;; String is quoted here; in EDN the quotation is not required 
        ;; You could also use :String
        {:type 'String
         :resolve (constantly "world")}}}))

;; Use default options:
(def service (lp/default-service hello-schema nil))

;; This is an adapted service map, that can be started and stopped.
;; From the REPL you can call http/start and http/stop on this service:
(defonce runnable-service (http/create-server service))

(defn -main
  "The entry-point for 'lein run'"
  [& args]
  (println "\nCreating your server...")
  (http/start runnable-service))

Lacinia will handle POST requests at the /api endpoint:

$ curl localhost:8888/api -X POST -H "content-type: application/json" -d '{"query": "{ hello }"}'
{"data":{"hello":"world"}}

You can also access the GraphQL IDE at http://localhost:8888/ide.

Development Mode

When developing an application, it is desirable to be able to change the schema without restarting. Lacinia-Pedestal supports this: in the above example, the schema passed to default-service could be a function that returns the compiled schema. It could even be a Var containing the function that returns the compiled schema.

In this way, the Pedestal stack continues to run, but each request rebuilds the compiled schema based on the latest code you've loaded into the REPL.

Beyond default-server

default-server is intentionally limited, and exists only to help you get started. Once you start adding anything more complicated, such as authentication, or supporting multiple schemas (or schema versions) at different paths, you will want to simply create your routes and servers in your own code, using the building-blocks provided by com.walmartlabs.lacinia.pedestal2.

GraphiQL

The GraphiQL packaged inside the library is built using npm, from version 1.4.7.

If you are including lacinia-pedestal via Git coordinate (rather than a published version of the library by using a :mvn/version coordinate), then the library will need to be prepped for use via clj -X:deps prep.

The prep action for lacinia-pedestal requires that you have npm installed.
The prep action generates the CSS and JavaScript files that are used to execute GraphiQL.

License

Copyright © 2017-2022 Walmart

Distributed under the Apache Software License 2.0.

GraphiQL has its own license.




鲜花

握手

雷人

路过

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

请发表评论

全部评论

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

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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