前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >编写graphql服务

编写graphql服务

作者头像
IMWeb前端团队
发布2017-12-29 18:23:56
9910
发布2017-12-29 18:23:56
举报

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相互依赖

由于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的每个字段的数据是怎么获取的。以storyTypecomments字段为例Resolve: func里告诉了如何去获取这篇文章的所有评论。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • graphql服务核心概念
  • graphql服务由type和filed构成,type和filed相互依赖
    • 所有查询的入口(query):
      • storyType的代码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档