package models import ( "fmt" "github.com/graphql-go/graphql" ) func NewSchema() (graphql.Schema, error) { user := 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}, "avatar": &graphql.Field{Type: graphql.String}, }, }) users := &graphql.Field{ Type: graphql.NewList(user), 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, "avatar": "", }, map[string]interface{}{ "id": 2, "name": "user2", "age": 20, "info": "info", "price": 2.2, "avatar": "", }, }, nil }, } 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{ "users": users, "images": images, }} schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)} schema, err := graphql.NewSchema(schemaConfig) if err != nil { return schema, err } return schema, nil }