What is Hasura? | source: click here | 20 JANUARY, 2020 | 3 MIN READ: The Hasura GraphQL Engine is a blazing-fast GraphQL server that gives you instant, realtime GraphQL APIs over Postgres, with webhook triggers on database events, and remote schemas for business logic. Hasura helps you build GraphQL apps backed by Postgres or incrementally move to GraphQL for existing applications using Postgres. The Hasura GraphQL Engine is open-source. You can check out the complete repo here. |
Tanmai Gopal (co-founder) | Blogs |
Rajoshi Ghosh(co-founder) |
// SAMPLE GRAPHQL QUERY VIA FETCH, made by code exporter in Hasura. ~Sahil
/*
This is an example snippet - you should consider tailoring it
to your service.
*/
async function fetchGraphQL(operationsDoc, operationName, variables) {
const result = await fetch(
"undefined",
{
method: "POST",
body: JSON.stringify({
query: operationsDoc,
variables: variables,
operationName: operationName
})
}
);
return await result.json();
}
const operationsDoc = `
mutation MyMutation($likes: Int = 2) {
insert_blogs(objects: {title: "awsm blog", year: 2022, url: "google.com", likes: $likes}) {
affected_rows
returning {
author
id
likes
title
updated_at
url
year
user_id
created_at
}
}
}
`;
function executeMyMutation(likes) {
return fetchGraphQL(
operationsDoc,
"MyMutation",
{"likes": likes}
);
}
async function startExecuteMyMutation(likes) {
const { errors, data } = await executeMyMutation(likes);
if (errors) {
// handle those errors like a pro
console.error(errors);
}
// do something great with this precious data
console.log(data);
}
startExecuteMyMutation(likes);
Awesome: Source: Hasura Authorization #EasyGraphQLwHasura, all this in blogpost: Click here.
employees
text there: Track
way to add relationship this time(see next screenshot): Track All
to track all realtions automatically: manager_id
field as nullable: We can see that Janet and Michael are added:
Now we use Michael's id
as highlighed in above screenshot to be as manager_id
for filed of Elanor and Chiddi:
We can see the results as expected:
Janet (using Janet’s id):
Michael (using Michael’s id):
Elanor (using Elanor’s id):
Chiddi (using Chidd’s id):
Now in payroll we can see all the entries:
Querying in graphiql we can see such data which says Michael is manager for Elanor and Chidi:
We can see employees for employees i.e., for Michael we can see he has employees as Elanor and Chidi:
We can see emoyees and salaries in payroll table:
Setting up permission for HR (insert* permission) and save that:
Now we clone the permission for select, update and delete as well by doing that:
We can see that HR now has all the permissions for insert, select, update and delete:
Now we set select permission for employees:
Now we set select permission for the employee who is actually a Manager:
Make same permission for update as well:
We change the name of payroll table to payrolls:
Lets make everyone to be able to select everything:
Lets clone select permission of Employees(i.e., same above permision) for HR and Managers as well (FYI: Do overwrite the permissions if asked):
Now we can see that everyone can select:
BUT for now we are just gonna do it like this for demo (role=HR and id=Jane’s Id):
and we see that HR can select the data.
also, if we put role=Manager and id=Michael’s Id we see that Michael can select data for Elanor and Chidi only:
and Elanor can only select her own data only:
and same for Chidi:
Also we can see that employee can not mutate data bcoz we set that in permission roles that employees can only query data:
And if we use Manager role instead we can see that Manage can mutate and insert data as well:
Changing salary of Elanor by Manager:
Lets see Elanor’s Salary (we can move to payroll table for any employee like that):
and we can see it update correctly (we can use that button to close it though):
Also, if we try to update Janet’s salary by using Manager role we see that its not allowed:
Now we can see that we can query using our jwt token(yo!!):