diff --git a/api/graphql.go b/api/graphql.go index c50211e..27c8314 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -27,19 +27,6 @@ func NewSchema(config Config) (graphql.Schema, error) { log.Fatalln("连接数据库失败", err) } - // 文章的可选字段 - article := graphql.NewObject(graphql.ObjectConfig{ - Name: "Article", - Description: "文章信息", - Fields: graphql.Fields{ - "id": &graphql.Field{Type: graphql.Int, Description: "文章ID"}, - "title": &graphql.Field{Type: graphql.String, Description: "文章标题"}, - "tags": &graphql.Field{Type: graphql.String, Description: "文章标签"}, - "create_time": &graphql.Field{Type: graphql.DateTime, Description: "文章创建时间"}, - "update_time": &graphql.Field{Type: graphql.DateTime, Description: "文章更新时间"}, - }, - }) - // 用户的可选字段 user := graphql.NewObject(graphql.ObjectConfig{ Name: "User", @@ -55,6 +42,20 @@ func NewSchema(config Config) (graphql.Schema, error) { }, }) + // 文章的可选字段 + article := graphql.NewObject(graphql.ObjectConfig{ + Name: "Article", + Description: "文章信息", + Fields: graphql.Fields{ + "id": &graphql.Field{Type: graphql.Int, Description: "文章ID"}, + "title": &graphql.Field{Type: graphql.String, Description: "文章标题"}, + "tags": &graphql.Field{Type: graphql.String, Description: "文章标签"}, + "user": &graphql.Field{Type: user, Description: "文章所属用户"}, + "create_time": &graphql.Field{Type: graphql.DateTime, Description: "文章创建时间"}, + "update_time": &graphql.Field{Type: graphql.DateTime, Description: "文章更新时间"}, + }, + }) + // 图像中的文字提取 text := graphql.NewObject(graphql.ObjectConfig{ Name: "Text", @@ -141,7 +142,6 @@ func NewSchema(config Config) (graphql.Schema, error) { "before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"}, }, Resolve: func(p graphql.ResolveParams) (interface{}, error) { - fields := strings.Join(get_fields(p.Info.FieldASTs[0].SelectionSet.Selections), ",") var where []string if p.Args["id"] != nil { where = append(where, fmt.Sprintf("id=%d", p.Args["id"])) @@ -159,6 +159,7 @@ func NewSchema(config Config) (graphql.Schema, error) { var users []User var total int + fields := strings.Join(get_fields(p.Info.FieldASTs[0].SelectionSet.Selections), ",") query.WriteString(fmt.Sprintf("SELECT %s FROM web_member %s LIMIT %d OFFSET %d", fields, where_str, 10, 0)) if err := connection.Select(&users, query.String()); err != nil { fmt.Println("获取用户列表失败", err)