Skip to content

Commit 52e6de4

Browse files
committed
Merge pull request #8 from DarkDNA/master
Remove extrenious dependencies.
2 parents f2b24d1 + e0638a4 commit 52e6de4

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

handler.go

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
2321
type Handler struct {
2422
Schema *graphql.Schema
25-
render *render.Render
23+
24+
pretty bool
2625
}
2726
type 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
}

handler_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ func TestContextPropagated(t *testing.T) {
8383
}
8484
}
8585

86-
func TestHandler_BasicQuery(t *testing.T) {
87-
86+
func TestHandler_BasicQuery_Pretty(t *testing.T) {
8887
expected := &graphql.Result{
8988
Data: map[string]interface{}{
9089
"rebels": map[string]interface{}{
@@ -108,6 +107,32 @@ func TestHandler_BasicQuery(t *testing.T) {
108107
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
109108
}
110109
}
110+
111+
func TestHandler_BasicQuery_Ugly(t *testing.T) {
112+
expected := &graphql.Result{
113+
Data: map[string]interface{}{
114+
"rebels": map[string]interface{}{
115+
"id": "RmFjdGlvbjox",
116+
"name": "Alliance to Restore the Republic",
117+
},
118+
},
119+
}
120+
queryString := `query=query RebelsShipsQuery { rebels { id, name } }`
121+
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
122+
123+
h := handler.New(&handler.Config{
124+
Schema: &starwars.Schema,
125+
Pretty: false,
126+
})
127+
result, resp := executeTest(t, h, req)
128+
if resp.Code != http.StatusOK {
129+
t.Fatalf("unexpected server response %v", resp.Code)
130+
}
131+
if !reflect.DeepEqual(result, expected) {
132+
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
133+
}
134+
}
135+
111136
func TestHandler_Params_NilParams(t *testing.T) {
112137
defer func() {
113138
if r := recover(); r != nil {

0 commit comments

Comments
 (0)