Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 08cfeaa

Browse files
author
sw-yx
committed
add hasura event trigger template
1 parent 8583c98 commit 08cfeaa

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
name: "hasura-event-triggered",
3+
description:
4+
"Serverless function to process a Hasura event and fire off a GraphQL mutation with cleaned text data"
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// with thanks to https://github.com/vnovick/netlify-function-example/blob/master/functions/bad-words.js
2+
const axios = require("axios");
3+
const Filter = require("bad-words");
4+
const filter = new Filter();
5+
const hgeEndpoint = "https://live-coding-netlify.herokuapp.com";
6+
7+
const query = `
8+
mutation verifiedp($id: uuid!, $title: String!, $content: String!) {
9+
update_posts(_set: { verified: true, content: $content, title: $title },
10+
where:{ id: { _eq: $id } }) {
11+
returning {
12+
id
13+
}
14+
}
15+
}
16+
`;
17+
18+
exports.handler = async (event, context) => {
19+
let request;
20+
try {
21+
request = JSON.parse(event.body);
22+
} catch (e) {
23+
return { statusCode: 400, body: "c annot parse hasura event" };
24+
}
25+
26+
const variables = {
27+
id: request.event.data.new.id,
28+
title: filter.clean(request.event.data.new.title),
29+
content: filter.clean(request.event.data.new.content)
30+
};
31+
try {
32+
await axios.post(hgeEndpoint + "/v1alpha1/graphql", { query, variables });
33+
return { statusCode: 200, body: "success" };
34+
} catch (err) {
35+
return { statusCode: 500, body: err.toString() };
36+
}
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "hasura-event-triggered",
3+
"version": "1.0.0",
4+
"description": "netlify functions:create - Serverless function to process a Hasura event and fire off a GraphQL mutation with cleaned text data",
5+
"main": "hasura-event-triggered.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"netlify",
11+
"serverless",
12+
"js",
13+
"hasura"
14+
],
15+
"author": "Netlify",
16+
"license": "MIT",
17+
"dependencies": {
18+
"axios": "^0.18.0",
19+
"bad-words": "^3.0.2"
20+
}
21+
}

0 commit comments

Comments
 (0)