-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
164 lines (140 loc) · 4.22 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package handler
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/playlyfe/go-graphql"
"golang.org/x/net/context"
)
//Shortcuts for the Content-Type header
const (
ContentTypeJSON = "application/json"
ContentTypeGraphQL = "application/graphql"
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
)
//Handler structure
type Handler struct {
Executor *graphql.Executor
Context interface{}
Pretty bool
}
//RequestParameters from query like " /graphql?query=getUser($id:ID){lastName}&variables={"id":"4"} "
type RequestParameters struct {
Query string `json:"query" url:"query" schema:"query"`
Variables map[string]interface{} `json:"variables" url:"variables" schema:"variables"`
OperationName string `json:"operationName" url:"operationName" schema:"operationName"`
}
//RequestParametersCompatibility represents an workaround for getting`variables` as a JSON string
type RequestParametersCompatibility struct {
Query string `json:"query" url:"query" schema:"query"`
Variables string `json:"variables" url:"variables" schema:"variables"`
OperationName string `json:"operationName" url:"operationName" schema:"operationName"`
}
func getFromURL(values url.Values) *RequestParameters {
if values.Get("query") != "" {
// get variables map
var variables map[string]interface{}
variablesStr := values.Get("variables")
json.Unmarshal([]byte(variablesStr), variables)
return &RequestParameters{
Query: values.Get("query"),
Variables: variables,
OperationName: values.Get("operationName"),
}
}
return nil
}
// NewRequestParameters Parses a http.Request into GraphQL request options struct
func NewRequestParameters(r *http.Request) *RequestParameters {
if reqParams := getFromURL(r.URL.Query()); reqParams != nil {
return reqParams
}
if r.Method != "POST" {
return &RequestParameters{}
}
if r.Body == nil {
return &RequestParameters{}
}
// TODO: improve Content-Type handling
contentTypeStr := r.Header.Get("Content-Type")
contentTypeTokens := strings.Split(contentTypeStr, ";")
contentType := contentTypeTokens[0]
switch contentType {
case ContentTypeGraphQL:
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return &RequestParameters{}
}
return &RequestParameters{
Query: string(body),
}
case ContentTypeFormURLEncoded:
if err := r.ParseForm(); err != nil {
return &RequestParameters{}
}
if reqParams := getFromURL(r.PostForm); reqParams != nil {
return reqParams
}
return &RequestParameters{}
case ContentTypeJSON:
fallthrough
default:
var params RequestParameters
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return ¶ms
}
err = json.Unmarshal(body, ¶ms)
if err != nil {
// Probably `variables` was sent as a string instead of an object.
// So, we try to be polite and try to parse that as a JSON string
var CompatibleParams RequestParametersCompatibility
json.Unmarshal(body, &CompatibleParams)
json.Unmarshal([]byte(CompatibleParams.Variables), ¶ms.Variables)
}
return ¶ms
}
}
// ContextHandler provides an entrypoint into executing graphQL queries with a
// user-provided context.
func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
// get query
params := NewRequestParameters(r)
// execute graphql query
result, _ := (h.Executor).Execute(h.Context, params.Query, params.Variables, params.OperationName)
if h.Pretty {
w.WriteHeader(200) //http.StatusOK = 200
buff, _ := json.MarshalIndent(result, "", " ")
w.Write(buff)
} else {
w.WriteHeader(200) //http.StatusOK = 200
buff, _ := json.Marshal(result)
w.Write(buff)
}
}
// ServeHTTP provides an entrypoint into executing graphQL queries.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.ContextHandler(context.Background(), w, r)
}
//Config for handler of the schema
type Config Handler
//New config
func New(c *Config) *Handler {
if c == nil {
c = &Config{
Executor: nil,
Context: "",
Pretty: true,
}
}
if c.Executor == nil {
panic("Undefined GraphQL Executor")
}
return &Handler{
Executor: c.Executor,
Context: c.Context,
Pretty: c.Pretty,
}
}