Hasura Data API + Postman collection
Task 3.2) Learn how to make CRUD queries using Hasura Data APIs
Nice to see you all again! Hope you saw our last blog on Data Modelling.If not then head to this link(Here:-link).Now, the next task of week 3 is to understand the CRUD queries. What is this CRUD(sounds something not easy) ??… Simply said, CRUD is an acronym for Create, Read, Update, Delete! (Not that difficult huh :D!)
Hasura Data API???
Hasura comes with a built-in data service that allows clients to communicate with a Postgres database, via HTTP APIs in JSON. The power of Postgres, and the ease of JSON!!
Any client with a HTTP library can use the data service, so most apps can be made completely ‘backend-less’.
So, for making these APIs Postman App is used.(Download the same :P)
Steps for make database calls using Hasura Data API
- Log in to hasuractl console and get the External Endpoint from the Data and Schema option on the left side.
2. Now, open the Postman App and do the following settings as shown :
Specify that you want to provide raw data and select Content-Type as JSON(application/json).
3. The next foremost important step is to set Authentication as Bearer <admin token>. You can get the admin token by logging in to the hasuractl console. Under the Project info section you can get the admin token.
Now, you are ready to use Hasura Data APIs to make database calls.
a. Insert data
Suppose, in our database we want to enter a new data, we use the insert command. In our case, in the “user” table we made an entry for columns “user_id”, “height” and “weight”. The JSON query for the same is as:
b. Select data
Now, for retrieving the data for some specific condition, we can use select query. In our case, if we want to retrieve data for the user with weight of 50 kgs, the JSON query will be like:
3. Updating data
If we want to update some kind of information about a specific user, we’ll just run the following JSON query:
4. Deleting data
For deleting some data from the database, we can simply use the delete JSON command. In our example, we can delete a particular user’s record as follows:
Thus, we have successfully learned and implemented the JSON queries i.e. CRUD. Now in the next blog, we’ll learn about Hasura Authentication API.Till then Ciao!!!