Article
This commit is contained in:
		@@ -27,6 +27,18 @@ func init() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewSchema() (graphql.Schema, error) {
 | 
					func NewSchema() (graphql.Schema, error) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						article := graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
 | 
							Name: "Article",
 | 
				
			||||||
 | 
							Fields: graphql.Fields{
 | 
				
			||||||
 | 
								"id":          &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
								"title":       &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
								"tags":        &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
								"create_time": &graphql.Field{Type: graphql.DateTime},
 | 
				
			||||||
 | 
								"update_time": &graphql.Field{Type: graphql.DateTime},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	user := graphql.NewObject(graphql.ObjectConfig{
 | 
						user := graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
		Name: "User",
 | 
							Name: "User",
 | 
				
			||||||
		Fields: graphql.Fields{
 | 
							Fields: graphql.Fields{
 | 
				
			||||||
@@ -97,9 +109,8 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
			"collect_count":           &graphql.Field{Type: graphql.Int},
 | 
								"collect_count":           &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
			"create_time":             &graphql.Field{Type: graphql.DateTime},
 | 
								"create_time":             &graphql.Field{Type: graphql.DateTime},
 | 
				
			||||||
			"update_time":             &graphql.Field{Type: graphql.DateTime},
 | 
								"update_time":             &graphql.Field{Type: graphql.DateTime},
 | 
				
			||||||
			"user": &graphql.Field{
 | 
								"user":                    &graphql.Field{Type: user},
 | 
				
			||||||
				Type: user,
 | 
								"article":                 &graphql.Field{Type: article},
 | 
				
			||||||
			},
 | 
					 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -129,7 +140,7 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
					if fieldAST.Name.Value == "user" {
 | 
										if fieldAST.Name.Value == "user" {
 | 
				
			||||||
						fields = append(fields, "user_id")
 | 
											fields = append(fields, "user_id")
 | 
				
			||||||
					} else if fieldAST.Name.Value == "article" {
 | 
										} else if fieldAST.Name.Value == "article" {
 | 
				
			||||||
						fields = append(fields, "article_category_top_id")
 | 
											fields = append(fields, "article_id")
 | 
				
			||||||
					} else {
 | 
										} else {
 | 
				
			||||||
						fields = append(fields, fieldAST.Name.Value)
 | 
											fields = append(fields, fieldAST.Name.Value)
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
@@ -196,6 +207,37 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 获取文章信息(如果图像列表不为空且请求字段中包含article)
 | 
				
			||||||
 | 
								if len(images) > 0 && strings.Contains(fields_str, "article_id") {
 | 
				
			||||||
 | 
									// 取到所有的文章ID, 去除重复
 | 
				
			||||||
 | 
									article_ids := make(map[int]bool)
 | 
				
			||||||
 | 
									for _, image := range images {
 | 
				
			||||||
 | 
										article_ids[image.ArticleID] = true
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// map 转换为数组
 | 
				
			||||||
 | 
									uniqueIds := make([]int, 0, len(article_ids))
 | 
				
			||||||
 | 
									for id := range article_ids {
 | 
				
			||||||
 | 
										uniqueIds = append(uniqueIds, id)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// 合并为以逗号分隔的字符串
 | 
				
			||||||
 | 
									article_ids_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(uniqueIds)), ","), "[]")
 | 
				
			||||||
 | 
									fmt.Println(article_ids_str)
 | 
				
			||||||
 | 
									// 查询文章信息
 | 
				
			||||||
 | 
									var articles []Article
 | 
				
			||||||
 | 
									if err := connection.Select(&articles, fmt.Sprintf("SELECT id,title,tags,create_time,update_time FROM web_article WHERE id IN (%s)", article_ids_str)); err != nil {
 | 
				
			||||||
 | 
										fmt.Println("获取文章列表失败", err)
 | 
				
			||||||
 | 
										return nil, err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// 将文章信息与图像信息关联
 | 
				
			||||||
 | 
									for i, image := range images {
 | 
				
			||||||
 | 
										for _, article := range articles {
 | 
				
			||||||
 | 
											if image.ArticleID == article.ID {
 | 
				
			||||||
 | 
												images[i].Article = article
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			return images, nil
 | 
								return images, nil
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,13 +5,14 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Image struct {
 | 
					type Image struct {
 | 
				
			||||||
	Id                   int       `json:"id"                      db:"id"`
 | 
						ID                   int       `json:"id"                      db:"id"`
 | 
				
			||||||
	Width                int       `json:"width"                   db:"width"`
 | 
						Width                int       `json:"width"                   db:"width"`
 | 
				
			||||||
	Height               int       `json:"height"                  db:"height"`
 | 
						Height               int       `json:"height"                  db:"height"`
 | 
				
			||||||
	Content              string    `json:"content"                 db:"content"`
 | 
						Content              string    `json:"content"                 db:"content"`
 | 
				
			||||||
	ArticleCategoryTopId int       `json:"article_category_top_id" db:"article_category_top_id"`
 | 
						ArticleCategoryTopId int       `json:"article_category_top_id" db:"article_category_top_id"`
 | 
				
			||||||
	PraiseCount          int       `json:"praise_count"            db:"praise_count"`
 | 
						PraiseCount          int       `json:"praise_count"            db:"praise_count"`
 | 
				
			||||||
	CollectCount         int       `json:"collect_count"           db:"collect_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"`
 | 
						UserID               int       `json:"user_id"                 db:"user_id"`
 | 
				
			||||||
	User                 User      `json:"user"                    db:"user"`
 | 
						User                 User      `json:"user"                    db:"user"`
 | 
				
			||||||
	Article              Article   `json:"article"                 db:"article"`
 | 
						Article              Article   `json:"article"                 db:"article"`
 | 
				
			||||||
@@ -29,7 +30,7 @@ type User struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Article struct {
 | 
					type Article struct {
 | 
				
			||||||
	Id         int       `json:"id"          db:"id"`
 | 
						ID         int       `json:"id"          db:"id"`
 | 
				
			||||||
	Title      string    `json:"title"       db:"title"`
 | 
						Title      string    `json:"title"       db:"title"`
 | 
				
			||||||
	Tags       string    `json:"tags"        db:"tags"`
 | 
						Tags       string    `json:"tags"        db:"tags"`
 | 
				
			||||||
	CreateTime time.Time `json:"create_time" db:"create_time"`
 | 
						CreateTime time.Time `json:"create_time" db:"create_time"`
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user