From ee1fd12e17ed63f7613b3f93fcf101b05dc02082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Wed, 6 Dec 2023 13:22:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 165 ++++++++++++++++++++++++++++++++++++++++++++++------ bin/main.go | 2 +- 2 files changed, 148 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e20217c..6946dbf 100644 --- a/README.md +++ b/README.md @@ -8,28 +8,157 @@ - [ ] 列表翻页 -GraphQL +GraphQL 基本规则 +- 必须指定要求返回的每个字段, 不指定的字段不会被返回, 用于减少无效查询 +- 通过 after before 作为游标翻页, 返回指定id之前或之后的列表, 而不是使用 pageNum, 作用是防止列表变化导致翻页请求结果的部分重复 +- 通过 first last 替代 pageSize 决定选择游标前n个还是游标后的n个列表 + ```javascript -const query = ` -query ($id: Int!) { - article(id: $id) { - id - title - content - author { - id - name - } - } +// 假设服务器上的数据 +const database = { + users: [ + { id: 1, user_name:'xx1', avatar:'xx1.png', create_time:'2022-01-02 12:12:12' } + { id: 2, user_name:'xx2', avatar:'xx2.png', create_time:'2022-01-02 12:12:12' } + { id: 3, user_name:'xx3', avatar:'xx3.png', create_time:'2022-01-02 12:12:12' } + { id: 4, user_name:'xx4', avatar:'xx4.png', create_time:'2022-01-02 12:12:12' } + ], + images: [ + { id: 1, title:'xx1', content:'xx1.png', user_id: 1, article_id: 2 } + { id: 2, title:'xx2', content:'xx2.png', user_id: 1, article_id: 2 } + { id: 3, title:'xx3', content:'xx3.png', user_id: 2, article_id: 4 } + { id: 4, title:'xx4', content:'xx4.png', user_id: 2, article_id: 4 } + ] } -` - - - - ``` +```javascript +// GET 查询id为1的用户列表, 要求列表中每项只返回 id, 用户名, 头像, 以及符合筛选条件的总数 total +const query = `users(id:1){total,list{id,user_name,avatar}}` +fetch('/api?'+query).then(res => res.json()).then(data => { + console.log(data) + // { data: { users: { total:1, list:[{ id: 1, user_name:'xx1', avatar:'xx1.png' }] } } } +}) + +// GET 查询 user_id 为 2 的图像列表, 并且包含 user 的部分信息, 以及符合筛选条件的总数 total +const query = `images(user_id:2){total,list{id,content,user:{id,username,avatar}}}` +fetch('/api?'+query).then(res => res.json()).then(data => { + console.log(data) + /* + { + data: { + images: { + total:2, + list:[ + {id:3,content:'xx3.png',user:{id:2,user_name:'xx2',avatar:'xx2.png'}}, + {id:4,content:'xx4.png',user:{id:2,user_name:'xx2',avatar:'xx2.png'}}, + ] + } + } + } + */ +}) + +// GET 查询所有图像的前2个(第一页) +const query = `images(first:2){total,list{id,content,user:{id,username,avatar}}}` +fetch('/api?'+query).then(res => res.json()).then(data => { + console.log(data) + /* + { + data: { + images: { + total:4, + list:[ + {id:1,content:'xx1.png',user:{id:1,user_name:'xx1',avatar:'xx1.png'}}, + {id:2,content:'xx2.png',user:{id:1,user_name:'xx1',avatar:'xx1.png'}}, + ] + } + } + } + */ +}) + + +// GET 查询所有图像的指定id之后的前2个(翻页) +const query = `images(after:2,first:2){total,list{id,content,user:{id,username,avatar}}}` +fetch('/api?'+query).then(res => res.json()).then(data => { + console.log(data) + /* + { + data: { + images: { + total:4, + list:[ + {id:3,content:'xx3.png',user:{id:2,user_name:'xx2',avatar:'xx2.png'}}, + {id:4,content:'xx4.png',user:{id:2,user_name:'xx2',avatar:'xx2.png'}}, + ] + } + } + } + */ +}) + +``` + +如何获取schema结构(GraphQL自省) +```javascript +const query = `/api?query={__schema{types{name,description,fields{name,type{name}}}}}` +const data = await fetch(query).then(res.json()) +``` + +当前数据结构 +```go +type Image struct { + ID int `json:"id" db:"id"` + Width int `json:"width" db:"width"` + Height int `json:"height" db:"height"` + Content string `json:"content" db:"content"` + Remark string `json:"remark" db:"remark"` + Description string `json:"description" db:"description"` + Tags string `json:"tags" db:"tags"` + Rank string `json:"rank" db:"rank"` + CommentNum int `json:"comment_num" db:"comment_num"` + ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"` + PraiseCount int `json:"praise_count" db:"praise_count"` + CollectCount int `json:"collect_count" db:"collect_count"` + ArticleID int `json:"article_id" db:"article_id"` + UserID int `json:"user_id" db:"user_id"` + User User `json:"user" db:"-"` + Article Article `json:"article" db:"-"` + CreateTime time.Time `json:"create_time" db:"create_time"` + UpdateTime time.Time `json:"update_time" db:"update_time"` + Text TextList `json:"text" db:"text"` +} + +type TextList []struct { + Text string `json:"text"` + Confidence float64 `json:"confidence"` + Coordinate [][]float64 `json:"coordinate"` +} + +type User struct { + ID int `json:"id" db:"id"` + UserName *string `json:"user_name" db:"user_name"` + Avatar *string `json:"avatar" db:"avatar"` + Rank *string `json:"rank" db:"rank"` + CreateTime time.Time `json:"create_time" db:"create_time"` + UpdateTime time.Time `json:"update_time" db:"update_time"` +} + +type Article struct { + ID int `json:"id" db:"id"` + Title string `json:"title" db:"title"` + Tags string `json:"tags" db:"tags"` + CreateTime time.Time `json:"create_time" db:"create_time"` + UpdateTime time.Time `json:"update_time" db:"update_time"` +} +``` + +* 以上为当前基本数据结构和查询方法 +* 如需添加筛选条件排序条件或其他字段后续补充 + + +### 流媒体 通过流媒体服务降低视频文件加载消耗及防止恶意刷流量 对视频地址添加有效期, 过期需由服务器重新提供token认证观众身份 可后期增加基于用户账户或cookie信任度评估的视频播放权限认证 @@ -179,7 +308,7 @@ https://github.com/facebookarchive/fb.resnet.torch/tree/master/pretrained 通用权重模型接口 -### 获取图片列表(标准查询) +### 获取图片列表(RESTful标准查询) GET /api/images ```javascript diff --git a/bin/main.go b/bin/main.go index c5ca05d..edb73af 100644 --- a/bin/main.go +++ b/bin/main.go @@ -210,7 +210,7 @@ func main() { w.Write([]byte("Hello World!")) }) - http.Handle("/graphql", handler.New(&handler.Config{ + http.Handle("/api", handler.New(&handler.Config{ Schema: &schema, Pretty: true, }))