从 demo 引用
This commit is contained in:
@@ -1,13 +1,31 @@
|
||||
package models
|
||||
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",
|
||||
@@ -42,7 +60,6 @@ func NewSchema() (graphql.Schema, error) {
|
||||
}
|
||||
}
|
||||
fields_str := strings.Join(fields, ",")
|
||||
|
||||
var where []string
|
||||
if p.Args["id"] != nil {
|
||||
where = append(where, fmt.Sprintf("id=%d", p.Args["id"]))
|
||||
@@ -50,7 +67,6 @@ func NewSchema() (graphql.Schema, error) {
|
||||
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)
|
||||
@@ -93,52 +109,94 @@ func NewSchema() (graphql.Schema, error) {
|
||||
"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) {
|
||||
fmt.Println("p.Args:", p.Args)
|
||||
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)
|
||||
|
||||
//connection.Query("SELECT ")
|
||||
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())
|
||||
|
||||
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
|
||||
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
|
||||
},
|
||||
}
|
||||
|
31
api/struct.go
Normal file
31
api/struct.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.satori.love/gameui/webp/models"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
Id int `json:"id" db:"id"`
|
||||
Width int `json:"width" db:"width"`
|
||||
Height int `json:"height" db:"height"`
|
||||
Content string `json:"content" db:"content"`
|
||||
ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"`
|
||||
PraiseCount int `json:"praise_count" db:"praise_count"`
|
||||
CollectCount int `json:"collect_count" db:"collect_count"`
|
||||
UserID int `json:"user_id" db:"user_id"`
|
||||
User models.User `json:"user" db:"user"`
|
||||
Article models.Article `json:"article" db:"article"`
|
||||
CreateTime time.Time `json:"createTime" db:"createTime"`
|
||||
UpdateTime time.Time `json:"updateTime" db:"updateTime"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int `json:"id" db:"id"`
|
||||
UserName string `json:"user_name" db:"user_name"`
|
||||
Avatar string `json:"avatar" db:"avatar"`
|
||||
Rank string `json:"rank" db:"rank"`
|
||||
CreateTime time.Time `json:"create_time" db:"create_time"`
|
||||
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
||||
}
|
26
bin/main.go
26
bin/main.go
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"encoding/json"
|
||||
|
||||
"git.satori.love/gameui/webp/api"
|
||||
"git.satori.love/gameui/webp/models"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/graphql-go/graphql"
|
||||
@@ -57,17 +58,18 @@ func LogComponent(startTime int64, r *http.Request) {
|
||||
}
|
||||
|
||||
type Image struct {
|
||||
Id int `json:"id"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Content string `json:"content"`
|
||||
ArticleCategoryTopId int `json:"article_category_top_id"`
|
||||
PraiseCount int `json:"praise_count"`
|
||||
CollectCount int `json:"collect_count"`
|
||||
CreateTime time.Time `json:"createTime"`
|
||||
UpdateTime time.Time `json:"updateTime"`
|
||||
User models.User `json:"user"`
|
||||
Article models.Article `json:"article"`
|
||||
Id int `json:"id" db:"id"`
|
||||
Width int `json:"width" db:"width"`
|
||||
Height int `json:"height" db:"height"`
|
||||
Content string `json:"content" db:"content"`
|
||||
ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"`
|
||||
PraiseCount int `json:"praise_count" db:"praise_count"`
|
||||
CollectCount int `json:"collect_count" db:"collect_count"`
|
||||
CreateTime time.Time `json:"createTime" db:"createTime"`
|
||||
UpdateTime time.Time `json:"updateTime" db:"updateTime"`
|
||||
UserID int `json:"user_id" db:"user_id"`
|
||||
User models.User `json:"user" db:"user"`
|
||||
Article models.Article `json:"article" db:"article"`
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
@@ -177,7 +179,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Schema
|
||||
schema, err := models.NewSchema()
|
||||
schema, err := api.NewSchema()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create new schema, error: %v", err)
|
||||
}
|
||||
|
71
demo/main.go
71
demo/main.go
@@ -7,77 +7,19 @@ import (
|
||||
"net/http"
|
||||
"runtime"
|
||||
|
||||
"git.satori.love/gameui/webp/api"
|
||||
"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)
|
||||
schema, err := api.NewSchema()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create new schema, error: %v", err)
|
||||
log.Fatalln("创建Schema失败", err)
|
||||
}
|
||||
|
||||
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.HandleFunc("/api", 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 {
|
||||
@@ -85,10 +27,9 @@ func main() {
|
||||
http.Error(w, result.Errors[0].Error(), 500)
|
||||
return
|
||||
}
|
||||
rJSON, _ := json.Marshal(result)
|
||||
fmt.Printf("%s \n", rJSON) // {"data":{"hello":"world"}}
|
||||
rJSON, _ := json.MarshalIndent(result, "", " ")
|
||||
w.Write(rJSON)
|
||||
})
|
||||
|
||||
http.ListenAndServe(":8080", nil)
|
||||
http.ListenAndServe(":6001", nil)
|
||||
}
|
||||
|
Reference in New Issue
Block a user