This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sw-yx
committed
Apr 22, 2019
1 parent
8583c98
commit 08cfeaa
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/functions-templates/js/hasura-event-triggered/.netlify-function-templates.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}; |
37 changes: 37 additions & 0 deletions
37
src/functions-templates/js/hasura-event-triggered/hasura-event-triggered.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/functions-templates/js/hasura-event-triggered/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |