GraphQL Tutorial: How to Build a PHP App

Profile Picture of Damien Filiatrault
Damien Filiatrault
Founder & CEO
PHP and GraphQL logos

Have you heard about GraphQL? Facebook open-sourced it in 2015 and I believe it is a much better way to establish contracts between a client and a server than REST. If, like me, you are always searching for the easiest way to develop and maintain code, this GraphQL Tutorial is for you.

In this article we will:

  • Give a brief introduction to GraphQL
  • Show the improvements of GraphQL vs REST
  • Show you, step by step, how to add a feature to a GraphQL app written in PHP

It’s worth noting that both the code and the application we are going to explore are open source, so you can use it as a foundation for your own work. With such new technologies, ‘best practices’ are constantly evolving. As a result, I have thoroughly researched and refactored all code used in this article and hope it can serve as example code to get you started on your GraphQL journey.

Note: I use the term ‘information’ and ‘informations’ throughout this article to describe, respectively, the singular and plural of an object. This stems from the article originally being written in Portuguese, where it makes sense to use the plural for the word ‘information’.

Table Of Contents

How I Ended up Testing GraphQL

I have been using React as my frontend for a while and was mostly happy with its performance. My apps became a lot cleaner thanks to React’s paradigm. Its beauty lies in the fact that you always have a predictable view for a specific model state. The headaches caused by most bind and listeners are not present, which results in clean and predictable code.

React focuses on rendering the view: having the app state as the starting point and as the single source of truth. To manage that app state I was using the Redux library. Since I started using these two libraries together, there has not been a single day where I’ve lost time tracking the cause of any unexpected view behaviors.

Even with all the above benefits, there was a fundamental problem with something out of the scope of these libraries: data integration with the server. Specifically, asynchronous data integration.

Originally published on Jun 28, 2017Last updated on Oct 28, 2022