forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
98 lines (86 loc) · 2.06 KB
/
index.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
package main
import (
"bytes"
"encoding/json"
"net/http"
"os"
)
type HasuraEvent struct {
ID string `json:"id"`
Event `json:"event"`
Table `json:"table"`
Trigger `json:"trigger"`
}
type Event struct {
Op string `json:"op"`
Data `json:"data"`
}
type Data struct {
Old map[string]interface{} `json:"old"`
New map[string]interface{} `json:"new"`
}
type Table struct {
Name string `json:"name"`
Schema string `json:"schema"`
}
type Trigger struct {
ID string `json:"id"`
Name string `json:"name"`
}
const MUTATION_UPDATE_NOTE_REVISION = `
mutation updateNoteRevision ($object: note_revision_insert_input!) {
insert_note_revision (objects: [$object]) {
affected_rows
returning {
id
}
}
}
`
var HGE_ENDPOINT = os.Getenv("HGE_ENDPOINT")
func Handler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var event HasuraEvent
err := decoder.Decode(&event)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
note_id, ok := event.Data.Old["id"]
if !ok {
http.Error(w, "invalid payload: note id not found", http.StatusBadRequest)
return
}
note, ok := event.Data.New["note"]
if !ok {
http.Error(w, "invalid payload: note not found", http.StatusBadRequest)
return
}
// execute the mutation
payload := map[string]interface{}{
"query": MUTATION_UPDATE_NOTE_REVISION,
"variables": map[string]interface{}{
"object": map[string]interface{}{
"note_id": note_id.(float64),
"note": note.(string),
},
},
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(payload)
res, err := http.Post(HGE_ENDPOINT, "application/json; charset=utf-8", b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer res.Body.Close()
var response map[string]interface{}
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}