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

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

Open
bkrukowski opened this issue Oct 1, 2024 · 3 comments · May be fixed by #704

Comments

@bkrukowski
Copy link

Issue

DefaultResolveFn works properly for the following maps:

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

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.

#697

Please take a look at the deeper explanation in the comment #697 (comment)

@bkrukowski
Copy link
Author

@chris-ramon I hope this description clarifies your doubts

@chris-ramon
Copy link
Member

ok thanks for the details, I'll give it a look.

@git-hulk
Copy link

@chris-ramon Would you mind having a look at PR #704

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants