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

Commit

Permalink
add hasura event trigger template
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Apr 22, 2019
1 parent 8583c98 commit 08cfeaa
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
name: "hasura-event-triggered",
description:
"Serverless function to process a Hasura event and fire off a GraphQL mutation with cleaned text data"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// with thanks to https://github.com/vnovick/netlify-function-example/blob/master/functions/bad-words.js
const axios = require("axios");
const Filter = require("bad-words");
const filter = new Filter();
const hgeEndpoint = "https://live-coding-netlify.herokuapp.com";

const query = `
mutation verifiedp($id: uuid!, $title: String!, $content: String!) {
update_posts(_set: { verified: true, content: $content, title: $title },
where:{ id: { _eq: $id } }) {
returning {
id
}
}
}
`;

exports.handler = async (event, context) => {
let request;
try {
request = JSON.parse(event.body);
} catch (e) {
return { statusCode: 400, body: "c annot parse hasura event" };
}

const variables = {
id: request.event.data.new.id,
title: filter.clean(request.event.data.new.title),
content: filter.clean(request.event.data.new.content)
};
try {
await axios.post(hgeEndpoint + "/v1alpha1/graphql", { query, variables });
return { statusCode: 200, body: "success" };
} catch (err) {
return { statusCode: 500, body: err.toString() };
}
};
21 changes: 21 additions & 0 deletions src/functions-templates/js/hasura-event-triggered/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "hasura-event-triggered",
"version": "1.0.0",
"description": "netlify functions:create - Serverless function to process a Hasura event and fire off a GraphQL mutation with cleaned text data",
"main": "hasura-event-triggered.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"netlify",
"serverless",
"js",
"hasura"
],
"author": "Netlify",
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"bad-words": "^3.0.2"
}
}

0 comments on commit 08cfeaa

Please sign in to comment.