Skip to content

Latest commit

 

History

History
158 lines (93 loc) · 3.35 KB

README_zh.md

File metadata and controls

158 lines (93 loc) · 3.35 KB

GQLEngine 一个高性能Go语言的GraphQL服务端落地框架

Demo

特性

  • 基本特性
    • Object类型支持
    • Interface类型支持
    • Union类型支持
    • Enum类型支持
    • 自定义Scalar类型支持
    • Input Object类型支持
    • 字段入参支持
  • 订阅功能(集成了高性能websocket,支持百万级连接)
  • 文件上传
  • 自定义ID类型
  • 链路跟踪分析
  • 标签化文档支持
  • 插件支持

关注我

image-20200211135929078

立马体验

通过go get

go get -u github.com/gqlengine/gqlengine

编写main.go

package main

import (
  "net/http"

  "github.com/gqlengine/gqlengine"
)

// MyInfo 定义了业务的数据结构
type MyInfo struct {
  gqlengine.IsGraphQLObject `gqlDesc:"my info"` // gqlDesc用于生成描述信息
  
  MyStringField string // 定义一个字段,gqlengine会根据golang的基本类型自动匹配到graphql类型 
  MyIntField 	  int `gqlRequired:"true"`  // gqlRequired用于标记该字段是必备非空字段
}

// MySimpleQuery 是业务的代码的具体实现
func MySimpleQuery() (MyInfo, error) {
  panic("not implemented")
}

func main() {
  engine := gqlengine.NewEngine(gqlengine.Options{
	    Tracing: true, // 使能GraphQL调用链路分析功能
  })
  
  engine.NewQuery(MySimpleQuery) // 务必注册你的接口!!!
  
  // 初始化engine
  if err := engine.Init(); err != nil {
    panic(err)
  }
  
  // serve for HTTP
  http.HandleFunc("/api/graphql", engine.ServeHTTP)
  if err := http.ListenAndServe(":8000", nil); err != nil {
    panic(err)
  }
}

以上是最基本的配置,go run main.go运行之后,就可以在https://localhost:8000/api/graphql 获得graphql api了。

如果你想要类似于swagger的API查看和调试工具,我们不仅有,还提供更好的GraphQL Playground功能,仅需添加另外一个包:

go get -u github.com/gqlengine/playground

然后再main()中添加以下部分代码:

...

import (
  "github.com/gorilla/mux"
  "github.com/gqlengine/playground"
)

...

func main() {
  
  ... // init your gql engine
  
  // 为playground配置GraphQL端点(起码让playground知道该去哪里获得graphql数据吧;))
  playground.SetEndpoints("/api/graphql", "/api/graphql/subscriptions")
  
  // recommends to use 'gorilla/mux' to serve the playground web assets
  r := mux.NewRouter()
  r.HandleFunc("/api/graphql", engine.ServeHTTP)
  r.HandleFunc("/api/graphql/subscriptions", engine.ServeWebsocket)
  r.PathPrefix("/api/graphql/playground").
    Handler(http.StripPrefix("/api/graphql/playground",
      http.FileServer(playground.WebBundle)))

  println("open playground https://localhost:9996/api/graphql/playground/")
  if err := http.ListenAndServe(":9996", r); err != nil {
    panic(err)
  }
}

打开浏览器:https://localhost:9996/api/graphql/playground

打赏

如果您觉得我们的开源软件对你有所帮助,请拿出手机扫下方二维码打赏我们一杯咖啡。

image-20200211142656556