graphql
This commit is contained in:
		
							
								
								
									
										89
									
								
								models/graphql.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								models/graphql.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,89 @@
 | 
			
		||||
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
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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},
 | 
			
		||||
						},
 | 
			
		||||
					}),
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
		})),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user