HomeGetting StartedAPI ReferenceRecipes
useCommentsGetting StartedAPI ReferenceRecipes

Recipes

  1. Pagination

  2. Comments with approval

  3. Send an email after receiving a new comment

Pagination

Below you can find an example implementation of pagination using use-comments.

Comments with approval

In your Hasura app you can see that there's column hidden in the comments table. By default this value is set to false meaning that after adding a comment it will be automatically visible for other people. You can change it to true and then all the comments would need to be manually approved before being publicly visible. To do this we need to go to Modify section of the comments table:

If hidden is set to true, newly added comments are going to have status delivered-awaiting-approval and won't be visible for other users. To approve them you'd need to edit a particular row and set value of hidden to false.

Send an email after receiving a new comment

It would be cool if there were some notifications after receiving a new comment, right? I have good news! With Hasura it's very straightforward. You can use Data Triggers. A Data Trigger will capture insert events on a specified table and then call a webhook that can carry out any custom logic. In this case this custom logic would be sending an email.

You can read more about Data Triggers here.

An example webhook which sends email implemented with Express.

const nodemailer = require('nodemailer');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const transporter = nodemailer.createTransport({
service: process.env.SMTP_SERVICE,
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
auth: {
user: process.env.SMTP_LOGIN,
pass: process.env.SMTP_PASSWORD,
},
});
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.json());
app.post('/send-email', function (req, res) {
const comment = req.body.event.data.new;
const mailOptions = {
from: process.env.SMTP_LOGIN,
to: process.env.SMTP_LOGIN,
subject: 'You have a new comment!',
text: `New comment: ${JSON.stringify(comment)}`,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
return;
}
res.json({ success: true });
});
});
app.listen(app.get('port'), function () {
console.log('Server started on: ' + app.get('port'));
});