@@ -4,11 +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- "github.com/unrolled/render"
11+
1212 "golang.org/x/net/context"
1313)
1414
@@ -18,11 +18,10 @@ const (
1818 ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
1919)
2020
21- var decoder = schema .NewDecoder ()
22-
2321type Handler struct {
2422 Schema * graphql.Schema
25- render * render.Render
23+
24+ pretty bool
2625}
2726type RequestOptions struct {
2827 Query string `json:"query" url:"query" schema:"query"`
@@ -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 :
@@ -122,8 +129,18 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
122129 }
123130 result := graphql .Do (params )
124131
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+ }
127144}
128145
129146// ServeHTTP provides an entrypoint into executing graphQL queries.
@@ -150,11 +167,9 @@ func New(p *Config) *Handler {
150167 if p .Schema == nil {
151168 panic ("undefined GraphQL schema" )
152169 }
153- r := render .New (render.Options {
154- IndentJSON : p .Pretty ,
155- })
170+
156171 return & Handler {
157172 Schema : p .Schema ,
158- render : r ,
173+ pretty : p . Pretty ,
159174 }
160175}
0 commit comments