一次查询,搞定需求
举个例子,Book对象有bookTypeId,那我想看对应的bookTypeName,bookType对应的summary咋办?
如果你用RESTful Api ,免不了要定制接口或者多次调用后端。
现在用上GraphQL,就是这么无情:
#查询语句
query add{
book(bookId: 1){
bookName
author
bookType{
typeId
typeName
summary
}
wordNumbers
cover
clickTimes
lastupdate
createtime
}
}
#结果
{
"data": {
"book": {
"bookName": "斗战狂潮",
"author": "骷髅精灵",
"bookType": {
"typeId": "48",
"typeName": "修真文明",
"summary": "修仙觅长生,热血任逍遥,踏莲曳波涤剑骨,凭虚御风塑圣魂!"
},
"wordNumbers": 2547900,
"cover": "//qidian.qpic.cn/qdbimg/349573/1003694333/180",
"clickTimes": 3,
"lastupdate": "2018-02-23T00:00:00",
"createtime": "2018-02-23T11:33:57"
}
}
}
sql语句?各种定制化接口?不存在的,一个GraphQL查询搞定,美滋滋。
继续来一发,排行榜表和作品表,排行榜表记录了book_id,前端展示只需这样:
query rank {
rankList(rankTypeId: 1,first: 2) {
edges {
node {
bookId
book {
bookName
author
wordNumbers
}
order
rankTypeId
createtime
}
}
}
}
**自动根据节点id查询对应node信息,多帅哦**
{
"data": {
"rankList": {
"edges": [
{
"node": {
"bookId": 1,
"book": {
"bookName": "斗战狂潮",
"author": "骷髅精灵",
"wordNumbers": 2547900
},
"order": 1,
"rankTypeId": 1,
"createtime": "2018-02-24T00:28:37"
}
},
{
"node": {
"bookId": 2,
"book": {
"bookName": "就问你气不气",
"author": "秋刀斩鱼",
"wordNumbers": 1736500
},
"order": 2,
"rankTypeId": 1,
"createtime": "2018-02-24T00:32:23"
}
}
]
}
}
}
这么写下来,GraphQL果然还是很不错的。。
上个链接GraphQL