读芯术|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}
第三 , 创建一个书目文件夹 , 创建如下文件:
读芯术|Facebook:如何在Golang中搭建GraphQL?
文章图片
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,},)


推荐阅读