gqlgenc icon indicating copy to clipboard operation
gqlgenc copied to clipboard

Unable to unmarshal scalar Map type (map[string]interface{})

Open LukasLeppich opened this issue 3 years ago • 0 comments

A schema with scalar Map is generated as type map[string]interface{} (https://gqlgen.com/reference/scalars/)

The graphqljson package can't unmarshal responses with this Map type:

func TestUnmarshalGraphql_map(t *testing.T) {
	t.Parallel()
	type query struct {
		Me struct {
			Name   string
			Height float64
			Properties map[string]interface{}
		}
	}
	var jsonStr = `{
		"me": {
			"name": "Luke Skywalker",
			"height": 1.72,
			"properties": {
				"propKey1": "123",
				"propKey2": "test"
			}
		}
	}`

	var got query
        // The default json decoder works as expected
	if err := json.NewDecoder(strings.NewReader(jsonStr)).Decode(&got); err != nil{
		t.Fatalf("Default json implementation failed: %s", err)
	}
	err := graphqljson.UnmarshalData([]byte(jsonStr), &got)
	if err != nil {
		t.Fatalf("Graphqljson implementation failed: %s", err)
	}
	var want query
	want.Me.Name = "Luke Skywalker"
	want.Me.Height = 1.72
	want.Me.Properties = map[string]interface{}{
		"propKey1": "123",
		"propKey2": "test",
	}
	if diff := cmp.Diff(got, want); diff != "" {
		t.Error(diff)
	}
}

Error: Graphqljson implementation failed: : : struct field for "propKey1" doesn't exist in any of 1 places to unmarshal

LukasLeppich avatar Jul 09 '21 12:07 LukasLeppich