原标题:编写graphql服务
用一种高效清晰的规范来替代目前正流行的RESTful架构。通过灵活的结构化查询语言让查询更高效,静态类型和规范提升前后端联调效率。作为一名前端我非常喜欢graphql,梦想有一天以前所有零散又没有文档的接口都被graphql所代替,这将极大提升我的开发效率和开发体验。等待不如自己动手,下面将介绍如何编写一个基于的graphql服务。
在编写前先体验已经做好的
这个graphql服务和类似,提供了获取文章和用户信息的接口。在开发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相互依赖
由于type由filed构成,编写graphql服务的核心工作就是编写filed,如何获取数据和处理写操作的逻辑全来自于filed。
接下来将使用golang开始编写。
所有查询的入口(query): varRootQuery = graphql.NewObject(graphql.ObjectConfig{ Name: "RootQuery", Deion: `hackernews's API`, Fields: graphql.Fields{ "story": &graphql.Field{ Type: storyType, Deion: `get story item by story.id`, Args: graphql.FieldConfigArgument{ "id": &graphql.ArgumentConfig{ Type: graphql.NewNonNull(graphql.Int), Deion: "item's unique id", }, }, Resolve: func(p graphql.ResolveParams) ( interface{}, error) { ifid, ok := p.Args[ "id"].( int); ok { returnmodel.GetStory( int32(id)) } returnnil, nil}, }, "topstories": &graphql.Field{ Type: graphql.NewList(storyType), Deion: `Up to 500 top stories`, Resolve: func(p graphql.ResolveParams) ( interface{}, error) { ids := [] int32{} model.GetKV( "topstories", &ids) returnmodel.GetStories(ids), nil}, }, },})
以上代码暴露出了2个查询接口(也是2个字段)
story字段通过id获取一个story,该字段返回类型是storyType
topstories获取500个热门的story,该字段返回类型是[storyType]
可以看到获取数据的逻辑都在Resolve函数里,由于篇幅有限model 里封装的如何去数据库获取数据的代码忽略了。
storyType的代码 varstoryType = graphql.NewObject(graphql.ObjectConfig{ Name: "Story", Deion: `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", Deion: `this poll's comments`, Type: graphql.NewList(commentType), Resolve: func(p graphql.ResolveParams) ( interface{}, error) { poll := p.Source.(*model.Poll) returnmodel.GetComments(poll.Kids), nil}, }, },})
这段代码定义了要返回的storyType是什么样的以及storyType的每个字段的数据是怎么获取的。以storyType的comments字段为例Resolve: func里告诉了如何去获取这篇文章的所有评论。返回搜狐,查看更多
责任编辑: