-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
62 lines (50 loc) · 1.46 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
//revive:disable:package-comments
package graphql
import (
"encoding/json"
"net/http"
"os"
"github.com/graphql-go/graphql"
"git.sonicoriginal.software/logger.git"
"git.sonicoriginal.software/server.git/v2"
)
// name is the name used to identify the service
const name = "gql"
type postData struct {
Variables map[string]interface{} `json:"variables"`
Query string `json:"query"`
Operation string `json:"operation"`
}
// Handler handles GraphQL API requests
type handler struct {
logger logger.Log
}
// ServeHTTP fulfills the http.Handler contract for Handler
func (h *handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
var jsonData postData
if err := json.NewDecoder(request.Body).Decode(&jsonData); err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
}
result := graphql.Do(graphql.Params{
Context: request.Context(),
Schema: Schema,
RequestString: jsonData.Query,
VariableValues: jsonData.Variables,
OperationName: jsonData.Operation,
})
if err := json.NewEncoder(writer).Encode(result); err != nil {
h.logger.Error("Could not write result to response: %s", err)
http.Error(writer, err.Error(), http.StatusBadRequest)
}
}
// New returns a new Handler
func New(mux *http.ServeMux) (route string) {
logger := logger.New(
name,
logger.DefaultSeverity,
os.Stdout,
os.Stderr,
)
return server.RegisterHandler(name, &handler{logger}, mux)
}