Terence Eden. He has a beard and is smiling.
Theme Switcher:

Get all the reactions to your GitHub content using GraphQL

· 3 comments · 850 words · Viewed ~392 times


I am both vain and prurient. A combination which makes me fun at parties and a delight to know.

Sometimes when I raise an issue on GitHub, or write a comment, other users leave me Emoji reactions. Perhaps a 👍 or 🎉 if they like my contribution, but occasionally a 👎 or 😕 if they're foolish enough to think I'm wrong.

The problem is, GitHub doesn't tell me that someone has 🚀'd my wisdom. If GitHub was as good as Facebook, it would present a little 🔔 to let me know exactly how many ❤️s I have received. Instead I have to manually check every issue I've raised to see if the hive-mind judges me worthy.

You might be thinking that there's an API for finding the reaction count to a specific piece of content - and you'd be right! The only problem is that it requires you to send it a specific content ID. So pretty bloody useless unless you want to construct a mega-query of everything you've ever written.

Enter the terrifying world of GraphQL - where men fear to tread and AIs are driven mad. It is possible to squeeze the API until the pips squeak. Here's a GraphQL query, run using the gh CLI, which grabs any issue with over zero reactions, displays how many reactions it received, and who gave what sort of reaction.

 Bashgh api graphql -f query='
query {
  search(query: "author:@me reactions:>0", type: ISSUE, first: 10) {
    nodes {
      ... on Issue {
        url
        reactions(last: 50) {
          totalCount
          nodes {
            content
            user {
              login
            }
          }
        }
      }
    }
  }
}'

As you might be able to decipher, that looks for the 10 most recent issues. If you are prolific, you may want to increase that number - although it will increase the time it takes for the query to run. If you have the temerity to dare to retrieve more than 100, you'll be slapped with the dreaded EXCESSIVE_PAGINATION error.

Similarly, it only gets the most recent 50 reactions. The count will be be the total number of reactions, no matter how low you set the number.

In return, you'll get a hideous mass of JavaScript which looks like it has been vomited up by a disgruntled Cacodemon:

 JSON{
  "data": {
    "search": {
      "nodes": [
       {
          "url": "https://github.com/validator/validator/issues/1814",
          "reactions": {
            "totalCount": 9,
            "nodes": [
              {
                "content": "THUMBS_UP",
                "user": {
                  "login": "markohoza"
                }
              },
              {
                "content": "EYES",
                "user": {
                  "login": "adamwolf"
                }
              },

There is no way to get anything older. If someone liked a comment you made in 2019, you will never know!

If you hate your eyes enough to read through the search type documentation, you'll notice there is no way to search Pull Requests. This is, of course, a rotten lie. If you read different documentation you'll see that PRs are classed as a type of issue. Why? Because your sanity is not worth the cost of updating things.

Anyway, it makes our life slightly easier. We can search both Issues and PRs in one easy to chew lump of GraphQL:

 Bashgh api graphql -f query='
query {
  search(query: "author:@me reactions:>0", type: ISSUE, first: 100) {
    nodes {
      ... on Issue {
        url
        reactions(last: 100) {
          totalCount
          nodes {
            content
            user {
              login
            }
          }
        }
      }
      ... on PullRequest {
        url
        reactions(last: 100) {
          totalCount
          nodes {
            content
            user {
              login
            }
          }
        }
      }
    }
  }
}'

Again, beware gazing into the JSON lest the JSON gazes into you!

 JSON{
  "data": {
    "search": {
      "nodes": [
        {
          "url": "https://github.com/WICG/webmonetization/pull/634",
          "reactions": {
            "totalCount": 2,
            "nodes": [
              {
                "content": "CONFUSED",
                "user": {
                  "login": "tomayac"
                }
              },
              {
                "content": "HEART",
                "user": {
                  "login": "tomayac"
                }
              }
            ]
          }
        },

OK, so it should be pretty damned simple to get the number of reactions to any comments, right? RIGHT?!?!

No. Because consistency is a dirty word and GraphQL was designed in the bowels of hell as a way to keep API developers from ever obtaining a state of grace.

There's no way I could find to use reactions:>0 with a comment search query. This will get you lots of useless unreacted results. I guess you can filter them with jq or just scratch your monitor with razor blades so you don't have to see their empty laughing maws.

 Bashgh api graphql -f query='
query {
  viewer {
    issueComments(last: 10) {
      nodes {
        url
        reactions(last: 10) {
          totalCount
          nodes {
            content
            user {
              login
            }
          }
        }
      }
    }
  }
}'

And, again, JSON nested like wheels within wheels and fires within fires:

 JSON{
  "data": {
    "viewer": {
      "issueComments": {
        "nodes": [
          {
            "url": "https://github.com/home-assistant/supervisor/issues/6474#issuecomment-3740347148",
            "reactions": {
              "totalCount": 0,
              "nodes": []
            }
          },
          {
            "url": "https://github.com/edent/3D-UK-Money/issues/1#issuecomment-3757022146",
            "reactions": {
              "totalCount": 1,
              "nodes": [
                {
                  "content": "THUMBS_UP",
                  "user": {
                    "login": "MickeyF2010"
                  }
                }
              ]
            }
          },

What Have We Learned Today?

The Necronomicon was probably written in GraphQL. Any form of Daemon summoning must use nested queries and frightening syntax.

Trying to track reactions to your content will drive you mad. There's a reason this knowledge is forbidden.

Disclaimer

This post was not sponsored by GitHub. Although I did drink rather too many of their free beers at FOSDEM. Consider this post payback for that self-induced hangover.


Share this post on…

3 thoughts on “Get all the reactions to your GitHub content using GraphQL”

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

See allowed HTML elements: <a href="" title="">
<abbr title="">
<acronym title="">
<b>
<blockquote cite="">
<br>
<cite>
<code>
<del datetime="">
<em>
<i>
<img src="" alt="" title="" srcset="">
<p>
<pre>
<q cite="">
<s>
<strike>
<strong>

To respond on your own website, write a post which contains a link to this post - then enter the URl of your page here. Learn more about WebMentions.