Skip to content

Commit 31b446a

Browse files
committed
Exposed handler.RequestOptions and handler.NewRequestOptions() to allow
consumers to write custom graphql HTTP handlers easily
1 parent 4b27062 commit 31b446a

File tree

2 files changed

+65
-64
lines changed

2 files changed

+65
-64
lines changed

handler.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Handler struct {
2323
Schema *graphql.Schema
2424
render *render.Render
2525
}
26-
type requestOptions struct {
26+
type RequestOptions struct {
2727
Query string `json:"query" url:"query" schema:"query"`
2828
Variables map[string]interface{} `json:"variables" url:"variables" schema:"variables"`
2929
OperationName string `json:"operationName" url:"operationName" schema:"operationName"`
@@ -36,7 +36,8 @@ type requestOptionsCompatibility struct {
3636
OperationName string `json:"operationName" url:"operationName" schema:"operationName"`
3737
}
3838

39-
func getRequestOptions(r *http.Request) *requestOptions {
39+
// RequestOptions Parses a http.Request into GraphQL request options struct
40+
func NewRequestOptions(r *http.Request) *RequestOptions {
4041

4142
query := r.URL.Query().Get("query")
4243
if query != "" {
@@ -46,17 +47,17 @@ func getRequestOptions(r *http.Request) *requestOptions {
4647
variablesStr := r.URL.Query().Get("variables")
4748
json.Unmarshal([]byte(variablesStr), variables)
4849

49-
return &requestOptions{
50+
return &RequestOptions{
5051
Query: query,
5152
Variables: variables,
5253
OperationName: r.URL.Query().Get("operationName"),
5354
}
5455
}
5556
if r.Method != "POST" {
56-
return &requestOptions{}
57+
return &RequestOptions{}
5758
}
5859
if r.Body == nil {
59-
return &requestOptions{}
60+
return &RequestOptions{}
6061
}
6162

6263
// TODO: improve Content-Type handling
@@ -68,26 +69,26 @@ func getRequestOptions(r *http.Request) *requestOptions {
6869
case ContentTypeGraphQL:
6970
body, err := ioutil.ReadAll(r.Body)
7071
if err != nil {
71-
return &requestOptions{}
72+
return &RequestOptions{}
7273
}
73-
return &requestOptions{
74+
return &RequestOptions{
7475
Query: string(body),
7576
}
7677
case ContentTypeFormURLEncoded:
77-
var opts requestOptions
78+
var opts RequestOptions
7879
err := r.ParseForm()
7980
if err != nil {
80-
return &requestOptions{}
81+
return &RequestOptions{}
8182
}
8283
err = decoder.Decode(&opts, r.PostForm)
8384
if err != nil {
84-
return &requestOptions{}
85+
return &RequestOptions{}
8586
}
8687
return &opts
8788
case ContentTypeJSON:
8889
fallthrough
8990
default:
90-
var opts requestOptions
91+
var opts RequestOptions
9192
body, err := ioutil.ReadAll(r.Body)
9293
if err != nil {
9394
return &opts
@@ -108,7 +109,7 @@ func getRequestOptions(r *http.Request) *requestOptions {
108109
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
109110

110111
// get query
111-
opts := getRequestOptions(r)
112+
opts := NewRequestOptions(r)
112113

113114
// execute graphql query
114115
params := graphql.Params{

0 commit comments

Comments
 (0)