Fetching data from Graphql API in Emberjs App

Search for a command to run...

No comments yet. Be the first to comment.
Motivation Recently, I tried to learn some low-level system programming stuff. I am a Mac user, and I thought that everything that works on Linux should also work on Mac. After all, Mac is a Unix-based system 😊. I guess we all heard this. Oh boy! I ...

Problem Statement While working on a Nestjs project, I encountered a weird problem related to the database column. I was trying to insert a record into a MySQL table using TypeORM. The error I was experiencing stated that a specific column “cannot be...

Recently, I was exploring design patterns courses on my LinkedIn Learning subscription. I came across a course, Node.js: Design Patterns by Alex Banks. It is a wonderful, easy-to-understand course. I started with the Builder Pattern, and the explanat...

Suppose you are working on a table in a LiveView project. This table has limited static data of not more than one page (you can avoid questions about pagination in the comment section 😊). From a user's point of view, it becomes hard to look into the...

Recently, while working on one of my personal Elixir projects. I came across a scenario where I needed to make some changes to the database schema.The changes mainly revolve around adding and removing indexes due to changes in the business requiremen...

Hashnode is a great platform to get started blogging by providing you hashnode sub-domain and also give option to add your own custom domain like I have this one. Suppose you are creating a web app where you want to display blogs that you have written on hashnode. It would be cumbersome to copy-paste the same blog along with certain alignment and images. Hashnode not only provides a blogging interface to write blog it also has its excellent Developer API which you can use to fetch the blogs on your web app. In this blog, we will use Hashnode's API to fetch your blogs and display them on the Ember.js application.Lets get started.
ember new show-hashnode-blogs.ember-apollo-client is the client we are going to install.ember install ember-apollo-client.ember-apollo-client requires you to register the endpoint API from which you want to fetch data.config/environment.js file and add following line within ENV object.let ENV = {
...
apollo: {
apiURL: 'https://api.hashnode.com/'
},
...
}
.graphql extension.gql/queries inside app folder where we will add our first .graphql file.app/gql/queries/blogs.graphql. It's purpose will be to fetch all blogs.query($page: Int!){
user(username:"your-hashnode-username") {
publication {
posts(page: $page) {
title
brief
slug
cuid
coverImage
}
}
}
}
app/routes/application.js and import the created graphql file in it. import Route from '@ember/routing/route';
import { queryManager } from "ember-apollo-client";
import allBlogQuery from "show-hashnode-blogs/gql/queries/blogs.graphql";
export default class ApplicationRoute extends Route {
@queryManager apollo;
async model() {
const result = await this.apollo.watchQuery({ query: allBlogQuery, { page: 0 }, "user");
return result.publication.posts;
}
}
watchQuery method which take the imported graphql query along with pagination option and model to fetch.This is pretty straightforward we are just going to loop over the model hook and display as per style we want.
{{#each @model as |post|}}
<section>
<div class="content">
<div class="inner">
<h2>{{post.title}}</h2>
<p>{{post.brief}}</p>
</div>
</div>
</section>
{{/each}}
Similarly, you can create graphql file for fetching individual blog i.e detail version of the blog with following query and call it on detail route of the blog.
query($slug: String!) {
post(slug: $slug, hostname:"username") {
title,
slug,
cuid,
coverImage,
content,
contentMarkdown,
tags {
name
}
}
}
I hope you like this blog and find it useful. If you have any questions then please comment below.