forked from graphql-go/relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
154 lines (136 loc) · 3.71 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
package gqlrelay
import (
"encoding/json"
"github.com/chris-ramon/graphql-go"
"github.com/chris-ramon/graphql-go/types"
"github.com/gorilla/schema"
"github.com/unrolled/render"
"io/ioutil"
"net/http"
"strings"
)
const (
ContentTypeJSON = "application/json"
ContentTypeGraphQL = "application/graphql"
ContentTypeFormUrlEncoded = "application/x-www-form-urlencoded"
)
var decoder = schema.NewDecoder()
type Handler struct {
Schema *types.GraphQLSchema
render *render.Render
}
type requestOptions 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"`
}
// a workaround for getting`variables` as a JSON string
type requestOptionsCompatibility 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 getRequestOptions(r *http.Request) *requestOptions {
query := r.URL.Query().Get("query")
if query != "" {
// get variables map
var variables map[string]interface{}
variablesStr := r.URL.Query().Get("variables")
json.Unmarshal([]byte(variablesStr), variables)
return &requestOptions{
Query: query,
Variables: variables,
OperationName: r.URL.Query().Get("operationName"),
}
}
if r.Method != "POST" {
return &requestOptions{}
}
if r.Body == nil {
return &requestOptions{}
}
// 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 &requestOptions{}
}
return &requestOptions{
Query: string(body),
}
case ContentTypeFormUrlEncoded:
var opts requestOptions
err := r.ParseForm()
if err != nil {
return &requestOptions{}
}
err = decoder.Decode(&opts, r.PostForm)
if err != nil {
return &requestOptions{}
}
return &opts
case ContentTypeJSON:
fallthrough
default:
var opts requestOptions
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return &opts
}
err = json.Unmarshal(body, &opts)
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 optsCompatible requestOptionsCompatibility
json.Unmarshal(body, &optsCompatible)
json.Unmarshal([]byte(optsCompatible.Variables), &opts.Variables)
}
return &opts
}
}
// ServeHTTP provides an entry point into executing graphQL queries
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// get query
opts := getRequestOptions(r)
// execute graphql query
resultChannel := make(chan *types.GraphQLResult)
params := gql.GraphqlParams{
Schema: *h.Schema,
RequestString: opts.Query,
VariableValues: opts.Variables,
OperationName: opts.OperationName,
}
go gql.Graphql(params, resultChannel)
result := <-resultChannel
// render result
h.render.JSON(w, http.StatusOK, result)
}
type HandlerConfig struct {
Schema *types.GraphQLSchema
Pretty bool
}
func NewHandlerConfig() *HandlerConfig {
return &HandlerConfig{
Schema: nil,
Pretty: true,
}
}
func NewHandler(p *HandlerConfig) *Handler {
if p == nil {
p = NewHandlerConfig()
}
if p.Schema == nil {
panic("undefined graphQL schema")
}
r := render.New(render.Options{
IndentJSON: p.Pretty,
})
return &Handler{
Schema: p.Schema,
render: r,
}
}