Introducing a PHP library for the Hashnode API

When I heard about the Hashnode Christmas Hackathon, I immediately began brainstorming ideas to develop something that members of the community might find useful, but would also be a great learning experience for me. After all, learning is at the heart of Hashnode.

In any case, after numerous scrapped ideas and half-baked prototypes (that I’ll talk about in an upcoming article!) I decided that I would go in the direction of building a library – something that I have never done before, and using PHP, a programming language that I’m quite familiar with. By working on this project, it was my ambition to learn more about GraphQL, the technology that Hashnode’s API (Application Programming Interface) utilises, and something I previously knew nothing about!

Enter, an unofficial PHP library for interacting with the Hashnode API in PHP.

How can I use it?

Visit the GitHub Repository

Update: The library is installable via Composer.

composer require oliverearl/php-hashnode

In your own code, you can instantiate a client instance and a root query object like this:

$client = new Hashnode();

$query = new RootQuery();

You can also specify a custom endpoint, or your Hashnode API token by adding it as a parameter to the Hashnode constructor:

$client = new Hashnode('https://some-other-endpoint.hashnode.com', [
  'Authorization' => 'token-goes-here'
]);

For more information on how to use the library, including how to retrieve data from the API, I strongly encourage you to view the repository’s README! I hope to write more in-depth documentation soon as it’s on the development roadmap!

You do need PHP 7.4 (or above) for the time being. Sorry about that!

Is it complete?

Almost. I really wanted to have it completely done before New Year’s Day, but truth be told I have had other responsibilities and familial commitments throughout the winter break that limited my free time. I hope you can understand!

However, all functionality to retrieve information from Hashnode is complete!

The development process

Originally, I wanted to develop something that was similar to how Laravel works with its models, where you could say $post->get(); and retrieve something, but this is currently out-of-my-depth and is something I’m hoping to be able to do in the future with more learning. I used a GraphQL client library for PHP and attempted to use the repository pattern to build ways of wrapping queries in method calls, but I realised that it would be impossible to build flexible GraphQL queries in the way that I wanted.

My gripes with GraphQL

One of the things I found most frustrating with GraphQL as part of its inherent design.

You only get what you request.

This wasn’t like what I was used to when working with RESTful APIs where I could retrieve everything that I wanted by accessing the appropriate endpoint. Instead, there was one endpoint, and I had to build the appropriate query. I was immensely frustrated at my lack of knowledge and knew I had to find another way.

I remember getting so angry that I accused GraphQL of being ‘rehashed SOAP with JSON’ and retiring to bed. After a good night’s sleep, I realised that I was just ignorant of what I didn’t fully understand – and that’s okay. So I got back to work on the project, but knew I needed a new approach.

Enter the GraphQL OQM library.

After a bit of research, I found the author of the GraphQL library had also developed another library for working with objects mapped to queries, or OQM (Object-Query Mapping). With this library, I was able to generate and map queries and arguments into classes of their own. Whilst this sounds straightforward, I spent a lot of time rewriting the code into PHP 7.4 typed code and organising things into a structure that was easy to work with for someone who is relatively new to object-oriented PHP.

Without discipline, agile methodologies can collapse

One of the most important things I learned while studying software engineering is that successful software projects require a careful balance of discipline and agility.

Without agility, you’re basically working to a methodology like Waterfall where the process is too rigid.

Without discipline, you’re just hacking code together and abandoning the process and any kind of planning. Mistakes and problems can then creep in.

I try to use Test-Driven Development (TDD) in all of my projects, where I will write tests and then get them to pass by implementing the required behaviour. By the time I went back to the drawing board and started again from scratch using the OQM library, I was writing and modifying code without testing it properly, so I had to go back and write tests afterwards. This is less than ideal, and I’m hoping to have more thorough test coverage and integration tests going forward. But since I was working solo, it wasn’t a fatal mistake to make.

What remains and how you can get involved

This project is something that I want to keep working on in the future! There is still some work to be done (a full list is available on the repository readme) and there are undoubtedly areas where the code can be improved, or maybe there are bugs.

If you want to get involved, some key areas that need implementing are:

  • Documentation! I haven’t had the time to write a great deal of documentation, but if you’re a keen writer, this would be a great place to help out.
  • Integration tests – while a (semi-complete) suite of unit tests exists, more elaborate tests that test against full queries need to be orchestrated.
  • Implementation of code that performs server-side mutation – in other words, we can currently retrieve data, but updating, deleting, and creating data is yet to be done!
  • Build some single-file examples of the API doing cool things and pull requesting them to join the examples directory!

What I learned

Despite me starting a new PHP software engineering job this January and having confidently worked with PHP and Laravel on a number of projects throughout my career I felt relatively out of my depth this time. I realised that it was okay to not know absolutely everything and that I shouldn’t get too frustrated when I can’t wrap my head around a concept the first time around. Something that I should probably know better by now.

I learned that although I was frustrated with GraphQL for not being able to just retrieve everything that I wanted, I did acknowledge that it’s incredibly efficient as it does just that. Returns what you want.

Conclusions

I hope you enjoyed my little write-up and I hope you enjoy playing with the Hashnode PHP library as much as I did developing it! Of course, it’s not perfect and I’m not the best programmer going, so if you can help me improve it, I would love your help and hope to see you on GitHub!

Thanks again to everyone at Hashnode and I hope that you have had a wonderful holiday season and a merry Christmas to those celebrating. Have a safe and fun New Year’s Eve and I’ll see you in 2021!

What I learned from the Hashnode Writing Bootcamp

As an academic and a teacher, I spend a ridiculous amount of time writing. I always believed that I was pretty good at it and always wanted to write even more online. I was enamoured with the concept of technical writing here on Hashnode, but I genuinely had no real idea where to start, what to write about, or how on Earth I was going to reach anyone when I did decide to start scribbling.

To my delight, Hashnode announced a boot camp where budding writers including myself could come together and learn from some of the most experienced and talented writers on the platform. They were ultimately Catalin PitOmotola ShogunleTapas Adhikary, and Victoria Lo, who gave wonderfully insightful glimpses into their writing journeys, their methodologies and thought processes behind each article, and how to fend off the ever-present dragon of procrastination when their motivation levels run low. It was a humbling experience and one I remain immensely grateful for the opportunity to have participated in.

Here are some of the biggest takeaways from the boot camp as a whole that I’m looking to incorporate into my own writing.

Keep it simple, stupid

My writing is often fluffy and elaborate, making it somewhat difficult for second-language speakers of English to read. I’m afraid this is symptomatic of spending so much time in the ivory tower!

Nevertheless, I have made a conscientious effort in my writing to primarily use active voice, as opposed to passive voice, to always explain acronyms whenever I first use them, and to try to structure my writing into clear, easy-to-digest paragraphs with headings and subheadings as appropriate.

Consistency is the key, but value quality over quantity

Write often and write consistently, but provide value in your writing – what can someone learn from you? What unique perspective do you provide? These are all important questions that we can ask ourselves as we decide on a topic to write about and share with the Internet. As someone with a great deal of experience both as a developer and as an educator, I think that this provides me with a unique viewpoint and perspective to share with the rest of the community.

A high quality, consistent blog is an amazing resource for demonstrating not only your commitment to an ongoing project but both your technical and writing skills at the same time.

Branding and promotion

I’m not great at this because I’m not a huge lover of social media – it took a fair bit of convincing for me to come back to Twitter! But in the long run, it cannot be denied that it remains a powerful utility for sharing your writing with developer communities and with like-minded individuals who will find value in your work.

Sharing your writing on other communities such as freeCodeCamp, Dev, and Medium are also options for furthering the reach of your writing, sometimes even opening up paid opportunities, though personally, I prefer that they remain centralised here.

I’ve taken steps to adopt a unique and consistent design pattern in my images and in my blog design using Adobe Spark, so I hope that I reap the benefit of that down the line as I continue my writing career.

Find a niche

Carving out a niche is a great strategy for building a reader base who will repeatedly come back to read what you’ve written about. Whether that’s about web development, cybersecurity, Python scripting and machine learning, whatever. Establish your niche and maximise upon it.

I’m considering giving this a greater deal of focus in the near future, but it’s really difficult when your interests are incredibly broad and you want to write about everything. Perhaps with time, I will naturally establish my own niche and my own audience who will appreciate my perspective and the links between the topics which I write about.

Conclusion

In conclusion, I want to once again thank the amazing folks at Hashnode for running such a well-coordinated and ambitious remote boot camp, and wish them the best of success in the future! I look forward to the post-COVID future when we can look forward to in-person writing boot camps. Perhaps I’ll see you there!

Furthermore, I’d once again like to thank the aforementioned writing veterans for their time, their expertise, and above all their infectious passion and inspiration that they provide to all of us within their community. Thank you!