读芯术|Facebook:如何在Golang中搭建GraphQL?( 三 )
model.go
packageinfrastructuretypeappstruct{Appnamestring`yaml:"name"`Debugbool`yaml:"debug"`Portstring`yaml:"port"`Servicestring`yaml:"service"`Hoststring`yaml:"host"`}typedatabasestruct{Namestring`yaml:"name"`Connectionstring`yaml:"connection"`}typeEnvironmentstruct{Appapp`yaml:"app"`Databasesmap[string]database`yaml:"databases"`pathstring}
第三 , 创建一个书目文件夹 , 创建如下文件:
文章图片
model.go
packagepackagebooktypeBookstruct{NamestringPricestringDescriptionstring}booktypeBookstruct{NamestringPricestringDescriptionstring}
resolver.go
packagebookimport("context""github.com/graphql-go/graphql")varproductType=graphql.NewObject(graphql.ObjectConfig{Name:"Book",Fields:graphql.Fields{"name":&graphql.Field{Type:graphql.String,},"price":&graphql.Field{Type:graphql.String,},"description":&graphql.Field{Type:graphql.String,},},},)varqueryType=graphql.NewObject(graphql.ObjectConfig{Name:"Query",Fields:graphql.Fields{"book":&graphql.Field{Type:productType,Description:"Getbookbyname",Args:graphql.FieldConfigArgument{"name":&graphql.ArgumentConfig{Type:graphql.String,},},Resolve:func(pgraphql.ResolveParams)(interface{},error){varresultinterface{}name,ok:=p.Args["name"].(string)ifok{//Findproductresult=GetBookByName(context.Background(),name)}returnresult,nil},},"list":&graphql.Field{Type:graphql.NewList(productType),Description:"Getbooklist",Args:graphql.FieldConfigArgument{"limit":&graphql.ArgumentConfig{Type:graphql.Int,},},Resolve:func(paramsgraphql.ResolveParams)(interface{},error){varresultinterface{}limit,_:=params.Args["limit"].(int)result=GetBookList(context.Background(),limit)returnresult,nil},},},})varmutationType=graphql.NewObject(graphql.ObjectConfig{Name:"Mutation",Fields:graphql.Fields{"create":&graphql.Field{Type:productType,Description:"Createnewbook",Args:graphql.FieldConfigArgument{"name":&graphql.ArgumentConfig{Type:graphql.NewNonNull(graphql.String),},"price":&graphql.ArgumentConfig{Type:graphql.NewNonNull(graphql.String),},"description":&graphql.ArgumentConfig{Type:graphql.NewNonNull(graphql.String),},},Resolve:func(paramsgraphql.ResolveParams)(interface{},error){book:=Book{Name:params.Args["name"].(string),Price:params.Args["price"].(string),Description:params.Args["description"].(string),}iferr:=InsertBook(context.Background(),book);err!=nil{returnnil,err}returnbook,nil},},"update":&graphql.Field{Type:productType,Description:"Updatebookbyname",Args:graphql.FieldConfigArgument{"name":&graphql.ArgumentConfig{Type:graphql.NewNonNull(graphql.String),},"price":&graphql.ArgumentConfig{Type:graphql.String,},"description":&graphql.ArgumentConfig{Type:graphql.String,},},Resolve:func(paramsgraphql.ResolveParams)(interface{},error){book:=Book{}ifname,nameOk:=params.Args["name"].(string);nameOk{book.Name=name}ifprice,priceOk:=params.Args["price"].(string);priceOk{book.Price=price}ifdescription,descriptionOk:=params.Args["description"].(string);descriptionOk{book.Description=description}iferr:=UpdateBook(context.Background(),book);err!=nil{returnnil,err}returnbook,nil},},"delete":&graphql.Field{Type:productType,Description:"Deletebookbyname",Args:graphql.FieldConfigArgument{"name":&graphql.ArgumentConfig{Type:graphql.NewNonNull(graphql.String),},},Resolve:func(paramsgraphql.ResolveParams)(interface{},error){name,_:=params.Args["name"].(string)iferr:=DeleteBook(context.Background(),name);err!=nil{returnnil,err}returnname,nil},},},})//schemavarSchema,_=graphql.NewSchema(graphql.SchemaConfig{Query:queryType,Mutation:mutationType,},)
推荐阅读
- Facebook向客户发邮件:对苹果隐私新规“别无选择”
- Facebook智能眼镜有望2021年上市 AR叠加体验或缺席
- 在Galaxy Buds Pro正式发布前 有人已开始在Facebook上出售这款产品
- 不再是特例:Facebook将在平台上禁掉乔治亚州的政治广告
- Facebook Messenger收集的数据量有多吓人?可视化对比图告诉你
- 一箱饮料在线下卖60元,为何在网上只卖30元?商家道出了实情
- 为何在人工智能研发领域Python应用比较多
- 声网 X Watch Party 如何在线上一起欢快的边看电影边吐槽
- Facebook警告欧洲ePrivacy指令将可能让用户置于危险境地
- 受欧盟新隐私规定影响:Facebook关闭儿童虐待检测工具