graphql-go / graphql-go/graphql

`DefaultResolveFn` does not work as expected with `map[K]V` for `K` that has string as the underlying type

Open
#700 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
10.1k
Forks
845
PR merge metrics
No merged PRs in 30d

Description

### Issue

`DefaultResolveFn` works properly for the following maps:

```go
type MyMap map[string]any
```

but when we change the map key:

```
type MyKey string

type MyMap map[MyKey]any
```

it returns the following errors:

```
reflect.Value.MapIndex: value of type string is not assignable to type .*
```

### Example:

#### Error

```
2009/11/10 23:00:00 failed to execute graphql operation, errors: [reflect.Value.MapIndex: value of type string is not assignable to type main.TranslationKey reflect.Value.MapIndex: value of type string is not assignable to type main.TranslationKey]
```

#### Code

```go
package main

import (
"encoding/json"
"fmt"
"log"

"github.com/graphql-go/graphql"
)

type TranslationKey string

type Translation map[TranslationKey]string

func main() {
// Schema
fields := graphql.Fields{
"hello": &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "Translation",
Fields: graphql.Fields{
"en": &graphql.Field{
Type: graphql.String,
},
"fr": &graphql.Field{
Type: graphql.String,
},
},
}),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return Translation{
"en": "hello",
"fr": "bonjour",
}, nil
},
},
}
rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("failed to create new schema, error: %v", err)
}

// Query
query := `
{
hello {
en
fr
}
}
`
params := graphql.Params{Schema: schema, RequestString: query}
r := graphql.Do(params)
if len(r.Errors) > 0 {
log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
}
rJSON, _ := json.Marshal(r)
fmt.Printf("%s \n", rJSON)
}
```

https://go.dev/play/p/giPGGUxKht8

### Solution

The following PR solves that problem, but I believe it's been wrongly closed.

https://github.com/graphql-go/graphql/pull/697

Please take a look at the deeper explanation in the comment https://github.com/graphql-go/graphql/pull/697#issuecomment-2385949145

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.