Skip to content

Instantly share code, notes, and snippets.

@hirozed
Created October 26, 2021 15:50
Show Gist options
  • Save hirozed/a2804ea5b03bd447cc4d0e8b7e6e8d45 to your computer and use it in GitHub Desktop.
Save hirozed/a2804ea5b03bd447cc4d0e8b7e6e8d45 to your computer and use it in GitHub Desktop.
Example script to pull Hashnode posts
const query = `
{
user(username: "hashnodeUser") {
publication {
posts(page: 0) {
slug
title
coverImage
}
}
}
}
`;
async function fetchPosts() {
const response = await fetch( 'https://api.hashnode.com', {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify( { query: query } )
});
const body = await response.json();
let markup = '';
body.data.user.publication.posts.slice(0,6).forEach((post) => {
markup += `<li>
<a href="https://hashnodeURL.com/${post.slug}">`;
if ( post.coverImage.length !== 0 ) {
markup += `<img src="${post.coverImage}" alt="${post.title} cover image" />`
}
markup +=`<div class="homepage-post-content">${post.title}</div>
</a>
</li>`;
});
document.querySelector('.homepage-posts').innerHTML = markup;
}
fetchPosts();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment