Golang Gin GraphQL 搭建api框架
Golang
Go 是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。为了方便搜索和识别,有时会将其称为Golang。
GraphQL
GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。
Gin
Gin is a HTTP web framework written in Go (Golang).
为什么要用GraphQL?
为了让api具有更强的适应性,采用graphql来编写查询接口是个不错的选择。现在的api需要适应的场景太多了,而且迭代节奏也很快,RESTful的查询接口在一些复杂的场景下显得特别的繁杂,如多重嵌套的资源。
为什么要用Go?
在 Go 语言出现之前,开发者们总是面临非常艰难的抉择,究竟是使用执行速度快但是编译速度并不理想的语言(如:C++),还是使用编译速度较快但执行效率不佳的语言(如:.NET、Java),或者说开发难度较低但执行速度一般的动态语言呢?显然,Go 语言在这 3 个条件之间做到了最佳的平衡:快速编译,高效执行,易于开发。
为什么要用Gin?
因为Gin够简洁,适合定制合适自己风格的框架。
在这里主要记录集成GraphQL的部分,对于Gin的目录结构组织因人而异,就不做详细介绍。
在Go里面集成graphql需要用到graphql-go这个package,当然可以参考GraphQL文档提供的其他。
// 从schema开始
// schema/schema.go
package schema
// 引入包graphql-go
import (
"github.com/graphql-go/graphql"
)
// 定义跟查询节点
var rootQuery = graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Description: "Root Query",
Fields: graphql.Fields{
"hello": &queryHello, // queryHello 参考schema/hello.go
},
})
// 定义Schema用于http handler处理
var Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
Mutation: nil, // 需要通过GraphQL更新数据,可以定义Mutation
})
// schema/hello.go
package schema
import (
"golesson/model"
"github.com/graphql-go/graphql"
)
// 定义查询对象的字段,支持嵌套
var helloType = graphql.NewObject(graphql.ObjectConfig{
Name: "Hello",
Description: "Hello Model",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.Int,
},
"name": &graphql.Field{
Type: graphql.String,
},
},
})
// 处理查询请求
var queryHello = graphql.Field{
Name: "QueryHello",
Description: "Query Hello",
Type: graphql.NewList(helloType),
// Args是定义在GraphQL查询中支持的查询字段,
// 可自行随意定义,如加上limit,start这类
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"name": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
// Resolve是一个处理请求的函数,具体处理逻辑可在此进行
Resolve: func(p graphql.ResolveParams) (result interface{}, err error) {
// Args里面定义的字段在p.Args里面,对应的取出来
// 因为是interface{}的值,需要类型转换,可参考类型断言(type assertion): https://zengweigang.gitbooks.io/core-go/content/eBook/11.3.html
id, _ := p.Args["id"].(int)
name, _ := p.Args["name"].(string)
// 调用Hello这个model里面的Query方法查询数据
return (&model.Hello{}).Query(id, name)
},
}
// 准备好了GraphQL在Go里面需要的东西之后,来看看如何跟Gin结合
// controller/graphql/graphql.go
package graphql
import (
"golesson/schema"
"github.com/gin-gonic/gin"
"github.com/graphql-go/handler"
)
func GraphqlHandler() gin.HandlerFunc{
h := handler.New(&handler.Config{
Schema: &schema.Schema,
Pretty: true,
GraphiQL: true,
})
// 只需要通过Gin简单封装即可
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
// route/router.go
package router
import (
"golesson/controller/graphql"
"github.com/gin-gonic/gin"
)
var Router *gin.Engine
func init() {
Router = gin.Default()
}
func SetRouter() {
// GET方法用于支持GraphQL的web界面操作
// 如果不需要web界面,可根据自己需求用GET/POST或者其他都可以
Router.POST("/graphql", graphql.GraphqlHandler())
Router.GET("/graphql", graphql.GraphqlHandler())
}
// main.go
package main
import "golesson/route"
func main () {
r := router.Router
router.SetRouter()
r.Run(":1234")
}
运行之后的web界面
Github:
编辑于 2019-01-21 00:05