Skip to content

Commit 3e0d192

Browse files
authored
Merge branch 'master' into make-validationrule-extensible
2 parents 64d8171 + d6b7434 commit 3e0d192

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
11431143
if gt.err = invariantf(
11441144
fieldConfig.Type != nil,
11451145
`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type,
1146-
); err != nil {
1146+
); gt.err != nil {
11471147
return resultFieldMap
11481148
}
11491149
field := &InputObjectField{}

util.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql
22

33
import (
4+
"encoding"
45
"fmt"
56
"reflect"
67
"strings"
@@ -40,7 +41,13 @@ func BindFields(obj interface{}) Fields {
4041

4142
var graphType Output
4243
if fieldType.Kind() == reflect.Struct {
43-
structFields := BindFields(v.Field(i).Interface())
44+
itf := v.Field(i).Interface()
45+
if _, ok := itf.(encoding.TextMarshaler); ok {
46+
fieldType = reflect.TypeOf("")
47+
goto nonStruct
48+
}
49+
50+
structFields := BindFields(itf)
4451

4552
if tag == "" {
4653
fields = appendFields(fields, structFields)
@@ -53,6 +60,7 @@ func BindFields(obj interface{}) Fields {
5360
}
5461
}
5562

63+
nonStruct:
5664
if tag == "" {
5765
continue
5866
}
@@ -122,14 +130,22 @@ func extractValue(originTag string, obj interface{}) interface{} {
122130

123131
for j := 0; j < val.NumField(); j++ {
124132
field := val.Type().Field(j)
133+
found := originTag == extractTag(field.Tag)
125134
if field.Type.Kind() == reflect.Struct {
126-
res := extractValue(originTag, val.Field(j).Interface())
135+
itf := val.Field(j).Interface()
136+
137+
if str, ok := itf.(encoding.TextMarshaler); ok && found {
138+
byt, _ := str.MarshalText()
139+
return string(byt)
140+
}
141+
142+
res := extractValue(originTag, itf)
127143
if res != nil {
128144
return res
129145
}
130146
}
131147

132-
if originTag == extractTag(field.Tag) {
148+
if found {
133149
return reflect.Indirect(val.Field(j)).Interface()
134150
}
135151
}

util_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"reflect"
77
"testing"
8+
"time"
89

910
"github.com/graphql-go/graphql"
1011
"github.com/graphql-go/graphql/testutil"
@@ -19,9 +20,10 @@ type Person struct {
1920
}
2021

2122
type Human struct {
22-
Alive bool `json:"alive,omitempty"`
23-
Age int `json:"age"`
24-
Weight float64 `json:"weight"`
23+
Alive bool `json:"alive,omitempty"`
24+
Age int `json:"age"`
25+
Weight float64 `json:"weight"`
26+
DoB time.Time `json:"dob"`
2527
}
2628

2729
type Friend struct {
@@ -40,6 +42,7 @@ var personSource = Person{
4042
Age: 24,
4143
Weight: 70.1,
4244
Alive: true,
45+
DoB: time.Date(2019, 01, 01, 01, 01, 01, 0, time.UTC),
4346
},
4447
Name: "John Doe",
4548
Home: Address{
@@ -82,6 +85,7 @@ func TestBindFields(t *testing.T) {
8285
{
8386
person{
8487
name,
88+
dob,
8589
home{street,city},
8690
friends{name,address},
8791
age,

0 commit comments

Comments
 (0)