Skip to content

Commit

Permalink
Merge branch 'master' into make-validationrule-extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon committed Aug 17, 2020
2 parents 64d8171 + d6b7434 commit 3e0d192
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
if gt.err = invariantf(
fieldConfig.Type != nil,
`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type,
); err != nil {
); gt.err != nil {
return resultFieldMap
}
field := &InputObjectField{}
Expand Down
22 changes: 19 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package graphql

import (
"encoding"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -40,7 +41,13 @@ func BindFields(obj interface{}) Fields {

var graphType Output
if fieldType.Kind() == reflect.Struct {
structFields := BindFields(v.Field(i).Interface())
itf := v.Field(i).Interface()
if _, ok := itf.(encoding.TextMarshaler); ok {
fieldType = reflect.TypeOf("")
goto nonStruct
}

structFields := BindFields(itf)

if tag == "" {
fields = appendFields(fields, structFields)
Expand All @@ -53,6 +60,7 @@ func BindFields(obj interface{}) Fields {
}
}

nonStruct:
if tag == "" {
continue
}
Expand Down Expand Up @@ -122,14 +130,22 @@ func extractValue(originTag string, obj interface{}) interface{} {

for j := 0; j < val.NumField(); j++ {
field := val.Type().Field(j)
found := originTag == extractTag(field.Tag)
if field.Type.Kind() == reflect.Struct {
res := extractValue(originTag, val.Field(j).Interface())
itf := val.Field(j).Interface()

if str, ok := itf.(encoding.TextMarshaler); ok && found {
byt, _ := str.MarshalText()
return string(byt)
}

res := extractValue(originTag, itf)
if res != nil {
return res
}
}

if originTag == extractTag(field.Tag) {
if found {
return reflect.Indirect(val.Field(j)).Interface()
}
}
Expand Down
10 changes: 7 additions & 3 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"reflect"
"testing"
"time"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
Expand All @@ -19,9 +20,10 @@ type Person struct {
}

type Human struct {
Alive bool `json:"alive,omitempty"`
Age int `json:"age"`
Weight float64 `json:"weight"`
Alive bool `json:"alive,omitempty"`
Age int `json:"age"`
Weight float64 `json:"weight"`
DoB time.Time `json:"dob"`
}

type Friend struct {
Expand All @@ -40,6 +42,7 @@ var personSource = Person{
Age: 24,
Weight: 70.1,
Alive: true,
DoB: time.Date(2019, 01, 01, 01, 01, 01, 0, time.UTC),
},
Name: "John Doe",
Home: Address{
Expand Down Expand Up @@ -82,6 +85,7 @@ func TestBindFields(t *testing.T) {
{
person{
name,
dob,
home{street,city},
friends{name,address},
age,
Expand Down

0 comments on commit 3e0d192

Please sign in to comment.