Skip to content

Commit

Permalink
Merge pull request #29 from graphql-go/fix/variables-bug
Browse files Browse the repository at this point in the history
handler: fixes queryparams variables
  • Loading branch information
chris-ramon authored Oct 15, 2017
2 parents 707d93f + f1c10e5 commit f0393b2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ _testmain.go
*.exe
*.test
*.prof
*.swp
5 changes: 2 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (

type Handler struct {
Schema *graphql.Schema

pretty bool
graphiql bool
}
Expand All @@ -41,9 +40,9 @@ func getFromForm(values url.Values) *RequestOptions {
query := values.Get("query")
if query != "" {
// get variables map
var variables map[string]interface{}
variables := make(map[string]interface{}, len(values))
variablesStr := values.Get("variables")
json.Unmarshal([]byte(variablesStr), variables)
json.Unmarshal([]byte(variablesStr), &variables)

return &RequestOptions{
Query: query,
Expand Down
36 changes: 31 additions & 5 deletions request_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
func TestRequestOptions_GET_BasicQueryString(t *testing.T) {
queryString := "query=query RebelsShipsQuery { rebels { name } }"
expected := &RequestOptions{
Query: "query RebelsShipsQuery { rebels { name } }",
Query: "query RebelsShipsQuery { rebels { name } }",
Variables: make(map[string]interface{}),
}

req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
Expand Down Expand Up @@ -69,7 +70,8 @@ func TestRequestOptions_GET_ContentTypeApplicationUrlEncoded(t *testing.T) {
func TestRequestOptions_POST_BasicQueryString_WithNoBody(t *testing.T) {
queryString := "query=query RebelsShipsQuery { rebels { name } }"
expected := &RequestOptions{
Query: "query RebelsShipsQuery { rebels { name } }",
Query: "query RebelsShipsQuery { rebels { name } }",
Variables: make(map[string]interface{}),
}

req, _ := http.NewRequest("POST", fmt.Sprintf("/graphql?%v", queryString), nil)
Expand Down Expand Up @@ -150,6 +152,27 @@ func TestRequestOptions_POST_ContentTypeApplicationJSON(t *testing.T) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}

func TestRequestOptions_GET_WithVariablesAsObject(t *testing.T) {
variables := url.QueryEscape(`{ "a": 1, "b": "2" }`)
query := url.QueryEscape("query RebelsShipsQuery { rebels { name } }")
queryString := fmt.Sprintf("query=%s&variables=%s", query, variables)
expected := &RequestOptions{
Query: "query RebelsShipsQuery { rebels { name } }",
Variables: map[string]interface{}{
"a": float64(1),
"b": "2",
},
}

req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
result := NewRequestOptions(req)

if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}

func TestRequestOptions_POST_ContentTypeApplicationJSON_WithVariablesAsObject(t *testing.T) {
body := `
{
Expand Down Expand Up @@ -223,7 +246,8 @@ func TestRequestOptions_POST_ContentTypeApplicationUrlEncoded(t *testing.T) {
data.Add("query", "query RebelsShipsQuery { rebels { name } }")

expected := &RequestOptions{
Query: "query RebelsShipsQuery { rebels { name } }",
Query: "query RebelsShipsQuery { rebels { name } }",
Variables: make(map[string]interface{}),
}

req, _ := http.NewRequest("POST", "/graphql", bytes.NewBufferString(data.Encode()))
Expand Down Expand Up @@ -263,7 +287,8 @@ func TestRequestOptions_POST_ContentTypeApplicationUrlEncoded_WithNilBody(t *tes
func TestRequestOptions_PUT_BasicQueryString(t *testing.T) {
queryString := "query=query RebelsShipsQuery { rebels { name } }"
expected := &RequestOptions{
Query: "query RebelsShipsQuery { rebels { name } }",
Query: "query RebelsShipsQuery { rebels { name } }",
Variables: make(map[string]interface{}),
}

req, _ := http.NewRequest("PUT", fmt.Sprintf("/graphql?%v", queryString), nil)
Expand Down Expand Up @@ -318,7 +343,8 @@ func TestRequestOptions_PUT_ContentTypeApplicationUrlEncoded(t *testing.T) {
func TestRequestOptions_DELETE_BasicQueryString(t *testing.T) {
queryString := "query=query RebelsShipsQuery { rebels { name } }"
expected := &RequestOptions{
Query: "query RebelsShipsQuery { rebels { name } }",
Query: "query RebelsShipsQuery { rebels { name } }",
Variables: make(map[string]interface{}),
}

req, _ := http.NewRequest("DELETE", fmt.Sprintf("/graphql?%v", queryString), nil)
Expand Down

0 comments on commit f0393b2

Please sign in to comment.