Skip to content

Commit 91784cc

Browse files
committed
cleanup and comments
1 parent 575ab88 commit 91784cc

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

util.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package graphql
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"reflect"
76
"strings"
87
)
98

109
const TAG = "json"
1110

11+
// can't take recursive slice type
12+
// e.g
13+
// type Person struct{
14+
// Friends []Person
15+
// }
16+
// it will throw panic stack-overflow
1217
func BindFields(obj interface{}) Fields {
1318
v := reflect.ValueOf(obj)
1419
fields := make(map[string]*Field)
@@ -23,13 +28,11 @@ func BindFields(obj interface{}) Fields {
2328
var graphType Output
2429
if typeField.Type.Kind() == reflect.Struct {
2530

31+
structFields := BindFields(v.Field(i).Interface())
2632
if tag == "" {
27-
structFields := BindFields(v.Field(i).Interface())
2833
fields = appendFields(fields, structFields)
2934
continue
3035
} else {
31-
structFields := BindFields(v.Field(i).Interface())
32-
3336
graphType = NewObject(ObjectConfig{
3437
Name: tag,
3538
Fields: structFields,
@@ -88,6 +91,7 @@ func getGraphList(tipe reflect.Type) *List {
8891
return NewList(String)
8992
}
9093

94+
// finaly bind object
9195
t := reflect.New(tipe.Elem())
9296
name := strings.Replace(fmt.Sprint(tipe.Elem()), ".", "_", -1)
9397
obj := NewObject(ObjectConfig{
@@ -140,18 +144,6 @@ func BindArg(obj interface{}, tags ...string) FieldConfigArgument {
140144
return config
141145
}
142146

143-
func UnmarshalArgs(args map[string]interface{}, pointer interface{}) error {
144-
js, err := json.Marshal(args)
145-
if err != nil {
146-
return fmt.Errorf("ini error 1 %v", err)
147-
}
148-
err = json.Unmarshal(js, pointer)
149-
if err != nil {
150-
return fmt.Errorf("ini error 2 %v", err)
151-
}
152-
return nil
153-
}
154-
155147
func inArray(slice interface{}, item interface{}) bool {
156148
s := reflect.ValueOf(slice)
157149
if s.Kind() != reflect.Slice {

util_test.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@ package graphql_test
22

33
import (
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

1813
type 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+
2526
type 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

4650
var 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

5655
func 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

Comments
 (0)