Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3658993

Browse files
committedSep 8, 2018
Handle empty variables map correctly.
Previously, only the nil variables map was handled correctly by fully omitting the variables from the generated GraphQL query. This change makes it so that an empty variables map is handled equivalently, and adds test coverage for it.
1 parent 62c9ce0 commit 3658993

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
 

‎graphql_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package graphql_test
33
import (
44
"context"
55
"io"
6+
"io/ioutil"
67
"net/http"
78
"net/http/httptest"
89
"testing"
@@ -100,6 +101,34 @@ func TestClient_Query_noDataWithErrorResponse(t *testing.T) {
100101
}
101102
}
102103

104+
// Test that an empty (but non-nil) variables map is
105+
// handled no differently than a nil variables map.
106+
func TestClient_Query_emptyVariables(t *testing.T) {
107+
mux := http.NewServeMux()
108+
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
109+
body := mustRead(req.Body)
110+
if got, want := body, `{"query":"{user{name}}"}`+"\n"; got != want {
111+
t.Errorf("got body: %v, want %v", got, want)
112+
}
113+
w.Header().Set("Content-Type", "application/json")
114+
mustWrite(w, `{"data": {"user": {"name": "Gopher"}}}`)
115+
})
116+
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})
117+
118+
var q struct {
119+
User struct {
120+
Name string
121+
}
122+
}
123+
err := client.Query(context.Background(), &q, map[string]interface{}{})
124+
if err != nil {
125+
t.Fatal(err)
126+
}
127+
if got, want := q.User.Name, "Gopher"; got != want {
128+
t.Errorf("got q.User.Name: %q, want: %q", got, want)
129+
}
130+
}
131+
103132
// localRoundTripper is an http.RoundTripper that executes HTTP transactions
104133
// by using handler directly, instead of going over an HTTP connection.
105134
type localRoundTripper struct {
@@ -112,6 +141,14 @@ func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
112141
return w.Result(), nil
113142
}
114143

144+
func mustRead(r io.Reader) string {
145+
b, err := ioutil.ReadAll(r)
146+
if err != nil {
147+
panic(err)
148+
}
149+
return string(b)
150+
}
151+
115152
func mustWrite(w io.Writer, s string) {
116153
_, err := io.WriteString(w, s)
117154
if err != nil {

‎query.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import (
1212

1313
func constructQuery(v interface{}, variables map[string]interface{}) string {
1414
query := query(v)
15-
if variables != nil {
15+
if len(variables) > 0 {
1616
return "query(" + queryArguments(variables) + ")" + query
1717
}
1818
return query
1919
}
2020

2121
func constructMutation(v interface{}, variables map[string]interface{}) string {
2222
query := query(v)
23-
if variables != nil {
23+
if len(variables) > 0 {
2424
return "mutation(" + queryArguments(variables) + ")" + query
2525
}
2626
return "mutation" + query

0 commit comments

Comments
 (0)
Failed to load comments.