@@ -2,18 +2,13 @@ package graphql_test
22
33import (
44 "encoding/json"
5- "github.com/graphql-go/graphql"
6- "github.com/graphql-go/graphql/testutil"
75 "log"
86 "reflect"
97 "testing"
10- )
118
12- type Human struct {
13- Alive bool `json:"alive"`
14- Age int `json:"age"`
15- Weight float64 `json:"weight"`
16- }
9+ "github.com/graphql-go/graphql"
10+ "github.com/graphql-go/graphql/testutil"
11+ )
1712
1813type Person struct {
1914 Human
@@ -22,6 +17,12 @@ type Person struct {
2217 Friends []Friend `json:"friends"`
2318}
2419
20+ type Human struct {
21+ Alive bool `json:"alive"`
22+ Age int `json:"age"`
23+ Weight float64 `json:"weight"`
24+ }
25+
2526type Friend struct {
2627 Name string `json:"name"`
2728 Address string `json:"address"`
@@ -38,29 +39,29 @@ var personSource = Person{
3839 Weight : 70.1 ,
3940 Alive : true ,
4041 },
41- Name : "John Doe" ,
42- Home : myaddress ,
42+ Name : "John Doe" ,
43+ Home : Address {
44+ Street : "Jl. G1" ,
45+ City : "Jakarta" ,
46+ },
4347 Friends : friendSource ,
4448}
4549
4650var friendSource = []Friend {
47- {"Arief" , "palembang" },
48- {"Al" , "semarang" },
49- }
50-
51- var myaddress = Address {
52- Street : "Jl. G1" ,
53- City : "Jakarta" ,
51+ {Name : "Arief" , Address : "palembang" },
52+ {Name : "Al" , Address : "semarang" },
5453}
5554
5655func TestBindFields (t * testing.T ) {
57- personObj := graphql .NewObject (graphql.ObjectConfig {
58- Name : "Person" ,
56+ // create person type based on Person struct
57+ personType := graphql .NewObject (graphql.ObjectConfig {
58+ Name : "Person" ,
59+ // pass empty Person struct to bind all of it's fields
5960 Fields : graphql .BindFields (Person {}),
6061 })
6162 fields := graphql.Fields {
6263 "person" : & graphql.Field {
63- Type : personObj ,
64+ Type : personType ,
6465 Resolve : func (p graphql.ResolveParams ) (interface {}, error ) {
6566 return personSource , nil
6667 },
@@ -118,6 +119,7 @@ func TestBindArg(t *testing.T) {
118119 fields := graphql.Fields {
119120 "friend" : & graphql.Field {
120121 Type : friendObj ,
122+ //it can be added more than one since it's a slice
121123 Args : graphql .BindArg (Friend {}, "name" ),
122124 Resolve : func (p graphql.ResolveParams ) (interface {}, error ) {
123125 if name , ok := p .Args ["name" ].(string ); ok {
0 commit comments