使文章支持用户信息查询

This commit is contained in:
2024-08-01 02:19:51 +08:00
parent ccd773ddad
commit 3515799c5c

View File

@@ -27,19 +27,6 @@ func NewSchema(config Config) (graphql.Schema, error) {
log.Fatalln("连接数据库失败", err) log.Fatalln("连接数据库失败", err)
} }
// 文章的可选字段
article := graphql.NewObject(graphql.ObjectConfig{
Name: "Article",
Description: "文章信息",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int, Description: "文章ID"},
"title": &graphql.Field{Type: graphql.String, Description: "文章标题"},
"tags": &graphql.Field{Type: graphql.String, Description: "文章标签"},
"create_time": &graphql.Field{Type: graphql.DateTime, Description: "文章创建时间"},
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "文章更新时间"},
},
})
// 用户的可选字段 // 用户的可选字段
user := graphql.NewObject(graphql.ObjectConfig{ user := graphql.NewObject(graphql.ObjectConfig{
Name: "User", Name: "User",
@@ -55,6 +42,20 @@ func NewSchema(config Config) (graphql.Schema, error) {
}, },
}) })
// 文章的可选字段
article := graphql.NewObject(graphql.ObjectConfig{
Name: "Article",
Description: "文章信息",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int, Description: "文章ID"},
"title": &graphql.Field{Type: graphql.String, Description: "文章标题"},
"tags": &graphql.Field{Type: graphql.String, Description: "文章标签"},
"user": &graphql.Field{Type: user, Description: "文章所属用户"},
"create_time": &graphql.Field{Type: graphql.DateTime, Description: "文章创建时间"},
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "文章更新时间"},
},
})
// 图像中的文字提取 // 图像中的文字提取
text := graphql.NewObject(graphql.ObjectConfig{ text := graphql.NewObject(graphql.ObjectConfig{
Name: "Text", Name: "Text",
@@ -141,7 +142,6 @@ func NewSchema(config Config) (graphql.Schema, error) {
"before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"}, "before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
}, },
Resolve: func(p graphql.ResolveParams) (interface{}, error) { Resolve: func(p graphql.ResolveParams) (interface{}, error) {
fields := strings.Join(get_fields(p.Info.FieldASTs[0].SelectionSet.Selections), ",")
var where []string var where []string
if p.Args["id"] != nil { if p.Args["id"] != nil {
where = append(where, fmt.Sprintf("id=%d", p.Args["id"])) where = append(where, fmt.Sprintf("id=%d", p.Args["id"]))
@@ -159,6 +159,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
var users []User var users []User
var total int var total int
fields := strings.Join(get_fields(p.Info.FieldASTs[0].SelectionSet.Selections), ",")
query.WriteString(fmt.Sprintf("SELECT %s FROM web_member %s LIMIT %d OFFSET %d", fields, where_str, 10, 0)) query.WriteString(fmt.Sprintf("SELECT %s FROM web_member %s LIMIT %d OFFSET %d", fields, where_str, 10, 0))
if err := connection.Select(&users, query.String()); err != nil { if err := connection.Select(&users, query.String()); err != nil {
fmt.Println("获取用户列表失败", err) fmt.Println("获取用户列表失败", err)