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

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

Open
LukasLeppich opened this issue Jul 9, 2021 · 0 comments
Open

Comments

@LukasLeppich
Copy link

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

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

No branches or pull requests

1 participant