diff --git a/bin/main.go b/bin/main.go index 1d79f87..ecfdee5 100644 --- a/bin/main.go +++ b/bin/main.go @@ -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) }) diff --git a/models/graphql.go b/models/graphql.go index 6cf0728..6f62991 100644 --- a/models/graphql.go +++ b/models/graphql.go @@ -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{