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

Fix panic in DefaultResolveFn if uses the string type alias in source key #704

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
17 changes: 16 additions & 1 deletion executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,22 @@ func DefaultResolveFn(p ResolveParams) (interface{}, error) {

// Try accessing as map via reflection
if r := reflect.ValueOf(p.Source); r.Kind() == reflect.Map && r.Type().Key().Kind() == reflect.String {
val := r.MapIndex(reflect.ValueOf(p.Info.FieldName))
fieldNameValue := reflect.ValueOf(p.Info.FieldName)
// The map key type might be a string type alias and its underlying type is string,
// but it will be panic if we try to use it as a string value in `MapIndex`.
// So we need to convert the value of the field name to the map key type before
// using it as a map key.
//
// Related issue: https://github.com/graphql-go/graphql/issues/700
//
// We cannot use `CanConvert` here since we need to be compatible with Go before 1.17.
if fieldNameValue.Kind() != reflect.String {
return nil, nil
}
mapKeyType := r.Type().Key()
fieldNameValue = fieldNameValue.Convert(mapKeyType)

val := r.MapIndex(fieldNameValue)
if val.IsValid() {
property := val.Interface()
if val.Type().Kind() == reflect.Func {
Expand Down
11 changes: 11 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import (
"github.com/graphql-go/graphql/testutil"
)

func TestDefaultResolveFn(t *testing.T) {
type Key string
type Source map[Key]interface{}
source := Source{
"foo": "bar",
}
graphql.DefaultResolveFn(graphql.ResolveParams{
Source: source,
})
}

func TestExecutesArbitraryCode(t *testing.T) {

deepData := map[string]interface{}{}
Expand Down