-
Notifications
You must be signed in to change notification settings - Fork 282
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
Query and Mutation methods ignore query content when it's wrapped in an interface #117
Comments
Thanks for reporting this. I'll take a look. |
The documentation for
To make the snippet you've shown work as is, that would need to change to something like this:
That seems to make the API more complex. But maybe it's possible to make this use-case work without needing to do that, like so: var query any
// imagine a switch-case pattern here which dynamically decides what the query actually is
-query = struct {
+query = &struct {
Hero struct {
Name graphql.String
Droid struct {
PrimaryFunction graphql.String
} `graphql:"... on Droid"`
Human struct {
Height graphql.Float
} `graphql:"... on Human"`
} `graphql:"hero(episode: \"JEDI\")"`
}{}
-err := a.client.Query(context.Background(), &query, nil)
+err := a.client.Query(context.Background(), query, nil) The difference is that the TestClient_Query_Issue117For reference, here's a test I used to check that the above should work: func TestClient_Query_Issue117(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
body := mustRead(req.Body)
if got, want := body, `{"query":"{user{name}}"}`+"\n"; got != want {
t.Errorf("got body: %v, want %v", got, want)
}
w.Header().Set("Content-Type", "application/json")
mustWrite(w, `{"data": {"user": {"name": "Gopher"}}}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})
var q any = &struct {
User struct {
Name string
}
}{}
err := client.Query(context.Background(), q, map[string]any{})
if err != nil {
t.Fatal(err)
}
if got, want := reflect.ValueOf(q).Elem().FieldByName("User").FieldByName("Name").Interface().(string), "Gopher"; got != want {
t.Errorf("got q.User.Name: %q, want: %q", got, want)
}
} |
Hey, thanks for the fast response! That would surely work for me. |
The following code won't work
The request body will look like
{"query":""}
. I set a query string in thegraphql
tag, it would appear here - but the requested fields etc. would still be missing.The problem is probably that the type of
query
is nowinterface{} | struct {...}
(orinterface{} | HeroQuery
with an explicit type declaration), and only typeinterface{}
is regarded.Edit: I had a look into the code. Seems like you only process
reflect.Ptr
,reflect.Type
andreflect.Struct
in the functionwriteQuery
. The kind here is (pointer to)interface{}
. Reflection is such a pain in Go. :/The text was updated successfully, but these errors were encountered: