支持分类查询 category

This commit is contained in:
2024-11-03 23:23:19 +08:00
parent 5c1d44dc5b
commit 3020481eca
2 changed files with 108 additions and 2 deletions

View File

@@ -81,7 +81,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
// 文章的可选字段
article = graphql.NewObject(graphql.ObjectConfig{
Name: "Article",
Description: "文章信息",
Description: "文章",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int, Description: "文章ID"},
"title": &graphql.Field{Type: graphql.String, Description: "文章标题"},
@@ -92,6 +92,26 @@ func NewSchema(config Config) (graphql.Schema, error) {
},
})
category := graphql.NewObject(graphql.ObjectConfig{
Name: "Category",
Description: "分类",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int, Description: "分类ID"},
"title": &graphql.Field{Type: graphql.String, Description: "分类标题"},
"keyword": &graphql.Field{Type: graphql.String, Description: "分类关键词"},
"parent_id": &graphql.Field{Type: graphql.Int, Description: "分类父级ID"},
"create_time": &graphql.Field{Type: graphql.DateTime, Description: "分类创建时间"},
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "分类更新时间"},
"status": &graphql.Field{Type: graphql.Int, Description: "分类状态"},
"content": &graphql.Field{Type: graphql.String, Description: "分类内容"},
"sort": &graphql.Field{Type: graphql.Int, Description: "分类排序"},
"image": &graphql.Field{Type: graphql.String, Description: "分类图片"},
"image_num": &graphql.Field{Type: graphql.Int, Description: "分类图片数量"},
"article_num": &graphql.Field{Type: graphql.Int, Description: "分类文章数量"},
},
})
fmt.Println(category)
// 图像中的文字提取
text = graphql.NewObject(graphql.ObjectConfig{
Name: "Text",
@@ -106,7 +126,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
// 图像的可选字段
image := graphql.NewObject(graphql.ObjectConfig{
Name: "Image",
Description: "图像信息",
Description: "图像",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int, Description: "图像ID"},
"width": &graphql.Field{Type: graphql.Int, Description: "图像宽度"},
@@ -180,6 +200,76 @@ func NewSchema(config Config) (graphql.Schema, error) {
}
schema, err := graphql.NewSchema(graphql.SchemaConfig{Query: graphql.NewObject(graphql.ObjectConfig{Name: "RootQuery", Fields: graphql.Fields{
"categorys": &graphql.Field{
Name: "categorys",
Description: "分类列表",
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "CategoryConnection",
Description: "条件筛选分类列表",
Fields: graphql.Fields{
"list": &graphql.Field{Type: graphql.NewList(category), Description: "分类列表"},
"total": &graphql.Field{Type: graphql.Int, Description: "分类总数"},
},
}),
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选分类中指定ID的"},
"title": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选分类中含有指定标题的"},
"keyword": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选分类中含有指定关键词的"},
"parent_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选分类中含有指定父级ID的"},
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "筛选分类中创建时间等于指定值的"},
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "筛选分类中更新时间等于指定值的"},
"first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"},
"last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"},
"after": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
"before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
var categorys []Category
var total int
var err error
// 获取筛选条件
var category Category
mapstructure.Decode(p.Args, &category)
// 获取翻页参数
var first, last int
var after, before string
if p.Args["first"] != nil {
first = p.Args["first"].(int)
}
if p.Args["last"] != nil {
last = p.Args["last"].(int)
}
if p.Args["after"] != nil {
after = p.Args["after"].(string)
}
if p.Args["before"] != nil {
before = p.Args["before"].(string)
}
fmt.Println(first, last, after, before)
// 获取请求的字段
fields := strings.Join(get_fields(p.Info.FieldASTs[0].SelectionSet.Selections), ",")
// 筛选条件
where_str := ""
// 查询分类列表
var query strings.Builder
query.WriteString(fmt.Sprintf("SELECT %s FROM web_article %s LIMIT %d OFFSET %d", fields, where_str, 10, 0))
if err := connection.Select(&categorys, query.String()); err != nil {
fmt.Println("查询分类列表失败", err)
return nil, err
}
return map[string]interface{}{
"list": categorys,
"total": total,
}, err
},
},
"users": &graphql.Field{
Name: "users",
Description: "用户列表",