214 lines
6.6 KiB
Go
214 lines
6.6 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"git.satori.love/gameui/webp/models"
|
|
"github.com/graphql-go/graphql"
|
|
"github.com/graphql-go/graphql/language/ast"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
var connection *sqlx.DB
|
|
|
|
func init() {
|
|
var err error
|
|
user := models.Viper.Get("mysql.user").(string)
|
|
password := models.Viper.Get("mysql.password").(string)
|
|
host := models.Viper.Get("mysql.host").(string)
|
|
port := models.Viper.Get("mysql.port").(int)
|
|
database := models.Viper.Get("mysql.database").(string)
|
|
connection, err = sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database))
|
|
if err != nil {
|
|
log.Fatalln("连接数据库失败", err)
|
|
}
|
|
}
|
|
|
|
func NewSchema() (graphql.Schema, error) {
|
|
user := graphql.NewObject(graphql.ObjectConfig{
|
|
Name: "User",
|
|
Fields: graphql.Fields{
|
|
"id": &graphql.Field{Type: graphql.Int},
|
|
"user_name": &graphql.Field{Type: graphql.String},
|
|
"avatar": &graphql.Field{Type: graphql.String},
|
|
"rank": &graphql.Field{Type: graphql.String},
|
|
"price": &graphql.Field{Type: graphql.Float},
|
|
"create_time": &graphql.Field{Type: graphql.DateTime},
|
|
"update_time": &graphql.Field{Type: graphql.DateTime},
|
|
},
|
|
})
|
|
|
|
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{
|
|
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: user,
|
|
},
|
|
},
|
|
})
|
|
|
|
images := &graphql.Field{
|
|
Type: graphql.NewList(image),
|
|
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,
|
|
},
|
|
},
|
|
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["width"] != nil {
|
|
where = append(where, fmt.Sprintf("width='%s'", p.Args["width"]))
|
|
}
|
|
if p.Args["height"] != nil {
|
|
where = append(where, fmt.Sprintf("height='%s'", p.Args["height"]))
|
|
}
|
|
if p.Args["content"] != nil {
|
|
where = append(where, fmt.Sprintf("content='%s'", p.Args["content"]))
|
|
}
|
|
// 筛选条件
|
|
where_str := strings.Join(where, " AND ")
|
|
fmt.Println(where_str)
|
|
|
|
var query strings.Builder
|
|
query.WriteString(fmt.Sprintf("SELECT %s FROM web_images WHERE %s LIMIT %s", fields_str, where_str, "10"))
|
|
fmt.Println(query.String())
|
|
|
|
var images []Image
|
|
if err := connection.Select(&images, query.String()); err != nil {
|
|
fmt.Println("获取图像列表失败", err)
|
|
return nil, err
|
|
}
|
|
fmt.Println(images)
|
|
return images, nil
|
|
//return []interface{}{
|
|
// map[string]interface{}{
|
|
// "id": 1,
|
|
// "width": 100,
|
|
// "height": 100,
|
|
// "content": "content",
|
|
// "article_category_top_id": 1,
|
|
// "praise_count": 1,
|
|
// "collect_count": 1,
|
|
// "create_time": "2018-01-01 00:00:00",
|
|
// "update_time": "2018-01-01 00:00:00",
|
|
// "user": map[string]interface{}{
|
|
// "id": 1,
|
|
// "name": "user1",
|
|
// "age": 10,
|
|
// "info": "info",
|
|
// "price": 1.1,
|
|
// "avatar": "",
|
|
// },
|
|
// },
|
|
// map[string]interface{}{
|
|
// "id": 2,
|
|
// "width": 100,
|
|
// "height": 100,
|
|
// "content": "content",
|
|
// "article_category_top_id": 1,
|
|
// "praise_count": 1,
|
|
// "collect_count": 1,
|
|
// "create_time": "2018-01-01 00:00:00",
|
|
// "update_time": "2018-01-01 00:00:00",
|
|
// "user": map[string]interface{}{
|
|
// "id": 2,
|
|
// "name": "user2",
|
|
// "age": 20,
|
|
// "info": "info",
|
|
// "price": 2.2,
|
|
// "avatar": "",
|
|
// },
|
|
// },
|
|
//}, 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 {
|
|
return schema, err
|
|
}
|
|
return schema, nil
|
|
}
|