Making Use of Hasura BaaS Data APIs
Jul 29, 2017 · 1 min read
In this section now we will look at how we make CRUD(Create, Read, Update, Delete) queries to the hasura platform.
We make simple HTTP POST request to the platform data endpoint to communicate with the Postgres database. The JSON query we make is compiled to SQL query by the Hasura platform and that is how we are able to interact directly with the database. The response is also sent in JSON format which makes it easier to work with across different platforms.
There are a plenty of resources for the postman requests so I will demo the API calls using python requests library.
INSERT
Request:
headers = {"Content-Type": "application/json",
"Authorization": "Bearer vgfdgibrishdhdfgibrishgtt"}def insert_in_hosts(name, email, phone):
body = {
"type": "insert",
"args": {
"table": "hosts",
"objects": [{
"name": name,
"email": email,
"phone": phone
}]
}
}
url = "http://data.c100.hasura.me/v1/query"
x = requests.post(url, data=json.dumps(body), headers=headers)
return x.text
Response:
{
"affected_rows" : 1
}SELECT
Request:
headers = {"Content-Type": "application/json",
"Authorization": "Bearer vgfdgibrishdhdfgibrishgtt"}def get_checkedout():
body = {
"type": "select",
"args": {
"table": "checkedout",
"columns": ["*"]
}
}
url = "http://data.c100.hasura.me/v1/query"
x = requests.post(url, data=json.dumps(body), headers=headers)
return x.text
Response:
An array of Object <Object>. The structure of each object is defined by the columns of the query.DELETE
Request:
headers = {"Content-Type": "application/json",
"Authorization": "Bearer vgfdgibrishdhdfgibrishgtt"}def delete_checkin(visitor_id):
body = {
"type": "delete",
"args": {
"table": "checkin",
"where": {"visitor_id": {"$eq": visitor_id}}
}
}
url = "http://data.c100.hasura.me/v1/query"
x = requests.post(url, data=json.dumps(body), headers=headers)
return x.text
Response:
{
"affected_rows" : 1
}