Skip to content

Instantly share code, notes, and snippets.

@unclebay143
Created December 3, 2025 08:45
Show Gist options
  • Select an option

  • Save unclebay143/8bdd5cac33f6c5b4b46930745e28bf53 to your computer and use it in GitHub Desktop.

Select an option

Save unclebay143/8bdd5cac33f6c5b4b46930745e28bf53 to your computer and use it in GitHub Desktop.
Hashnode Headless GQL Util to get your blog posts and single post
const HASHNODE_ENDPOINT = 'https://gql.hashnode.com';
const HASHNODE_HOST = ""; // example.com or example.com/blog
export interface HashnodePost {
id: string;
title: string;
subtitle: string;
slug: string;
publishedAt: string;
coverImage?: {
url: string;
};
tags: Array<{
id: string;
name: string;
}>;
author: {
name: string;
profilePicture?: string;
};
readTimeInMinutes: number;
content?: {
markdown: string;
html: string;
text: string;
};
}
interface HashnodePostsResponse {
data: {
publication: {
posts: {
edges: Array<{
node: HashnodePost;
}>;
pageInfo: {
hasNextPage: boolean;
endCursor: string;
};
};
};
};
}
interface HashnodePostResponse {
data: {
publication: {
post: HashnodePost;
};
};
}
export class HashnodeClient {
private static async fetchGraphQL(query: string, variables: Record<string, any> = {}) {
const response = await fetch(HASHNODE_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
variables,
}),
});
if (!response.ok) {
throw new Error(`Hashnode API error: ${response.status}`);
}
return response.json();
}
static async getPosts(first: number = 10, after?: string): Promise<HashnodePostsResponse> {
const query = `
query GetPosts($host: String!, $first: Int!, $after: String) {
publication(host: $host) {
posts(first: $first, after: $after) {
edges {
node {
id
title
subtitle
slug
publishedAt
coverImage {
url
}
content {
markdown
html
text
}
tags {
id
name
}
author {
name
profilePicture
}
readTimeInMinutes
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`;
const variables = {
host: HASHNODE_HOST,
first,
after,
};
const response = await this.fetchGraphQL(query, variables);
return response;
}
static async getPost(slug: string): Promise<HashnodePostResponse> {
const query = `
query GetPost($slug: String!, $host: String!) {
publication(host: $host) {
post(slug: $slug) {
id
title
subtitle
slug
publishedAt
coverImage {
url
}
tags {
id
name
}
author {
name
profilePicture
}
readTimeInMinutes
content {
markdown
html
text
}
}
}
}
`;
const variables = {
slug,
host: HASHNODE_HOST,
};
const result = await this.fetchGraphQL(query, variables);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment