graphql-go将字段类型定义为对象

时间:2019-01-30 20:26:11

标签: go graphql graphql-go

使用graphql-go如何处理给定api中的嵌套对象会在解析器中返回以下内容

{ id:“ 34”, 名称:“某物”, 区域:{     id:34,     名称:“圣何塞”    } }

我们如何为区域实施:             “区域”:&graphql.Field {                 类型:graphql。??,             }

1 个答案:

答案 0 :(得分:0)

以下是使用SELECT count() FROM default.TestDict /* result ┌─count()─┐ │ 27742 │ └─────────┘ */ 的示例:

github.com/graphql-go/graphql v0.7.9

使用package main import ( "encoding/json" "fmt" "net/http" "github.com/graphql-go/graphql" ) type region struct { ID int `json:"id"` Name string `json:"name"` } type user struct { ID string `json:"id"` Name string `json:"name"` Region region `json:"region"` } var aRegion = region{ID: 34, Name: "San jose"} var aUser = user{ID: "34", Name: "something", Region: aRegion} var regionType = graphql.NewObject(graphql.ObjectConfig{ Name: "Region", Fields: graphql.Fields{ "id": &graphql.Field{Type: graphql.Int}, "name": &graphql.Field{Type: graphql.String}, }, }) var userType = graphql.NewObject(graphql.ObjectConfig{ Name: "User", Fields: graphql.Fields{ "id": &graphql.Field{Type: graphql.String}, "name": &graphql.Field{Type: graphql.String}, "region": &graphql.Field{ Type: regionType, Resolve: func(p graphql.ResolveParams) (interface{}, error) { source, ok := p.Source.(user) fmt.Printf("%#v\n", source) fmt.Println("ok:", ok) return source.Region, nil }, }, }, }) var queryType = graphql.NewObject(graphql.ObjectConfig{ Name: "Query", Fields: graphql.Fields{ "user": &graphql.Field{ Type: userType, Resolve: func(p graphql.ResolveParams) (interface{}, error) { return aUser, nil }, }, }, }) func executeQuery(query string, schema graphql.Schema) *graphql.Result { result := graphql.Do(graphql.Params{ Schema: schema, RequestString: query, }) if len(result.Errors) > 0 { fmt.Printf("wrong result, unexpected errors: %v", result.Errors) } return result } func main() { var schema, _ = graphql.NewSchema(graphql.SchemaConfig{ Query: queryType, }) http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) { result := executeQuery(r.URL.Query().Get("query"), schema) json.NewEncoder(w).Encode(result) }) fmt.Println("Now server is running on port 8080") fmt.Println("Access the web app via browser at 'http://localhost:8080'") http.ListenAndServe(":8080", nil) } 发送GraphQL请求:

curl

服务器日志:

~ curl -g 'http://localhost:8080/graphql?query={user{id,name,region{id,name}}}'
{"data":{"user":{"id":"34","name":"something","region":{"id":34,"name":"San jose"}}}}