articles
This commit is contained in:
@@ -343,6 +343,97 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"articles": &graphql.Field{
|
||||||
|
Type: graphql.NewObject(graphql.ObjectConfig{
|
||||||
|
Name: "ArticleConnection",
|
||||||
|
Fields: graphql.Fields{
|
||||||
|
"list": &graphql.Field{Type: graphql.NewList(article)},
|
||||||
|
"total": &graphql.Field{Type: graphql.Int},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
Args: graphql.FieldConfigArgument{
|
||||||
|
"id": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||||
|
"title": &graphql.ArgumentConfig{Type: graphql.String},
|
||||||
|
"tags": &graphql.ArgumentConfig{Type: graphql.String},
|
||||||
|
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||||
|
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||||
|
},
|
||||||
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
|
var fields []string
|
||||||
|
requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections
|
||||||
|
for _, field := range requestedFields {
|
||||||
|
fieldAST, ok := field.(*ast.Field)
|
||||||
|
if ok {
|
||||||
|
switch fieldAST.Name.Value {
|
||||||
|
case "list":
|
||||||
|
for _, field := range fieldAST.SelectionSet.Selections {
|
||||||
|
fieldAST, ok := field.(*ast.Field)
|
||||||
|
if ok {
|
||||||
|
fields = append(fields, fieldAST.Name.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "total":
|
||||||
|
fmt.Println("total")
|
||||||
|
default:
|
||||||
|
fmt.Println(fieldAST.Name.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
first := p.Args["first"]
|
||||||
|
after := p.Args["after"]
|
||||||
|
fields_str := strings.Join(fields, ",")
|
||||||
|
var where []string
|
||||||
|
if p.Args["id"] != nil {
|
||||||
|
where = append(where, fmt.Sprintf("id=%d", p.Args["id"]))
|
||||||
|
}
|
||||||
|
if p.Args["title"] != nil {
|
||||||
|
where = append(where, fmt.Sprintf("title='%s'", p.Args["title"]))
|
||||||
|
}
|
||||||
|
// 筛选条件
|
||||||
|
where_str := strings.Join(where, " AND ")
|
||||||
|
if where_str != "" {
|
||||||
|
where_str = "WHERE " + where_str
|
||||||
|
}
|
||||||
|
|
||||||
|
var query strings.Builder
|
||||||
|
query.WriteString(fmt.Sprintf("SELECT %s FROM web_article %s LIMIT %d OFFSET %s", fields_str, where_str, first, after))
|
||||||
|
// 返回翻页信息
|
||||||
|
var articles []Article
|
||||||
|
if err := connection.Select(&articles, query.String()); err != nil {
|
||||||
|
fmt.Println("获取文章列表失败", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return map[string]interface{}{
|
||||||
|
"list": articles,
|
||||||
|
"total": 0,
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"tags": &graphql.Field{
|
||||||
|
Type: graphql.NewObject(graphql.ObjectConfig{
|
||||||
|
Name: "TagConnection",
|
||||||
|
Fields: graphql.Fields{
|
||||||
|
"list": &graphql.Field{Type: graphql.NewList(graphql.String)},
|
||||||
|
"total": &graphql.Field{Type: graphql.Int},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
Args: graphql.FieldConfigArgument{
|
||||||
|
"first": &graphql.ArgumentConfig{Type: graphql.Int, DefaultValue: 10},
|
||||||
|
"after": &graphql.ArgumentConfig{Type: graphql.String, DefaultValue: "0"},
|
||||||
|
"before": &graphql.ArgumentConfig{Type: graphql.String},
|
||||||
|
},
|
||||||
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
|
var tags []string
|
||||||
|
if err := connection.Select(&tags, "SELECT DISTINCT tags FROM web_images LIMIT 10"); err != nil {
|
||||||
|
fmt.Println("获取标签列表失败", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return map[string]interface{}{
|
||||||
|
"list": tags,
|
||||||
|
"total": 0,
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
}})})
|
}})})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return schema, err
|
return schema, err
|
||||||
|
@@ -316,6 +316,13 @@ func main() {
|
|||||||
// 文字搜索支持翻页
|
// 文字搜索支持翻页
|
||||||
// 文字搜索支持与按颜色筛选混合
|
// 文字搜索支持与按颜色筛选混合
|
||||||
|
|
||||||
|
// TODO 查找含有指定文字的图像
|
||||||
|
// TODO 查找含有指定标签的图像
|
||||||
|
// TODO 查找含有指定特征的图像
|
||||||
|
// TODO 查找含有指定颜色的图像(倒排索引)
|
||||||
|
// TODO 查找含有指定分类的图像
|
||||||
|
// 1,000,000
|
||||||
|
|
||||||
// 获取查询条件(忽略空值)
|
// 获取查询条件(忽略空值)
|
||||||
QueryConditions := func(key string) (list []string) {
|
QueryConditions := func(key string) (list []string) {
|
||||||
for _, item := range strings.Split(r.URL.Query().Get(key), ",") {
|
for _, item := range strings.Split(r.URL.Query().Get(key), ",") {
|
||||||
@@ -350,8 +357,6 @@ func main() {
|
|||||||
images.Total = 0
|
images.Total = 0
|
||||||
images.Next = false
|
images.Next = false
|
||||||
images.List = make([]interface{}, 0)
|
images.List = make([]interface{}, 0)
|
||||||
|
|
||||||
// 将对象转换为有缩进的JSON输出
|
|
||||||
data, _ := json.MarshalIndent(images, "", " ")
|
data, _ := json.MarshalIndent(images, "", " ")
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
|
Reference in New Issue
Block a user