Refactor GraphQL schema and format JSON output
This commit is contained in:
@@ -197,7 +197,8 @@ func main() {
|
||||
http.Error(w, result.Errors[0].Error(), 500)
|
||||
return
|
||||
}
|
||||
rJSON, _ := json.Marshal(result)
|
||||
// 格式化输出
|
||||
rJSON, _ := json.MarshalIndent(result, "", " ")
|
||||
fmt.Printf("%s \n", rJSON)
|
||||
w.Write(rJSON)
|
||||
})
|
||||
|
@@ -49,31 +49,74 @@ func NewSchema() (graphql.Schema, error) {
|
||||
},
|
||||
}
|
||||
|
||||
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},
|
||||
},
|
||||
}),
|
||||
},
|
||||
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,
|
||||
},
|
||||
},
|
||||
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||
fmt.Println("p.Args:", p.Args)
|
||||
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{
|
||||
|
Reference in New Issue
Block a user