从 demo 引用
This commit is contained in:
@@ -1,13 +1,31 @@
|
|||||||
package models
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.satori.love/gameui/webp/models"
|
||||||
"github.com/graphql-go/graphql"
|
"github.com/graphql-go/graphql"
|
||||||
"github.com/graphql-go/graphql/language/ast"
|
"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) {
|
func NewSchema() (graphql.Schema, error) {
|
||||||
user := graphql.NewObject(graphql.ObjectConfig{
|
user := graphql.NewObject(graphql.ObjectConfig{
|
||||||
Name: "User",
|
Name: "User",
|
||||||
@@ -42,7 +60,6 @@ func NewSchema() (graphql.Schema, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fields_str := strings.Join(fields, ",")
|
fields_str := strings.Join(fields, ",")
|
||||||
|
|
||||||
var where []string
|
var where []string
|
||||||
if p.Args["id"] != nil {
|
if p.Args["id"] != nil {
|
||||||
where = append(where, fmt.Sprintf("id=%d", p.Args["id"]))
|
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 {
|
if p.Args["user_name"] != nil {
|
||||||
where = append(where, fmt.Sprintf("user_name='%s'", p.Args["user_name"]))
|
where = append(where, fmt.Sprintf("user_name='%s'", p.Args["user_name"]))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 筛选条件
|
// 筛选条件
|
||||||
where_str := strings.Join(where, " AND ")
|
where_str := strings.Join(where, " AND ")
|
||||||
fmt.Println(where_str)
|
fmt.Println(where_str)
|
||||||
@@ -93,52 +109,94 @@ func NewSchema() (graphql.Schema, error) {
|
|||||||
"id": &graphql.ArgumentConfig{
|
"id": &graphql.ArgumentConfig{
|
||||||
Type: graphql.Int,
|
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) {
|
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{}{
|
var images []Image
|
||||||
map[string]interface{}{
|
if err := connection.Select(&images, query.String()); err != nil {
|
||||||
"id": 1,
|
fmt.Println("获取图像列表失败", err)
|
||||||
"width": 100,
|
return nil, err
|
||||||
"height": 100,
|
}
|
||||||
"content": "content",
|
fmt.Println(images)
|
||||||
"article_category_top_id": 1,
|
return images, nil
|
||||||
"praise_count": 1,
|
//return []interface{}{
|
||||||
"collect_count": 1,
|
// map[string]interface{}{
|
||||||
"create_time": "2018-01-01 00:00:00",
|
// "id": 1,
|
||||||
"update_time": "2018-01-01 00:00:00",
|
// "width": 100,
|
||||||
"user": map[string]interface{}{
|
// "height": 100,
|
||||||
"id": 1,
|
// "content": "content",
|
||||||
"name": "user1",
|
// "article_category_top_id": 1,
|
||||||
"age": 10,
|
// "praise_count": 1,
|
||||||
"info": "info",
|
// "collect_count": 1,
|
||||||
"price": 1.1,
|
// "create_time": "2018-01-01 00:00:00",
|
||||||
"avatar": "",
|
// "update_time": "2018-01-01 00:00:00",
|
||||||
},
|
// "user": map[string]interface{}{
|
||||||
},
|
// "id": 1,
|
||||||
map[string]interface{}{
|
// "name": "user1",
|
||||||
"id": 2,
|
// "age": 10,
|
||||||
"width": 100,
|
// "info": "info",
|
||||||
"height": 100,
|
// "price": 1.1,
|
||||||
"content": "content",
|
// "avatar": "",
|
||||||
"article_category_top_id": 1,
|
// },
|
||||||
"praise_count": 1,
|
// },
|
||||||
"collect_count": 1,
|
// map[string]interface{}{
|
||||||
"create_time": "2018-01-01 00:00:00",
|
// "id": 2,
|
||||||
"update_time": "2018-01-01 00:00:00",
|
// "width": 100,
|
||||||
"user": map[string]interface{}{
|
// "height": 100,
|
||||||
"id": 2,
|
// "content": "content",
|
||||||
"name": "user2",
|
// "article_category_top_id": 1,
|
||||||
"age": 20,
|
// "praise_count": 1,
|
||||||
"info": "info",
|
// "collect_count": 1,
|
||||||
"price": 2.2,
|
// "create_time": "2018-01-01 00:00:00",
|
||||||
"avatar": "",
|
// "update_time": "2018-01-01 00:00:00",
|
||||||
},
|
// "user": map[string]interface{}{
|
||||||
},
|
// "id": 2,
|
||||||
}, nil
|
// "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"
|
"encoding/json"
|
||||||
|
|
||||||
|
"git.satori.love/gameui/webp/api"
|
||||||
"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/graphql-go/graphql"
|
||||||
@@ -57,17 +58,18 @@ func LogComponent(startTime int64, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Image struct {
|
type Image struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id" db:"id"`
|
||||||
Width int `json:"width"`
|
Width int `json:"width" db:"width"`
|
||||||
Height int `json:"height"`
|
Height int `json:"height" db:"height"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content" db:"content"`
|
||||||
ArticleCategoryTopId int `json:"article_category_top_id"`
|
ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"`
|
||||||
PraiseCount int `json:"praise_count"`
|
PraiseCount int `json:"praise_count" db:"praise_count"`
|
||||||
CollectCount int `json:"collect_count"`
|
CollectCount int `json:"collect_count" db:"collect_count"`
|
||||||
CreateTime time.Time `json:"createTime"`
|
CreateTime time.Time `json:"createTime" db:"createTime"`
|
||||||
UpdateTime time.Time `json:"updateTime"`
|
UpdateTime time.Time `json:"updateTime" db:"updateTime"`
|
||||||
User models.User `json:"user"`
|
UserID int `json:"user_id" db:"user_id"`
|
||||||
Article models.Article `json:"article"`
|
User models.User `json:"user" db:"user"`
|
||||||
|
Article models.Article `json:"article" db:"article"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
@@ -177,7 +179,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Schema
|
// Schema
|
||||||
schema, err := models.NewSchema()
|
schema, err := api.NewSchema()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to create new schema, error: %v", err)
|
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"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
"git.satori.love/gameui/webp/api"
|
||||||
"github.com/graphql-go/graphql"
|
"github.com/graphql-go/graphql"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
|
||||||
// Schema
|
schema, err := api.NewSchema()
|
||||||
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 {
|
if err != nil {
|
||||||
log.Fatalf("failed to create new schema, error: %v", err)
|
log.Fatalln("创建Schema失败", err)
|
||||||
}
|
}
|
||||||
|
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
query := r.URL.Query().Get("query")
|
query := r.URL.Query().Get("query")
|
||||||
fmt.Println("query:", query)
|
|
||||||
params := graphql.Params{Schema: schema, RequestString: query}
|
params := graphql.Params{Schema: schema, RequestString: query}
|
||||||
result := graphql.Do(params)
|
result := graphql.Do(params)
|
||||||
if len(result.Errors) > 0 {
|
if len(result.Errors) > 0 {
|
||||||
@@ -85,10 +27,9 @@ func main() {
|
|||||||
http.Error(w, result.Errors[0].Error(), 500)
|
http.Error(w, result.Errors[0].Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rJSON, _ := json.Marshal(result)
|
rJSON, _ := json.MarshalIndent(result, "", " ")
|
||||||
fmt.Printf("%s \n", rJSON) // {"data":{"hello":"world"}}
|
|
||||||
w.Write(rJSON)
|
w.Write(rJSON)
|
||||||
})
|
})
|
||||||
|
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":6001", nil)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user