Skip to content

Commit 36a88d7

Browse files
committed
Additionally remove dependency on gorilla/schema
1 parent 16bc447 commit 36a88d7

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

handler.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"encoding/json"
55
"io/ioutil"
66
"net/http"
7+
"net/url"
78
"strings"
89

9-
"github.com/gorilla/schema"
1010
"github.com/graphql-go/graphql"
11+
1112
"golang.org/x/net/context"
1213
)
1314

@@ -17,8 +18,6 @@ const (
1718
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
1819
)
1920

20-
var decoder = schema.NewDecoder()
21-
2221
type Handler struct {
2322
Schema *graphql.Schema
2423

@@ -37,26 +36,34 @@ type requestOptionsCompatibility struct {
3736
OperationName string `json:"operationName" url:"operationName" schema:"operationName"`
3837
}
3938

40-
// RequestOptions Parses a http.Request into GraphQL request options struct
41-
func NewRequestOptions(r *http.Request) *RequestOptions {
42-
43-
query := r.URL.Query().Get("query")
39+
func getFromForm(values url.Values) *RequestOptions {
40+
query := values.Get("query")
4441
if query != "" {
45-
4642
// get variables map
4743
var variables map[string]interface{}
48-
variablesStr := r.URL.Query().Get("variables")
44+
variablesStr := values.Get("variables")
4945
json.Unmarshal([]byte(variablesStr), variables)
5046

5147
return &RequestOptions{
5248
Query: query,
5349
Variables: variables,
54-
OperationName: r.URL.Query().Get("operationName"),
50+
OperationName: values.Get("operationName"),
5551
}
5652
}
53+
54+
return nil
55+
}
56+
57+
// RequestOptions Parses a http.Request into GraphQL request options struct
58+
func NewRequestOptions(r *http.Request) *RequestOptions {
59+
if reqOpt := getFromForm(r.URL.Query()); reqOpt != nil {
60+
return reqOpt
61+
}
62+
5763
if r.Method != "POST" {
5864
return &RequestOptions{}
5965
}
66+
6067
if r.Body == nil {
6168
return &RequestOptions{}
6269
}
@@ -76,16 +83,16 @@ func NewRequestOptions(r *http.Request) *RequestOptions {
7683
Query: string(body),
7784
}
7885
case ContentTypeFormURLEncoded:
79-
var opts RequestOptions
80-
err := r.ParseForm()
81-
if err != nil {
86+
if err := r.ParseForm(); err != nil {
8287
return &RequestOptions{}
8388
}
84-
err = decoder.Decode(&opts, r.PostForm)
85-
if err != nil {
86-
return &RequestOptions{}
89+
90+
if reqOpt := getFromForm(r.PostForm); reqOpt != nil {
91+
return reqOpt
8792
}
88-
return &opts
93+
94+
return &RequestOptions{}
95+
8996
case ContentTypeJSON:
9097
fallthrough
9198
default:

0 commit comments

Comments
 (0)