Skip to content

Commit 2933f82

Browse files
Рагозин Романchris-ramon
authored andcommitted
Fixed bug with variables of graphql query
1 parent 707d93f commit 2933f82

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const (
2020

2121
type Handler struct {
2222
Schema *graphql.Schema
23-
2423
pretty bool
2524
graphiql bool
2625
}
@@ -41,9 +40,9 @@ func getFromForm(values url.Values) *RequestOptions {
4140
query := values.Get("query")
4241
if query != "" {
4342
// get variables map
44-
var variables map[string]interface{}
43+
variables := make(map[string]interface{})
4544
variablesStr := values.Get("variables")
46-
json.Unmarshal([]byte(variablesStr), variables)
45+
json.Unmarshal([]byte(variablesStr), &variables)
4746

4847
return &RequestOptions{
4948
Query: query,

request_options_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ func TestRequestOptions_POST_ContentTypeApplicationJSON(t *testing.T) {
150150
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
151151
}
152152
}
153+
154+
func TestRequestOptions_GET_WithVariablesAsObject(t *testing.T) {
155+
variables := url.QueryEscape(`{ "a": 1, "b": "2" }`)
156+
query := url.QueryEscape("query RebelsShipsQuery { rebels { name } }")
157+
queryString := fmt.Sprintf("query=%s&variables=%s", query, variables)
158+
expected := &RequestOptions{
159+
Query: "query RebelsShipsQuery { rebels { name } }",
160+
Variables: map[string]interface{}{
161+
"a": float64(1),
162+
"b": "2",
163+
},
164+
}
165+
166+
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
167+
result := NewRequestOptions(req)
168+
169+
if !reflect.DeepEqual(result, expected) {
170+
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
171+
}
172+
}
173+
153174
func TestRequestOptions_POST_ContentTypeApplicationJSON_WithVariablesAsObject(t *testing.T) {
154175
body := `
155176
{

0 commit comments

Comments
 (0)