Created
December 3, 2025 08:45
-
-
Save unclebay143/8bdd5cac33f6c5b4b46930745e28bf53 to your computer and use it in GitHub Desktop.
Hashnode Headless GQL Util to get your blog posts and single post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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