forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
125 lines (102 loc) · 2.66 KB
/
http.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
package graphql
import (
"context"
"encoding/json"
"errors"
"net/http"
"sync"
"github.com/samsarahq/thunder/batch"
"github.com/samsarahq/thunder/reactive"
)
func HTTPHandler(schema *Schema, middlewares ...MiddlewareFunc) http.Handler {
return &httpHandler{
schema: schema,
middlewares: middlewares,
}
}
type httpHandler struct {
schema *Schema
middlewares []MiddlewareFunc
}
type httpPostBody struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}
type httpResponse struct {
Data interface{} `json:"data"`
Errors []string `json:"errors"`
}
func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
writeResponse := func(value interface{}, err error) {
response := httpResponse{}
if err != nil {
response.Errors = []string{err.Error()}
} else {
response.Data = value
}
responseJSON, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Error(w, string(responseJSON), http.StatusOK)
}
if r.Method != "POST" {
writeResponse(nil, errors.New("request must be a POST"))
return
}
if r.Body == nil {
writeResponse(nil, errors.New("request must include a query"))
return
}
var params httpPostBody
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
writeResponse(nil, err)
return
}
query, err := Parse(params.Query, params.Variables)
if err != nil {
writeResponse(nil, err)
return
}
schema := h.schema.Query
if query.Kind == "mutation" {
schema = h.schema.Mutation
}
if err := PrepareQuery(schema, query.SelectionSet); err != nil {
writeResponse(nil, err)
return
}
var wg sync.WaitGroup
e := Executor{}
wg.Add(1)
runner := reactive.NewRerunner(r.Context(), func(ctx context.Context) (interface{}, error) {
defer wg.Done()
ctx = batch.WithBatching(ctx)
var middlewares []MiddlewareFunc
middlewares = append(middlewares, h.middlewares...)
middlewares = append(middlewares, func(input *ComputationInput, next MiddlewareNextFunc) *ComputationOutput {
output := next(input)
output.Current, output.Error = e.Execute(input.Ctx, schema, nil, input.ParsedQuery)
return output
})
output := RunMiddlewares(middlewares, &ComputationInput{
Ctx: ctx,
ParsedQuery: query,
Query: params.Query,
Variables: params.Variables,
})
current, err := output.Current, output.Error
if err != nil {
if ErrorCause(err) == context.Canceled {
return nil, err
}
writeResponse(nil, err)
return nil, err
}
writeResponse(current, nil)
return nil, nil
}, DefaultMinRerunInterval)
wg.Wait()
runner.Stop()
}