Skip to main content

Command Palette

Search for a command to run...

Hashnode's GraphQL API for RSS feeds

Using GraphQL as an alternative to REST API for Hashnode

Updated
1 min read
Hashnode's GraphQL API for RSS feeds
A
I am a web developer from Navi Mumbai. Mainly dealt with LAMP stack, now into Django and getting into Laravel and Cloud. Founder of nerul.in and gaali.in

All this while, parsing my blog's RSS feed from https://anjanesh.dev/rss.xml on my server was possible to extract the contents of title, description, link, image and date to be inserted at my NextJS powered template at https://anjanesh.com/blogs.

But Hashnode recently made some changes to their blogging system where the front-end seem to sit on a Vercel.

php blog.rss.php
PHP Warning:  file_get_contents(https://anjanesh.dev/rss.xml): failed to open stream: HTTP request failed! HTTP/1.0 429 Too Many Requests

It looks we've hit a Vercel rate limit, as per https://docs.vercel.com/docs/rest-api/reference/welcome#rate-limits. But I got it working using their GraphQL API though - https://docs.hashnode.com/quickstart/introduction

function fetch_hashnode_posts($domain)
{
    $url = "https://gql.hashnode.com";

    $query = '
    query {
      publication(host: "' . $domain . '") {
        posts(first: 10) {
          edges {
            node {
              title
              brief
              url
              publishedAt
              coverImage {
                url
              }
            }
          }
        }
      }
    }';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['query' => $query]));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'User-Agent: My-Hashnode/2.5'
    ]);

    $response = curl_exec($ch);
    $result = json_decode($response, true);
    curl_close($ch);

    return $result;
}

This has got me interested in knowing more about GraphQL which seem to be the way forward from REST API, esp RSS feeds.