-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler-with-sanitization.js
68 lines (52 loc) · 1.48 KB
/
handler-with-sanitization.js
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
const express = require("express");
const bodyParser = require("body-parser");
var Filter = require('bad-words');
const filter = new Filter();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
const fetch = require("node-fetch")
const HASURA_OPERATION = `
mutation add_todo ($title:String!, $is_public:Boolean = false, $user_id:Int) {
insert_todos_one(object:{title:$title, is_public:$is_public, user_id:$user_id}) {
id
}
}
`;
// execute the parent operation in Hasura
const execute = async (variables) => {
const fetchResponse = await fetch(
"https://react-summit-hasura-workshop.herokuapp.com/v1/graphql",
{
method: 'POST',
headers: {
'x-hasura-admin-secret': process.env.SECRET
},
body: JSON.stringify({
query: HASURA_OPERATION,
variables
})
}
);
const data = await fetchResponse.json();
console.log('DEBUG: ', data);
return data;
};
// Request Handler
app.post('/add_todo', async (req, res) => {
// get request input
const { title, is_public, user_id } = req.body.input;
// run some business logic
const sanitized = filter.clean(title);
// execute the Hasura operation
const { data, errors } = await execute({ title: sanitized, is_public, user_id });
// if Hasura operation errors, then throw error
if (errors) {
return res.status(400).json(errors[0])
}
// success
return res.json({
id: data.insert_todos_one.id
})
});
app.listen(PORT);