@@ -4,11 +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
- "github.com/unrolled/render"
11
+
12
12
"golang.org/x/net/context"
13
13
)
14
14
@@ -18,11 +18,10 @@ const (
18
18
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
19
19
)
20
20
21
- var decoder = schema .NewDecoder ()
22
-
23
21
type Handler struct {
24
22
Schema * graphql.Schema
25
- render * render.Render
23
+
24
+ pretty bool
26
25
}
27
26
type RequestOptions struct {
28
27
Query string `json:"query" url:"query" schema:"query"`
@@ -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 :
@@ -122,8 +129,18 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
122
129
}
123
130
result := graphql .Do (params )
124
131
125
- // render result
126
- h .render .JSON (w , http .StatusOK , result )
132
+
133
+ if h .pretty {
134
+ w .WriteHeader (http .StatusOK )
135
+ buff , _ := json .MarshalIndent (result , "" , "\t " )
136
+
137
+ w .Write (buff )
138
+ } else {
139
+ w .WriteHeader (http .StatusOK )
140
+ buff , _ := json .Marshal (result )
141
+
142
+ w .Write (buff )
143
+ }
127
144
}
128
145
129
146
// ServeHTTP provides an entrypoint into executing graphQL queries.
@@ -150,11 +167,9 @@ func New(p *Config) *Handler {
150
167
if p .Schema == nil {
151
168
panic ("undefined GraphQL schema" )
152
169
}
153
- r := render .New (render.Options {
154
- IndentJSON : p .Pretty ,
155
- })
170
+
156
171
return & Handler {
157
172
Schema : p .Schema ,
158
- render : r ,
173
+ pretty : p . Pretty ,
159
174
}
160
175
}
0 commit comments