graphql
This commit is contained in:
		
							
								
								
									
										26
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -17,6 +17,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"git.satori.love/gameui/webp/models"
 | 
						"git.satori.love/gameui/webp/models"
 | 
				
			||||||
	_ "github.com/go-sql-driver/mysql"
 | 
						_ "github.com/go-sql-driver/mysql"
 | 
				
			||||||
 | 
						"github.com/graphql-go/graphql"
 | 
				
			||||||
	"github.com/milvus-io/milvus-sdk-go/v2/entity"
 | 
						"github.com/milvus-io/milvus-sdk-go/v2/entity"
 | 
				
			||||||
	"github.com/spf13/viper"
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -175,11 +176,32 @@ func main() {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Schema
 | 
				
			||||||
 | 
						schema, err := models.NewSchema()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Fatalf("failed to create new schema, error: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
						http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
		w.Write([]byte("Hello World!"))
 | 
							w.Write([]byte("Hello World!"))
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
 | 
							query := r.URL.Query().Get("query")
 | 
				
			||||||
 | 
							params := graphql.Params{Schema: schema, RequestString: query}
 | 
				
			||||||
 | 
							result := graphql.Do(params)
 | 
				
			||||||
 | 
							if len(result.Errors) > 0 {
 | 
				
			||||||
 | 
								fmt.Printf("failed to execute graphql operation, errors: %+v", result.Errors)
 | 
				
			||||||
 | 
								http.Error(w, result.Errors[0].Error(), 500)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							rJSON, _ := json.Marshal(result)
 | 
				
			||||||
 | 
							fmt.Printf("%s \n", rJSON)
 | 
				
			||||||
 | 
							w.Write(rJSON)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
 | 
						http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
		http.Error(w, "Not Found", http.StatusNotFound)
 | 
							http.Error(w, "Not Found", http.StatusNotFound)
 | 
				
			||||||
@@ -249,8 +271,8 @@ func main() {
 | 
				
			|||||||
		// 私域: (自己的图片, 自己的文章, 自己的精选集, 点赞收藏精选集)
 | 
							// 私域: (自己的图片, 自己的文章, 自己的精选集, 点赞收藏精选集)
 | 
				
			||||||
		// 条件查询(模糊搜索, 时间区间, 作者, 标签, 分类, 精选集, 状态, 置顶, 模糊权重)(权重规则:权重指数)
 | 
							// 条件查询(模糊搜索, 时间区间, 作者, 标签, 分类, 精选集, 状态, 置顶, 模糊权重)(权重规则:权重指数)
 | 
				
			||||||
		// 条件筛选(交集, 并集, 差集, 子集)
 | 
							// 条件筛选(交集, 并集, 差集, 子集)
 | 
				
			||||||
		// 排序
 | 
							// 文字搜索支持翻页
 | 
				
			||||||
		// 分页
 | 
							// 文字搜索支持与按颜色筛选混合
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 获取查询条件(忽略空值)
 | 
							// 获取查询条件(忽略空值)
 | 
				
			||||||
		QueryConditions := func(key string) (list []string) {
 | 
							QueryConditions := func(key string) (list []string) {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										94
									
								
								demo/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								demo/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"runtime"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/graphql-go/graphql"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						runtime.GOMAXPROCS(runtime.NumCPU())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Schema
 | 
				
			||||||
 | 
						fields := graphql.Fields{
 | 
				
			||||||
 | 
							"hello": &graphql.Field{
 | 
				
			||||||
 | 
								Type: graphql.String,
 | 
				
			||||||
 | 
								Resolve: func(p graphql.ResolveParams) (interface{}, error) {
 | 
				
			||||||
 | 
									return "world", nil
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"users": &graphql.Field{
 | 
				
			||||||
 | 
								Type: graphql.NewList(graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
 | 
									Name: "User",
 | 
				
			||||||
 | 
									Fields: graphql.Fields{
 | 
				
			||||||
 | 
										"id": &graphql.Field{
 | 
				
			||||||
 | 
											Type: graphql.Int,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
										"name": &graphql.Field{
 | 
				
			||||||
 | 
											Type: graphql.String,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
										"age": &graphql.Field{
 | 
				
			||||||
 | 
											Type: graphql.Int,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
										"info": &graphql.Field{
 | 
				
			||||||
 | 
											Type: graphql.String,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
										"price": &graphql.Field{
 | 
				
			||||||
 | 
											Type: graphql.Float,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								})),
 | 
				
			||||||
 | 
								Args: graphql.FieldConfigArgument{
 | 
				
			||||||
 | 
									"id": &graphql.ArgumentConfig{
 | 
				
			||||||
 | 
										Type: graphql.Int,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								Resolve: func(p graphql.ResolveParams) (interface{}, error) {
 | 
				
			||||||
 | 
									fmt.Println("p.Args:", p.Args)
 | 
				
			||||||
 | 
									return []interface{}{
 | 
				
			||||||
 | 
										map[string]interface{}{
 | 
				
			||||||
 | 
											"id":    1,
 | 
				
			||||||
 | 
											"name":  "user1",
 | 
				
			||||||
 | 
											"age":   10,
 | 
				
			||||||
 | 
											"info":  "info",
 | 
				
			||||||
 | 
											"price": 1.1,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
										map[string]interface{}{
 | 
				
			||||||
 | 
											"id":    2,
 | 
				
			||||||
 | 
											"name":  "user2",
 | 
				
			||||||
 | 
											"age":   20,
 | 
				
			||||||
 | 
											"info":  "info",
 | 
				
			||||||
 | 
											"price": 2.2,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									}, nil
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
 | 
				
			||||||
 | 
						schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
 | 
				
			||||||
 | 
						schema, err := graphql.NewSchema(schemaConfig)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Fatalf("failed to create new schema, error: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							query := r.URL.Query().Get("query")
 | 
				
			||||||
 | 
							fmt.Println("query:", query)
 | 
				
			||||||
 | 
							params := graphql.Params{Schema: schema, RequestString: query}
 | 
				
			||||||
 | 
							result := graphql.Do(params)
 | 
				
			||||||
 | 
							if len(result.Errors) > 0 {
 | 
				
			||||||
 | 
								fmt.Printf("failed to execute graphql operation, errors: %+v", result.Errors)
 | 
				
			||||||
 | 
								http.Error(w, result.Errors[0].Error(), 500)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							rJSON, _ := json.Marshal(result)
 | 
				
			||||||
 | 
							fmt.Printf("%s \n", rJSON) // {"data":{"hello":"world"}}
 | 
				
			||||||
 | 
							w.Write(rJSON)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						http.ListenAndServe(":8080", nil)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@@ -24,6 +24,7 @@ require (
 | 
				
			|||||||
	github.com/elastic/go-elasticsearch/v8 v8.11.0 // indirect
 | 
						github.com/elastic/go-elasticsearch/v8 v8.11.0 // indirect
 | 
				
			||||||
	github.com/fsnotify/fsnotify v1.6.0 // indirect
 | 
						github.com/fsnotify/fsnotify v1.6.0 // indirect
 | 
				
			||||||
	github.com/golang/protobuf v1.5.2 // indirect
 | 
						github.com/golang/protobuf v1.5.2 // indirect
 | 
				
			||||||
 | 
						github.com/graphql-go/graphql v0.8.1 // indirect
 | 
				
			||||||
	github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
 | 
						github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
 | 
				
			||||||
	github.com/hashicorp/hcl v1.0.0 // indirect
 | 
						github.com/hashicorp/hcl v1.0.0 // indirect
 | 
				
			||||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
						github.com/json-iterator/go v1.1.12 // indirect
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@@ -169,6 +169,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
 | 
				
			|||||||
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
 | 
					github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
 | 
				
			||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
 | 
					github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
 | 
				
			||||||
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
 | 
					github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
 | 
				
			||||||
 | 
					github.com/graphql-go/graphql v0.8.1 h1:p7/Ou/WpmulocJeEx7wjQy611rtXGQaAcXGqanuMMgc=
 | 
				
			||||||
 | 
					github.com/graphql-go/graphql v0.8.1/go.mod h1:nKiHzRM0qopJEwCITUuIsxk9PlVlwIiiI8pnJEhordQ=
 | 
				
			||||||
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
 | 
					github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
 | 
				
			||||||
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
 | 
					github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
 | 
				
			||||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
 | 
					github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										89
									
								
								models/graphql.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								models/graphql.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,89 @@
 | 
				
			|||||||
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/graphql-go/graphql"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewSchema() (graphql.Schema, error) {
 | 
				
			||||||
 | 
						user := graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
 | 
							Name: "User",
 | 
				
			||||||
 | 
							Fields: graphql.Fields{
 | 
				
			||||||
 | 
								"id":     &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
								"name":   &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
								"age":    &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
								"info":   &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
								"price":  &graphql.Field{Type: graphql.Float},
 | 
				
			||||||
 | 
								"avatar": &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						users := &graphql.Field{
 | 
				
			||||||
 | 
							Type: graphql.NewList(user),
 | 
				
			||||||
 | 
							Args: graphql.FieldConfigArgument{
 | 
				
			||||||
 | 
								"id": &graphql.ArgumentConfig{
 | 
				
			||||||
 | 
									Type: graphql.Int,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							Resolve: func(p graphql.ResolveParams) (interface{}, error) {
 | 
				
			||||||
 | 
								fmt.Println("p.Args:", p.Args)
 | 
				
			||||||
 | 
								return []interface{}{
 | 
				
			||||||
 | 
									map[string]interface{}{
 | 
				
			||||||
 | 
										"id":     1,
 | 
				
			||||||
 | 
										"name":   "user1",
 | 
				
			||||||
 | 
										"age":    10,
 | 
				
			||||||
 | 
										"info":   "info",
 | 
				
			||||||
 | 
										"price":  1.1,
 | 
				
			||||||
 | 
										"avatar": "",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									map[string]interface{}{
 | 
				
			||||||
 | 
										"id":     2,
 | 
				
			||||||
 | 
										"name":   "user2",
 | 
				
			||||||
 | 
										"age":    20,
 | 
				
			||||||
 | 
										"info":   "info",
 | 
				
			||||||
 | 
										"price":  2.2,
 | 
				
			||||||
 | 
										"avatar": "",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								}, nil
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						images := &graphql.Field{
 | 
				
			||||||
 | 
							Type: graphql.NewList(graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
 | 
								Name: "Image",
 | 
				
			||||||
 | 
								Fields: graphql.Fields{
 | 
				
			||||||
 | 
									"id":                      &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
									"width":                   &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
									"height":                  &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
									"content":                 &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
									"article_category_top_id": &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
									"praise_count":            &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
									"collect_count":           &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
									"create_time":             &graphql.Field{Type: graphql.DateTime},
 | 
				
			||||||
 | 
									"update_time":             &graphql.Field{Type: graphql.DateTime},
 | 
				
			||||||
 | 
									"user": &graphql.Field{
 | 
				
			||||||
 | 
										Type: graphql.NewObject(graphql.ObjectConfig{
 | 
				
			||||||
 | 
											Name: "User",
 | 
				
			||||||
 | 
											Fields: graphql.Fields{
 | 
				
			||||||
 | 
												"id":       &graphql.Field{Type: graphql.Int},
 | 
				
			||||||
 | 
												"nickname": &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
												"avatar":   &graphql.Field{Type: graphql.String},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										}),
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							})),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						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 {
 | 
				
			||||||
 | 
							return schema, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return schema, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user