This repo features examples of Actions to perform custom business logic with Hasura.
Each example comes with it's own schema with relevant migration files to get started.
Examples of handling custom business logic with Hasura Actions
Home Page: https://www.youtube.com/watch?v=oqbxEp4FIjE&list=PLTRTpHrUcSB9e9m43VKDrO0UBDQfZmW90
The current login action is written in Node.js Express.
-- An endpoint /login which receives username and password as part of the input argument in mutation
-- Return the id and token generated using a JWT library.
An example on how to send email with Amazon SES
The current example uses SMTP to send emails. (Can be integrated with any provider that supports SMTP - like SendGrid, SparkPost etc).
We need a new route to show an example of how to send emails using Amazon SES using their SDK.
The docker-compose up command fails on linux based systems as the hostname host.docker.internal is not resolved in Linux systems, throws could not translate host name "host.docker.internal" to address: Name or service not known error.
Workaround:
use docker's internal gateway address 172.17.0.1 instead of host.docker.internal while using the default docker network.
tried on Ubuntu 20.04.1 LTS
Create a Custom Signup Mutation with Hasura Actions in Heroku deploy v1.2.0 from following:
https://www.youtube.com/watch?v=oqbxEp4FIjE&t=186s
mutation {
signup(name: "Seb", home: "UK", email: "[email protected]", password: "test123") {
id
}
}
getting:
{
"errors": [
{
"extensions": {
"internal": {
"error": {
"type": "http_exception",
"message": "ConnectionFailure Network.Socket.getAddrInfo (called with preferred socket type/protocol: AddrInfo {addrFlags = [AI_ADDRCONFIG], addrFamily = AF_UNSPEC, addrSocketType = Stream, addrProtocol = 0, addrAddress = <assumed to be undefined>, addrCanonName = <assumed to be undefined>}, host name: Just \"host.docker.internal\", service name: Just \"3000\"): does not exist (Name or service not known)"
},
"response": null,
"request": {
"body": {
"session_variables": {
"x-hasura-role": "admin"
},
"input": {
"email": "[email protected]",
"home": "UK",
"name": "seb",
"password": "test"
},
"action": {
"name": "signup"
}
},
"url": "http://host.docker.internal:3000/signup",
"headers": []
}
},
"path": "$",
"code": "unexpected"
},
"message": "http exception when calling webhook"
}
]
}
This solves it ?
Cloned this project then in auth directory
docker-compose -d
in localhost:8080
mutation {
signup(name: "Seb", username: "[email protected]", password: "test123"){
id
name
}
}
also:
"message": "ConnectionFailure Network.Socket.connect: <socket: 33>: does not exist (Connection refused)"
Show how to create an action that runs a custom SQL query against the db
Add an example to do social login (Google, Facebook, Github) with Hasura
const fetch = require("node-fetch");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const HASURA_OPERATION = `
mutation ($username: String!, $password: String!) {
insert_user_one(object: {
username: $username,
password: $password,
}) {
id
}
}
`;
// execute the parent mutation in Hasura
const execute = async (variables, reqHeaders) => {
const fetchResponse = await fetch("http://localhost:8080/v1/graphql", {
method: "POST",
headers: reqHeaders || {},
body: JSON.stringify({
query: HASURA_OPERATION,
variables,
}),
});
return await fetchResponse.json();
};
// Request Handler
const handler = async (req, res) => {
// get request input
const { username, password } = req.body.input;
// run some business logic
const salt = bcrypt.genSaltSync();
let hashedPassword = await bcrypt.hash(password, salt);
// execute the Hasura operation
const { data, errors } = await execute(
{ username, password: hashedPassword },
req.headers
);
// if Hasura operation errors, then throw error
if (errors) {
return res.status(400).json({
message: errors.message,
});
}
const tokenContents = {
sub: data.insert_user_one.id.toString(),
name: username,
iat: Date.now() / 1000,
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": ["user", "anonymous"],
"x-hasura-user-id": data.insert_user_one.id.toString(),
"x-hasura-default-role": "user",
"x-hasura-role": "user",
},
exp: Math.floor(Date.now() / 1000) + 24 * 60 * 60,
};
const token = jwt.sign(tokenContents, process.env.AUTH_PUBLIC_KEY);
// success
return res.json({
...data.insert_user_one,
token: token,
});
};
module.exports = handler;
Auth0 authentication with flutter and hasura.
The current signup action is written in Node.js Express. The same example needs to be ported to python flask.
An endpoint /signup
which receives name
, username
and password
as part of the input argument in mutation
Return the id
and token
generated using a JWT library.
An example on how to implement an Hasura action to process an e-commerce order with Stripe
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.