实装 graphql
This commit is contained in:
		
							
								
								
									
										191
									
								
								api/graphql.go
									
									
									
									
									
								
							
							
						
						
									
										191
									
								
								api/graphql.go
									
									
									
									
									
								
							@@ -54,52 +54,6 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 用户列表查询
 | 
					 | 
				
			||||||
	users := &graphql.Field{
 | 
					 | 
				
			||||||
		Type: graphql.NewList(user),
 | 
					 | 
				
			||||||
		Args: graphql.FieldConfigArgument{
 | 
					 | 
				
			||||||
			"id":          &graphql.ArgumentConfig{Type: graphql.Int},
 | 
					 | 
				
			||||||
			"user_name":   &graphql.ArgumentConfig{Type: graphql.String},
 | 
					 | 
				
			||||||
			"avatar":      &graphql.ArgumentConfig{Type: graphql.String},
 | 
					 | 
				
			||||||
			"rank":        &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 {
 | 
					 | 
				
			||||||
					fields = append(fields, fieldAST.Name.Value)
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			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["user_name"] != nil {
 | 
					 | 
				
			||||||
				where = append(where, fmt.Sprintf("user_name='%s'", p.Args["user_name"]))
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			// 筛选条件
 | 
					 | 
				
			||||||
			where_str := strings.Join(where, " AND ")
 | 
					 | 
				
			||||||
			fmt.Println(where_str)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			var query strings.Builder
 | 
					 | 
				
			||||||
			query.WriteString(fmt.Sprintf("SELECT %s FROM web_member WHERE %s LIMIT %s", fields_str, where_str, "10"))
 | 
					 | 
				
			||||||
			fmt.Println(query.String())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			var users []User
 | 
					 | 
				
			||||||
			if err := connection.Select(&users, query.String()); err != nil {
 | 
					 | 
				
			||||||
				fmt.Println("获取用户列表失败", err)
 | 
					 | 
				
			||||||
				return nil, err
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			fmt.Println(users)
 | 
					 | 
				
			||||||
			return users, nil
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	image := graphql.NewObject(graphql.ObjectConfig{
 | 
						image := graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
		Name: "Image",
 | 
							Name: "Image",
 | 
				
			||||||
		Fields: graphql.Fields{
 | 
							Fields: graphql.Fields{
 | 
				
			||||||
@@ -122,27 +76,123 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	images := &graphql.Field{
 | 
						schema, err := graphql.NewSchema(graphql.SchemaConfig{Query: graphql.NewObject(graphql.ObjectConfig{Name: "RootQuery", Fields: graphql.Fields{
 | 
				
			||||||
		Type: graphql.NewList(image),
 | 
							"users": &graphql.Field{
 | 
				
			||||||
 | 
								Type: graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
 | 
									Name: "UserConnection",
 | 
				
			||||||
 | 
									Fields: graphql.Fields{
 | 
				
			||||||
 | 
										"list":  &graphql.Field{Type: graphql.NewList(user)},
 | 
				
			||||||
 | 
										"next":  &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
										"total": &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								}),
 | 
				
			||||||
			Args: graphql.FieldConfigArgument{
 | 
								Args: graphql.FieldConfigArgument{
 | 
				
			||||||
			"id": &graphql.ArgumentConfig{
 | 
									"id":          &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
				Type: graphql.Int,
 | 
									"user_name":   &graphql.ArgumentConfig{Type: graphql.String},
 | 
				
			||||||
 | 
									"avatar":      &graphql.ArgumentConfig{Type: graphql.String},
 | 
				
			||||||
 | 
									"rank":        &graphql.ArgumentConfig{Type: graphql.String},
 | 
				
			||||||
 | 
									"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
 | 
				
			||||||
 | 
									"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
 | 
				
			||||||
 | 
									"first":       &graphql.ArgumentConfig{Type: graphql.Int, DefaultValue: 10},     // 翻页参数
 | 
				
			||||||
 | 
									"after":       &graphql.ArgumentConfig{Type: graphql.String, DefaultValue: "0"}, // 翻页参数
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			"width": &graphql.ArgumentConfig{
 | 
								Resolve: func(p graphql.ResolveParams) (interface{}, error) {
 | 
				
			||||||
				Type: graphql.Int,
 | 
									var fields []string
 | 
				
			||||||
 | 
									requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections
 | 
				
			||||||
 | 
									for _, field := range requestedFields {
 | 
				
			||||||
 | 
										fieldAST, ok := field.(*ast.Field)
 | 
				
			||||||
 | 
										if ok {
 | 
				
			||||||
 | 
											if fieldAST.Name.Value == "list" {
 | 
				
			||||||
 | 
												for _, field := range fieldAST.SelectionSet.Selections {
 | 
				
			||||||
 | 
													fieldAST, ok := field.(*ast.Field)
 | 
				
			||||||
 | 
													if ok {
 | 
				
			||||||
 | 
														fields = append(fields, fieldAST.Name.Value)
 | 
				
			||||||
 | 
													}
 | 
				
			||||||
 | 
												}
 | 
				
			||||||
 | 
											} else if fieldAST.Name.Value == "next" {
 | 
				
			||||||
 | 
												fmt.Println("next")
 | 
				
			||||||
 | 
											} else {
 | 
				
			||||||
 | 
												fields = append(fields, 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["user_name"] != nil {
 | 
				
			||||||
 | 
										where = append(where, fmt.Sprintf("user_name='%s'", p.Args["user_name"]))
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// 筛选条件
 | 
				
			||||||
 | 
									where_str := strings.Join(where, " AND ")
 | 
				
			||||||
 | 
									var query strings.Builder
 | 
				
			||||||
 | 
									query.WriteString(fmt.Sprintf("SELECT %s FROM web_member WHERE %s LIMIT %d OFFSET %s", fields_str, where_str, first, after))
 | 
				
			||||||
 | 
									// 返回翻页信息
 | 
				
			||||||
 | 
									fmt.Println(query.String())
 | 
				
			||||||
 | 
									var users []User
 | 
				
			||||||
 | 
									if err := connection.Select(&users, query.String()); err != nil {
 | 
				
			||||||
 | 
										fmt.Println("获取用户列表失败", err)
 | 
				
			||||||
 | 
										return nil, err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									//return users, nil
 | 
				
			||||||
 | 
									//var users []User
 | 
				
			||||||
 | 
									//// 获取用户列表
 | 
				
			||||||
 | 
									//if err := connection.Select(&users, "SELECT id,user_name,avatar,rank,create_time,update_time FROM web_member"); err != nil {
 | 
				
			||||||
 | 
									//	fmt.Println("获取用户列表失败", err)
 | 
				
			||||||
 | 
									//	return nil, err
 | 
				
			||||||
 | 
									//}
 | 
				
			||||||
 | 
									//// 获取总数
 | 
				
			||||||
 | 
									//var count int
 | 
				
			||||||
 | 
									//if err := connection.Get(&count, "SELECT COUNT(*) FROM web_member"); err != nil {
 | 
				
			||||||
 | 
									//	fmt.Println("获取用户总数失败", err)
 | 
				
			||||||
 | 
									//	return nil, err
 | 
				
			||||||
 | 
									//}
 | 
				
			||||||
 | 
									return map[string]interface{}{
 | 
				
			||||||
 | 
										"list": users,
 | 
				
			||||||
 | 
										"next": "2333",
 | 
				
			||||||
 | 
									}, nil
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			"height": &graphql.ArgumentConfig{
 | 
					 | 
				
			||||||
				Type: graphql.Int,
 | 
					 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
			"content": &graphql.ArgumentConfig{
 | 
							"images": &graphql.Field{
 | 
				
			||||||
				Type: graphql.String,
 | 
								Type: graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
 | 
									Name: "ImageConnection",
 | 
				
			||||||
 | 
									Fields: graphql.Fields{
 | 
				
			||||||
 | 
										"list":  &graphql.Field{Type: graphql.NewList(image)},
 | 
				
			||||||
 | 
										"next":  &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
										"total": &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
 | 
								}),
 | 
				
			||||||
 | 
								Args: graphql.FieldConfigArgument{
 | 
				
			||||||
 | 
									"id":            &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
 | 
									"width":         &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
 | 
									"height":        &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
 | 
									"content":       &graphql.ArgumentConfig{Type: graphql.String},
 | 
				
			||||||
 | 
									"remark":        &graphql.ArgumentConfig{Type: graphql.String},
 | 
				
			||||||
 | 
									"description":   &graphql.ArgumentConfig{Type: graphql.String},
 | 
				
			||||||
 | 
									"tags":          &graphql.ArgumentConfig{Type: graphql.String},
 | 
				
			||||||
 | 
									"rank":          &graphql.ArgumentConfig{Type: graphql.String},
 | 
				
			||||||
 | 
									"comment_num":   &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
 | 
									"praise_count":  &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
 | 
									"collect_count": &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
 | 
									"article_id":    &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
 | 
									"user_id":       &graphql.ArgumentConfig{Type: graphql.Int},
 | 
				
			||||||
 | 
									"create_time":   &graphql.ArgumentConfig{Type: graphql.DateTime},
 | 
				
			||||||
 | 
									"update_time":   &graphql.ArgumentConfig{Type: graphql.DateTime},
 | 
				
			||||||
 | 
									"first":         &graphql.ArgumentConfig{Type: graphql.Int, DefaultValue: 10},     // 翻页参数
 | 
				
			||||||
 | 
									"after":         &graphql.ArgumentConfig{Type: graphql.String, DefaultValue: "0"}, // 翻页参数
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
 | 
								Resolve: func(p graphql.ResolveParams) (interface{}, error) {
 | 
				
			||||||
				// 返回字段
 | 
									// 返回字段
 | 
				
			||||||
				var fields []string
 | 
									var fields []string
 | 
				
			||||||
				requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections
 | 
									requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections
 | 
				
			||||||
				for _, field := range requestedFields {
 | 
									for _, field := range requestedFields {
 | 
				
			||||||
 | 
										fieldAST, ok := field.(*ast.Field)
 | 
				
			||||||
 | 
										if ok {
 | 
				
			||||||
 | 
											if fieldAST.Name.Value == "list" {
 | 
				
			||||||
 | 
												for _, field := range fieldAST.SelectionSet.Selections {
 | 
				
			||||||
								fieldAST, ok := field.(*ast.Field)
 | 
													fieldAST, ok := field.(*ast.Field)
 | 
				
			||||||
								if ok {
 | 
													if ok {
 | 
				
			||||||
									if fieldAST.Name.Value == "user" {
 | 
														if fieldAST.Name.Value == "user" {
 | 
				
			||||||
@@ -154,6 +204,15 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
									}
 | 
														}
 | 
				
			||||||
								}
 | 
													}
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
 | 
											} else if fieldAST.Name.Value == "next" {
 | 
				
			||||||
 | 
												fmt.Println("next")
 | 
				
			||||||
 | 
											} else {
 | 
				
			||||||
 | 
												fields = append(fields, fieldAST.Name.Value)
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									first := p.Args["first"]
 | 
				
			||||||
 | 
									after := p.Args["after"]
 | 
				
			||||||
				fields_str := strings.Join(fields, ",")
 | 
									fields_str := strings.Join(fields, ",")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// 筛选条件
 | 
									// 筛选条件
 | 
				
			||||||
@@ -171,11 +230,10 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
					where = append(where, fmt.Sprintf("content='%s'", p.Args["content"]))
 | 
										where = append(where, fmt.Sprintf("content='%s'", p.Args["content"]))
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				where_str := strings.Join(where, " AND ")
 | 
									where_str := strings.Join(where, " AND ")
 | 
				
			||||||
			fmt.Println(where_str)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// 执行查询
 | 
									// 执行查询
 | 
				
			||||||
				var query strings.Builder
 | 
									var query strings.Builder
 | 
				
			||||||
			query.WriteString(fmt.Sprintf("SELECT %s FROM web_images WHERE %s LIMIT %s", fields_str, where_str, "10"))
 | 
									query.WriteString(fmt.Sprintf("SELECT %s FROM web_images WHERE %s LIMIT %d OFFSET %s", fields_str, where_str, first, after))
 | 
				
			||||||
				fmt.Println(query.String())
 | 
									fmt.Println(query.String())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				var images []Image
 | 
									var images []Image
 | 
				
			||||||
@@ -198,7 +256,6 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
					}
 | 
										}
 | 
				
			||||||
					// 合并为以逗号分隔的字符串
 | 
										// 合并为以逗号分隔的字符串
 | 
				
			||||||
					user_ids_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(uniqueIds)), ","), "[]")
 | 
										user_ids_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(uniqueIds)), ","), "[]")
 | 
				
			||||||
				fmt.Println(user_ids_str)
 | 
					 | 
				
			||||||
					// 查询用户信息
 | 
										// 查询用户信息
 | 
				
			||||||
					var users []User
 | 
										var users []User
 | 
				
			||||||
					if err := connection.Select(&users, fmt.Sprintf("SELECT id,user_name,avatar,rank,create_time,update_time FROM web_member WHERE id IN (%s)", user_ids_str)); err != nil {
 | 
										if err := connection.Select(&users, fmt.Sprintf("SELECT id,user_name,avatar,rank,create_time,update_time FROM web_member WHERE id IN (%s)", user_ids_str)); err != nil {
 | 
				
			||||||
@@ -229,7 +286,6 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
					}
 | 
										}
 | 
				
			||||||
					// 合并为以逗号分隔的字符串
 | 
										// 合并为以逗号分隔的字符串
 | 
				
			||||||
					article_ids_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(uniqueIds)), ","), "[]")
 | 
										article_ids_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(uniqueIds)), ","), "[]")
 | 
				
			||||||
				fmt.Println(article_ids_str)
 | 
					 | 
				
			||||||
					// 查询文章信息
 | 
										// 查询文章信息
 | 
				
			||||||
					var articles []Article
 | 
										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 {
 | 
										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 {
 | 
				
			||||||
@@ -246,16 +302,13 @@ func NewSchema() (graphql.Schema, error) {
 | 
				
			|||||||
					}
 | 
										}
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			return images, nil
 | 
									return map[string]interface{}{
 | 
				
			||||||
 | 
										"list": images,
 | 
				
			||||||
 | 
										"next": "2333",
 | 
				
			||||||
 | 
									}, nil
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
	}
 | 
							},
 | 
				
			||||||
 | 
						}})})
 | 
				
			||||||
	rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: graphql.Fields{
 | 
					 | 
				
			||||||
		"users":  users,
 | 
					 | 
				
			||||||
		"images": images,
 | 
					 | 
				
			||||||
	}}
 | 
					 | 
				
			||||||
	schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
 | 
					 | 
				
			||||||
	schema, err := graphql.NewSchema(schemaConfig)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return schema, err
 | 
							return schema, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,9 +27,9 @@ func main() {
 | 
				
			|||||||
			http.Error(w, result.Errors[0].Error(), 500)
 | 
								http.Error(w, result.Errors[0].Error(), 500)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		rJSON, _ := json.MarshalIndent(result, "", "  ")
 | 
							rJSON, _ := json.MarshalIndent(result.Data, "", "  ")
 | 
				
			||||||
		w.Write(rJSON)
 | 
							w.Write(rJSON)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
						fmt.Println("Now server is running on port 6001")
 | 
				
			||||||
	http.ListenAndServe(":6001", nil)
 | 
						http.ListenAndServe(":6001", nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user