Skip to content

Commit 199cbce

Browse files
committed
fix: refactor according to changed hashnode api
1 parent 0312291 commit 199cbce

File tree

2 files changed

+22
-59
lines changed

2 files changed

+22
-59
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
<h1 align="center">gatsby-source-hashnode-devblog</h1>
88
<p align="center">
99
Gatsby plugin to retrieve blog posts from your <a href = "https://hashnode.com/devblog">devblog</a> on <a href = "https://hashnode.com/">hashnode</a>. </p>
10-
<p align="center"><a href="https://npmjs.com/package/gatsby-source-hashnode-devblog"><img src="https://badge.fury.io/js/gatsby-source-hashnode-devblog.svg" alt="npm version"></a><img alt="npm" src="https://img.shields.io/npm/dt/gatsby-source-hashnode-devblog"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square"><img src="https://badgen.net/github/license/nishantwrp/gatsby-source-hashnode-devblog"><img src="https://img.shields.io/david/nishantwrp/gatsby-source-hashnode-devblog"></p>
10+
<p align="center">
11+
<a href="https://npmjs.com/package/gatsby-source-hashnode-devblog"><img src="https://badge.fury.io/js/gatsby-source-hashnode-devblog.svg" alt="npm version"></a>
12+
<img alt="npm" src="https://img.shields.io/npm/dt/gatsby-source-hashnode-devblog">
13+
<img src="https://img.shields.io/david/nishantwrp/gatsby-source-hashnode-devblog">
14+
</p>
1115
</p>
1216

1317
## Installation
@@ -29,7 +33,7 @@ module.exports = {
2933
{
3034
resolve: 'gatsby-source-hashnode-devblog',
3135
options: {
32-
username: '', // Your username on hashnode
36+
username: '', // Your username on hashnode without `@`.
3337
}
3438
}
3539
]
@@ -43,20 +47,17 @@ query MyQuery {
4347
allDevblogPost {
4448
edges {
4549
node {
46-
post {
47-
brief
48-
content
49-
contentMarkdown
50-
coverImage
51-
cuid
52-
dateAdded
53-
dateUpdated
54-
slug
55-
title
56-
type
57-
tags {
58-
name
59-
}
50+
brief
51+
contentMarkdown
52+
coverImage
53+
cuid
54+
dateAdded
55+
dateUpdated
56+
slug
57+
title
58+
type
59+
tags {
60+
name
6061
}
6162
id
6263
}

gatsby-node.js

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,16 @@ const crypto = require("crypto");
33

44
HASHNODE_API_URL = 'https://api.hashnode.com/';
55

6-
async function getCuidsForAllPosts(username) {
7-
let query = `query{ user(username: "` + username + `") {publication {posts { cuid }}}}`;
6+
async function getAllPosts(username) {
7+
let query = `query { user(username: "` + username + `") { publication { posts { cuid slug title type dateUpdated dateAdded contentMarkdown brief coverImage tags { name }}}}}`;
88
let { data } = await axios.post(HASHNODE_API_URL, { query: query });
99
let publication = data.data.user.publication;
1010

1111
if (!publication) {
1212
throw new Error('No publications found for this user.');
1313
}
1414

15-
let posts = publication.posts;
16-
17-
let allCuids = []
18-
for (let post of posts) {
19-
allCuids.push(post.cuid);
20-
}
21-
22-
return allCuids;
23-
}
24-
25-
function getQueryForSinglePostDetail(cuid) {
26-
return cuid + `:` + `post(cuid: "` + cuid + `") { cuid slug title type dateUpdated dateAdded contentMarkdown content brief coverImage tags { name }}`;
27-
}
28-
29-
async function getAllPostDetails(allCuids) {
30-
if (!allCuids.length) {
31-
console.warn('No posts found in the devblog.');
32-
return [];
33-
}
34-
35-
let query = `query{`;
36-
37-
for (let cuid of allCuids) {
38-
query += getQueryForSinglePostDetail(cuid);
39-
}
40-
41-
query += '}';
42-
43-
let { data } = await axios.post(HASHNODE_API_URL, { query: query });
44-
data = data.data;
45-
46-
let posts = [];
47-
48-
for (let postCuid in data) {
49-
posts.push(data[postCuid]);
50-
}
51-
52-
return posts;
15+
return publication.posts;
5316
}
5417

5518
exports.sourceNodes = async ({ actions }, options) => {
@@ -59,14 +22,13 @@ exports.sourceNodes = async ({ actions }, options) => {
5922

6023
const { createNode } = actions;
6124

62-
const allCuids = await getCuidsForAllPosts(options.username);
63-
const posts = await getAllPostDetails(allCuids);
25+
const posts = await getAllPosts(options.username);
6426

6527
for (let post of posts) {
6628
const jsonString = JSON.stringify(post);
6729

6830
const gatsbyNode = {
69-
post: Object.assign({}, post),
31+
...post,
7032
id: `${post.cuid}`,
7133
parent: "__SOURCE__",
7234
children: [],

0 commit comments

Comments
 (0)