Skip to content

executor: adds support for anonymous contained structs #371

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
59 changes: 33 additions & 26 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,38 @@ type FieldResolver interface {
Resolve(p ResolveParams) (interface{}, error)
}

func defaultResolveStruct(sourceVal reflect.Value, fieldName string) (interface{}, error) {
for i := 0; i < sourceVal.NumField(); i++ {
valueField := sourceVal.Field(i)
typeField := sourceVal.Type().Field(i)
// try matching the field name first
if strings.EqualFold(typeField.Name, fieldName) {
return valueField.Interface(), nil
}
if typeField.Anonymous && typeField.Type.Kind() == reflect.Struct {
return defaultResolveStruct(valueField, fieldName)

Choose a reason for hiding this comment

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

There's a bug here. We return too early if an anonymous struct comes before a field we're looking for.

I think what we want to do is run through all the top-level fields first, then dive if the field wasn't found.
Repeat for all anonymous structs until it's found. If not found, then return nil, nil.

Choose a reason for hiding this comment

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

Here's a fix, sans a proper test:

abraithwaite@5decd5b

Choose a reason for hiding this comment

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

Good catch, @abraithwaite

}
tag := typeField.Tag
checkTag := func(tagName string) bool {
t := tag.Get(tagName)
tOptions := strings.Split(t, ",")
if len(tOptions) == 0 {
return false
}
if tOptions[0] != fieldName {
return false
}
return true
}
if checkTag("json") || checkTag("graphql") {
return valueField.Interface(), nil
} else {
continue
}
}
return nil, nil
}

// defaultResolveFn If a resolve function is not given, then a default resolve behavior is used
// which takes the property of the source object of the same name as the field
// and returns it as the result, or if it's a function, returns the result
Expand All @@ -922,32 +954,7 @@ func DefaultResolveFn(p ResolveParams) (interface{}, error) {
}

if sourceVal.Type().Kind() == reflect.Struct {
for i := 0; i < sourceVal.NumField(); i++ {
valueField := sourceVal.Field(i)
typeField := sourceVal.Type().Field(i)
// try matching the field name first
if strings.EqualFold(typeField.Name, p.Info.FieldName) {
return valueField.Interface(), nil
}
tag := typeField.Tag
checkTag := func(tagName string) bool {
t := tag.Get(tagName)
tOptions := strings.Split(t, ",")
if len(tOptions) == 0 {
return false
}
if tOptions[0] != p.Info.FieldName {
return false
}
return true
}
if checkTag("json") || checkTag("graphql") {
return valueField.Interface(), nil
} else {
continue
}
}
return nil, nil
return defaultResolveStruct(sourceVal, p.Info.FieldName)
}

// try p.Source as a map[string]interface
Expand Down
84 changes: 83 additions & 1 deletion executor_resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_With

func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_WithJSONTags(t *testing.T) {

// For structs without JSON tags, it will map to upper-cased exported field names
// For structs with JSON tags, it will use those tags as field names
type SubObjectWithJSONTags struct {
OtherField string `json:""`
Str string `json:"str"`
Expand Down Expand Up @@ -264,3 +264,85 @@ func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_With
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
}

func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_WithContainedStruct(t *testing.T) {

// For anonymous contained structs, it will use the field names within
type ContainedObjectWithJSONTags struct {
Str string `json:"str"`
Int int
}

type SubObjectWithContained struct {
ContainedObjectWithJSONTags
AnotherStr string `json:"anotherStr"`
}

schema := testSchema(t, &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "SubObject",
Description: "Maps GraphQL Object `SubObject` to Go struct `SubObjectWithContained`",
Fields: graphql.Fields{
"str": &graphql.Field{Type: graphql.String},
"Int": &graphql.Field{Type: graphql.Int},
},
}),
Args: graphql.FieldConfigArgument{
"aStr": &graphql.ArgumentConfig{Type: graphql.String},
"aInt": &graphql.ArgumentConfig{Type: graphql.Int},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
aStr, _ := p.Args["aStr"].(string)
aInt, _ := p.Args["aInt"].(int)
return &SubObjectWithContained{
ContainedObjectWithJSONTags: ContainedObjectWithJSONTags{
Str: aStr,
Int: aInt,
},
}, nil
},
})

expected := map[string]interface{}{
"test": map[string]interface{}{
"str": "",
"Int": 0,
},
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test { str, Int } }`,
})

if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}

expected = map[string]interface{}{
"test": map[string]interface{}{
"str": "String!",
"Int": 0,
},
}
result = graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test(aStr: "String!") { str, Int } }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}

expected = map[string]interface{}{
"test": map[string]interface{}{
"str": "String!",
"Int": -123,
},
}
result = graphql.Do(graphql.Params{
Schema: schema,
RequestString: `{ test(aInt: -123, aStr: "String!") { str, Int } }`,
})
if !reflect.DeepEqual(expected, result.Data) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result.Data))
}
}