-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure default resolver can traverse embedded structs (#2341)
Signed-off-by: James Phillips <[email protected]>
- Loading branch information
1 parent
eaa6196
commit c07579c
Showing
5 changed files
with
118 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package graphql | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDefaultResolver(t *testing.T) { | ||
type meta struct { | ||
Name string `json:"firstname"` | ||
} | ||
|
||
type animal struct { | ||
meta | ||
Age int | ||
} | ||
|
||
fren := animal{meta: meta{Name: "bob"}, Age: 10} | ||
|
||
testCases := []struct { | ||
desc string | ||
source interface{} | ||
field string | ||
out interface{} | ||
}{ | ||
{ | ||
desc: "field on struct", | ||
source: fren, | ||
field: "name", | ||
out: "bob", | ||
}, | ||
{ | ||
desc: "second field on struct", | ||
source: fren, | ||
field: "age", | ||
out: 10, | ||
}, | ||
{ | ||
desc: "field on struct w/ tag", | ||
source: fren, | ||
field: "firstname", | ||
out: "bob", | ||
}, | ||
{ | ||
desc: "missing field on struct", | ||
source: fren, | ||
field: "surname", | ||
out: nil, | ||
}, | ||
{ | ||
desc: "field on map", | ||
source: map[string]interface{}{"name": "bob"}, | ||
field: "name", | ||
out: "bob", | ||
}, | ||
{ | ||
desc: "missing field on map", | ||
source: map[string]interface{}{"name": "bob"}, | ||
field: "firstname", | ||
out: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
result, err := DefaultResolver(tc.source, tc.field) | ||
require.NoError(t, err) | ||
assert.EqualValues(t, result, tc.out) | ||
}) | ||
} | ||
} |