编写graphql服务
- 2019 年 12 月 4 日
- 笔记
graphql用一种高效清晰的规范来替代目前正流行的RESTful架构。通过灵活的结构化查询语言让查询更高效,静态类型和规范提升前后端联调效率。作为一名前端我非常喜欢graphql,梦想有一天以前所有零散又没有文档的接口都被graphql所代替,这将极大提升我的开发效率和开发体验。等待不如自己动手,下面将介绍如何编写一个基于hackernews API的graphql服务。
在编写前先体验已经做好的graphql服务demo
这个graphql服务和hackernews官方API类似,提供了获取文章和用户信息的接口。 在开发graphql服务前你可能需要知道graphql提供了哪些能力,以及graphql定义了哪些规范,这些都可以在这里学习。
graphql服务核心概念
- 类型(type):type是最基础的因为graphql服务返回数据一定是定义好的类型的单个或者数组。类型就像面对对象编程里的class,返回数据是class的实例
- 字段(filed):filed组成type,一个type由多个filed构成。filed就像面对对象编程里的class的一个属性。每个字段都有返回类型,返回类型是定义的type
- 查询(query):通过query来暴露graphql服务所提供的所有读接口,query就是type它由filed组成
- 变化(mutation):通过mutation来暴露graphql服务所提供的写接口,mutation就是type它由filed组成
graphql服务由type和filed构成,type和filed相互依赖
![](https://ask.qcloudimg.com/http-save/6430183/0sya8exp71.png)
由于type由filed构成,编写graphql服务的核心工作就是编写filed,如何获取数据和处理写操作的逻辑全来自于filed。
接下来将使用golang开始编写。
所有查询的入口(query):
var RootQuery = graphql.NewObject(graphql.ObjectConfig{ Name: "RootQuery", Description: `hackernews's API`, Fields: graphql.Fields{ "story": &graphql.Field{ Type: storyType, Description: `get story item by story.id`, Args: graphql.FieldConfigArgument{ "id": &graphql.ArgumentConfig{ Type: graphql.NewNonNull(graphql.Int), Description: "item's unique id", }, }, Resolve: func(p graphql.ResolveParams) (interface{}, error) { if id, ok := p.Args["id"].(int); ok { return model.GetStory(int32(id)) } return nil, nil }, }, "topstories": &graphql.Field{ Type: graphql.NewList(storyType), Description: `Up to 500 top stories`, Resolve: func(p graphql.ResolveParams) (interface{}, error) { ids := []int32{} model.GetKV("topstories", &ids) return model.GetStories(ids), nil }, }, }, })
以上代码暴露出了2个查询接口(也是2个字段)
story
字段通过id获取一个story,该字段返回类型是storyType
topstories
获取500个热门的story,该字段返回类型是[storyType]
可以看到获取数据的逻辑都在Resolve函数里,由于篇幅有限model 里封装的如何去数据库获取数据的代码忽略了。
storyType
的代码
var storyType = graphql.NewObject(graphql.ObjectConfig{ Name: "Story", Description: `Stories, They're identified by their ids, which are unique integers. All items have some of the following properties. `, Fields: graphql.Fields{ "id": idField, "by": byField, "time": timeField, "deleted": deletedField, "dead": deadField, "text": textField, "score": scoreField, "url": urlField, "title": titleField, "descendants": descendantsField, "doc": docField, "comments": &graphql.Field{ Name: "comments", Description: `this poll's comments`, Type: graphql.NewList(commentType), Resolve: func(p graphql.ResolveParams) (interface{}, error) { poll := p.Source.(*model.Poll) return model.GetComments(poll.Kids), nil }, }, }, })
这段代码定义了要返回的storyType
是什么样的以及storyType
的每个字段的数据是怎么获取的。以storyType
的comments
字段为例Resolve: func
里告诉了如何去获取这篇文章的所有评论。