3.2) Hasura Data API + Postman collection

ALOK SHAKYA
3 min readAug 4, 2017

--

In my previous blog I discussed about data modeling. In which I made 6 tables 1. category table 2. article table 3.comment table 4. user_details 5. article_like 6. comment_like

Now I will make queries from database to fetch data for my blog app functionality.

  1. First I added article to the database using postman like this

Then I fetched articles from database by latest article published like this

Fetching article query

Now for adding like to article I used query

For commenting on article I used query

Now for displaying number of likes I fetched all likes from VIEW article_likes

like this

and stored these values in an array in DataService service from which fetch likes for each article.

Similar approach for comment numbers.

Then for fetching all comments on particular article I used query

Now comes nested query for fetching the recent Activity of a user

In this query I am using relationships to fulfill my objective of fetching recent activity of a user. Firstly this is select type query. Then I am selecting from user_details table. And using three relationships 1) “published_articles” which I established between user_details table and article table using user_id foreign key.

2) “commented_on” which I established between user_details and comment table using user_id.

3) “ liked_article” between user_details and article_like table using user_id and inside this relationship one more nested relation named “liked_on_article” which is between article_like and article table to fetch article related info from article_like table itself.

What actually these relationship are doing is that once I have user_id of a particular user then I can fetch all details about him

By using first relationship “published_articles” I can fetch all info from article table. Simply I have to provide name of relationship then I have to choose columns from article table in my result.

Similar is the case with “commented_on”

But in third relationship there is relationship inside relationship

Once I reached inside article_like table using “liked_article” relationship then I also want title of article on which I liked so to fulfill this need I created another relationship “liked_on_article” in article_like table between article_like table and article table. Then using this relationship I will get article titles on which user has liked.

Response of this request is :

These queries are being used in my blogging app to fulfill its functionality.

In my next articlie I will discuss about auth API.

Previous Next

--

--