Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for embedded struct type in resolver #338

Merged
merged 8 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions example/social/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,19 @@ func (r *searchResult) ToUser() (*user, bool) {
return res, ok
}

type contact struct {
Email string
Phone string
}

type user struct {
IDField string
NameField string
RoleField string
Email string
Phone string
Address *[]string
Friends *[]*user
CreatedAt graphql.Time
contact
}

func (u user) ID() graphql.ID {
Expand Down Expand Up @@ -126,37 +130,45 @@ var users = []*user{
IDField: "0x01",
NameField: "Albus Dumbledore",
RoleField: "ADMIN",
Email: "Albus@hogwarts.com",
Phone: "000-000-0000",
Address: &[]string{"Office @ Hogwarts", "where Horcruxes are"},
CreatedAt: graphql.Time{Time: time.Now()},
contact: contact{
Email: "Albus@hogwarts.com",
Phone: "000-000-0000",
},
},
{
IDField: "0x02",
NameField: "Harry Potter",
RoleField: "USER",
Email: "harry@hogwarts.com",
Phone: "000-000-0001",
Address: &[]string{"123 dorm room @ Hogwarts", "456 random place"},
CreatedAt: graphql.Time{Time: time.Now()},
contact: contact{
Email: "harry@hogwarts.com",
Phone: "000-000-0001",
},
},
{
IDField: "0x03",
NameField: "Hermione Granger",
RoleField: "USER",
Email: "hermione@hogwarts.com",
Phone: "000-000-0011",
Address: &[]string{"233 dorm room @ Hogwarts", "786 @ random place"},
CreatedAt: graphql.Time{Time: time.Now()},
contact: contact{
Email: "hermione@hogwarts.com",
Phone: "000-000-0011",
},
},
{
IDField: "0x04",
NameField: "Ronald Weasley",
RoleField: "USER",
Email: "ronald@hogwarts.com",
Phone: "000-000-0111",
Address: &[]string{"411 dorm room @ Hogwarts", "981 @ random place"},
CreatedAt: graphql.Time{Time: time.Now()},
contact: contact{
Email: "ronald@hogwarts.com",
Phone: "000-000-0111",
},
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
if res.Kind() == reflect.Ptr {
res = res.Elem()
}
result = res.Field(f.field.FieldIndex)
result = res.FieldByIndex(f.field.FieldIndex)
}
return nil
}()
Expand Down
59 changes: 48 additions & 11 deletions internal/exec/resolvable/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Field struct {
schema.Field
TypeName string
MethodIndex int
FieldIndex int
FieldIndex []int
HasContext bool
HasError bool
ArgsPacker *packer.StructPacker
Expand All @@ -43,7 +43,7 @@ type Field struct {
}

func (f *Field) UseMethodResolver() bool {
return f.FieldIndex == -1
return len(f.FieldIndex) == 0
}

type TypeAssertion struct {
Expand Down Expand Up @@ -228,13 +228,17 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p

Fields := make(map[string]*Field)
rt := unwrapPtr(resolverType)
fieldsCount := getFieldCount(rt, map[string]int{})
for _, f := range fields {
fieldIndex := -1
var fieldIndex []int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the field index is an array? I don't get it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is an array because it is an index sequence for Type.FieldByIndex.

It allows for several layers of anonymously-nested fields.

methodIndex := findMethod(resolverType, f.Name)
if b.schema.UseFieldResolvers && methodIndex == -1 {
fieldIndex = findField(rt, f.Name)
if fieldsCount[strings.ToLower(stripUnderscore(f.Name))] > 1 {
return nil, fmt.Errorf("%s does not resolve %q: ambiguous field %q", resolverType, typeName, f.Name)
}
eloyekunle marked this conversation as resolved.
Show resolved Hide resolved
fieldIndex = findField(rt, f.Name, []int{})
}
if methodIndex == -1 && fieldIndex == -1 {
if methodIndex == -1 && len(fieldIndex) == 0 {
hint := ""
if findMethod(reflect.PtrTo(resolverType), f.Name) != -1 {
hint = " (hint: the method exists on the pointer type)"
Expand All @@ -247,7 +251,7 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p
if methodIndex != -1 {
m = resolverType.Method(methodIndex)
} else {
sf = rt.Field(fieldIndex)
sf = rt.FieldByIndex(fieldIndex)
}
fe, err := b.makeFieldExec(typeName, f, m, sf, methodIndex, fieldIndex, methodHasReceiver)
if err != nil {
Expand Down Expand Up @@ -290,7 +294,7 @@ var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
var errorType = reflect.TypeOf((*error)(nil)).Elem()

func (b *execBuilder) makeFieldExec(typeName string, f *schema.Field, m reflect.Method, sf reflect.StructField,
methodIndex, fieldIndex int, methodHasReceiver bool) (*Field, error) {
methodIndex int, fieldIndex []int, methodHasReceiver bool) (*Field, error) {

var argsPacker *packer.StructPacker
var hasError bool
Expand Down Expand Up @@ -380,13 +384,46 @@ func findMethod(t reflect.Type, name string) int {
return -1
}

func findField(t reflect.Type, name string) int {
func findField(t reflect.Type, name string, index []int) []int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this method need an integer array? , index []int?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function builds the index sequence for Type.FieldByIndex recursively, returning the sequence if found and an empty array otherwise.

for i := 0; i < t.NumField(); i++ {
if strings.EqualFold(stripUnderscore(name), stripUnderscore(t.Field(i).Name)) {
return i
field := t.Field(i)

if field.Type.Kind() == reflect.Struct && field.Anonymous {
newIndex := findField(field.Type, name, []int{i})
if len(newIndex) > 1 {
return append(index, newIndex...)
eloyekunle marked this conversation as resolved.
Show resolved Hide resolved
}
}

if strings.EqualFold(stripUnderscore(name), stripUnderscore(field.Name)) {
return append(index, i)
}
}
return -1

return index
}

// getFieldCount helps resolve ambiguity when more than one embedded struct contains fields with the same name.
eloyekunle marked this conversation as resolved.
Show resolved Hide resolved
func getFieldCount(t reflect.Type, count map[string]int) map[string]int {
if t.Kind() != reflect.Struct {
return nil
}

for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldName := strings.ToLower(stripUnderscore(field.Name))

if field.Type.Kind() == reflect.Struct && field.Anonymous {
count = getFieldCount(field.Type, count)
} else {
if _, ok := count[fieldName]; !ok {
count[fieldName] = 0
}
count[fieldName]++
}
}

return count
}

func unwrapNonNull(t common.Type) (common.Type, bool) {
Expand Down