@@ -4,10 +4,11 @@ import (
4
4
"encoding/json"
5
5
"io/ioutil"
6
6
"net/http"
7
+ "net/url"
7
8
"strings"
8
9
9
- "github.com/gorilla/schema"
10
10
"github.com/graphql-go/graphql"
11
+
11
12
"golang.org/x/net/context"
12
13
)
13
14
@@ -17,8 +18,6 @@ const (
17
18
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
18
19
)
19
20
20
- var decoder = schema .NewDecoder ()
21
-
22
21
type Handler struct {
23
22
Schema * graphql.Schema
24
23
@@ -37,26 +36,34 @@ type requestOptionsCompatibility struct {
37
36
OperationName string `json:"operationName" url:"operationName" schema:"operationName"`
38
37
}
39
38
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" )
44
41
if query != "" {
45
-
46
42
// get variables map
47
43
var variables map [string ]interface {}
48
- variablesStr := r . URL . Query () .Get ("variables" )
44
+ variablesStr := values .Get ("variables" )
49
45
json .Unmarshal ([]byte (variablesStr ), variables )
50
46
51
47
return & RequestOptions {
52
48
Query : query ,
53
49
Variables : variables ,
54
- OperationName : r . URL . Query () .Get ("operationName" ),
50
+ OperationName : values .Get ("operationName" ),
55
51
}
56
52
}
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
+
57
63
if r .Method != "POST" {
58
64
return & RequestOptions {}
59
65
}
66
+
60
67
if r .Body == nil {
61
68
return & RequestOptions {}
62
69
}
@@ -76,16 +83,16 @@ func NewRequestOptions(r *http.Request) *RequestOptions {
76
83
Query : string (body ),
77
84
}
78
85
case ContentTypeFormURLEncoded :
79
- var opts RequestOptions
80
- err := r .ParseForm ()
81
- if err != nil {
86
+ if err := r .ParseForm (); err != nil {
82
87
return & RequestOptions {}
83
88
}
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
87
92
}
88
- return & opts
93
+
94
+ return & RequestOptions {}
95
+
89
96
case ContentTypeJSON :
90
97
fallthrough
91
98
default :
0 commit comments