Skip to content
Closed
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
2 changes: 1 addition & 1 deletion executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ 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))
val := r.MapIndex(reflect.ValueOf(p.Info.FieldName).Convert(r.Type().Key()))
if val.IsValid() {
property := val.Interface()
if val.Type().Kind() == reflect.Func {
Expand Down
118 changes: 76 additions & 42 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,59 +297,93 @@ func TestMergesParallelFragments(t *testing.T) {
}
}

type CustomMap map[string]interface{}
type (
CustomMap map[string]interface{}

CustomKey string
CustomMap2 map[CustomKey]interface{}
)

func TestCustomMapType(t *testing.T) {
query := `
query Example { data { a } }
`
data := CustomMap{
"a": "1",
"b": "2",

scenarios := []struct {
query string
data interface{}
expected interface{}
}{
{
query: query,
data: CustomMap{
"a": "1",
"b": "2",
},
expected: map[string]interface{}{
"data": map[string]interface{}{
"a": "1",
},
},
},
{
query: query,
data: CustomMap2{
"a": "1",
"b": "2",
},
expected: map[string]interface{}{
"data": map[string]interface{}{
"a": "1",
},
},
},
}
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"data": &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "Data",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
"b": &graphql.Field{
Type: graphql.String,

for i, s := range scenarios {
s := s

t.Run(fmt.Sprintf("Scenario #%d", i), func(t *testing.T) {
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"data": &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "Data",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
"b": &graphql.Field{
Type: graphql.String,
},
},
}),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return s.data, nil
},
},
}),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return data, nil
},
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}

result := testutil.TestExecute(t, graphql.ExecuteParams{
Schema: schema,
Root: data,
AST: testutil.TestParse(t, query),
})
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
result := testutil.TestExecute(t, graphql.ExecuteParams{
Schema: schema,
Root: s.data,
AST: testutil.TestParse(t, s.query),
})
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}

expected := map[string]interface{}{
"data": map[string]interface{}{
"a": "1",
},
}
if !reflect.DeepEqual(result.Data, expected) {
t.Fatalf("Expected context.key to equal %v, got %v", expected, result.Data)
if !reflect.DeepEqual(result.Data, s.expected) {
t.Fatalf("Expected context.key to equal %v, got %v", s.expected, result.Data)
}
})
}
}

Expand Down