Skip to content

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

Open
@bkrukowski

Description

@bkrukowski

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)

Activity

bkrukowski

bkrukowski commented on Oct 1, 2024

@bkrukowski
Author

@chris-ramon I hope this description clarifies your doubts

chris-ramon

chris-ramon commented on Oct 1, 2024

@chris-ramon
Member

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

added a commit that references this issue on Nov 25, 2024
e1bf8d6
git-hulk

git-hulk commented on Nov 25, 2024

@git-hulk

@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @chris-ramon@git-hulk@bkrukowski

      Issue actions

        `DefaultResolveFn` does not work as expected with `map[K]V` for `K` that has string as the underlying type · Issue #700 · graphql-go/graphql