forked from cayleygraph/cayley
/
http.go
39 lines (33 loc) · 781 Bytes
/
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
package graphql
import (
"context"
"encoding/json"
"io"
"github.com/dennwc/graphql/gqlerrors"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/query"
)
type httpResult struct {
Data interface{} `json:"data"`
Errors []gqlerrors.FormattedError `json:"errors,omitempty"`
}
func httpError(w query.ResponseWriter, err error) {
json.NewEncoder(w).Encode(httpResult{
Errors: []gqlerrors.FormattedError{{
Message: err.Error(),
}},
})
}
func httpQuery(ctx context.Context, qs graph.QuadStore, w query.ResponseWriter, r io.Reader) {
q, err := Parse(r)
if err != nil {
httpError(w, err)
return
}
m, err := q.Execute(ctx, qs)
if err != nil {
httpError(w, err)
return
}
json.NewEncoder(w).Encode(httpResult{Data: m})
}