Skip to content

Commit 0beacfd

Browse files
committed
Support custom map types in resolvers (e.g.
`type Thing map[string]interface{}`) by falling back to reflection. Fixes #439.
1 parent 21fe71d commit 0beacfd

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

executor.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,22 @@ func DefaultResolveFn(p ResolveParams) (interface{}, error) {
964964
return property, nil
965965
}
966966

967+
// Try accessing as map via reflection
968+
if r := reflect.ValueOf(p.Source); r.Kind() == reflect.Map && r.Type().Key().Kind() == reflect.String {
969+
val := r.MapIndex(reflect.ValueOf(p.Info.FieldName))
970+
if val.IsValid() {
971+
property := val.Interface()
972+
if val.Type().Kind() == reflect.Func {
973+
// try type casting the func to the most basic func signature
974+
// for more complex signatures, user have to define ResolveFn
975+
if propertyFn, ok := property.(func() interface{}); ok {
976+
return propertyFn(), nil
977+
}
978+
}
979+
return property, nil
980+
}
981+
}
982+
967983
// last resort, return nil
968984
return nil, nil
969985
}

0 commit comments

Comments
 (0)