REST 2.0 Is Here and Its Name Is GraphQL
GraphQL is a query language for APIs. Although it’s fundamentally different than REST, GraphQL can replace serve as an alternative to REST that offers performance, a great developer experience, and very powerful tools.
Throughout this article, we’re going to look at how you might tackle a few common use-cases with REST and GraphQL. This article comes complete with three projects. You will find the code for REST and GraphQL APIs that serve information about popular movies and actors as well as a simple frontend app built with HTML and jQuery.
We’re going to use these APIs to look into how these technologies are different so that we can identify their strengths and weaknesses. To start, however, let’s set the stage by taking a quick look at how these technologies came to be.
The Early Days of the Web
The early days of the web were simple. Web applications began as static HTML documents served over the early internet. Websites advanced to include dynamic content stored in databases (e.g. SQL) and used JavaScript to add interactivity. The vast majority of web content was viewed through web browsers on desktop computers and all was good with the world.
REST: The Rise of the API
Fast forward to 2007 when Steve Jobs introduced the iPhone. In addition to the far-reaching impacts that the smartphone would have on the world, culture, and communications, it also made developers’ lives a lot more complicated. The smartphone disrupted the development status quo. In a few short years, we suddenly had desktops, iPhones, Androids, and tablets.
In response, developers started using RESTful APIs to serve data to applications of all shapes and sizes. The new development model looked something like this:
GraphQL: The Evolution of the API
GraphQL is a query language for APIs that was designed and open-sourced by Facebook. You can think of GraphQL as an alternative to REST for building APIs. Whereas REST is a conceptual model that you can use to design and implement your API, GraphQL is a standardized language, type system, and specification that creates a strong contract between client and server. Having a standard language through which all of our devices communicate simplifies the process of creating large, cross-platform applications.
With GraphQL our diagram simplifies:
GraphQL vs REST
Throughout the rest of this tutorial (no pun intended), I encourage you to follow along with code! You can find the code for this article in the accompanying GitHub repo.
The code includes three projects:
- A RESTful API
- a GraphQL API and
- a simple client web page built with jQuery and HTML.
The projects are purposefully simple and were designed to provide as simple a comparison between these technologies as possible.
If you would like to follow along open up three terminal windows and cd
to the RESTful
, GraphQL
, and Client
directories in the project repository. From each of these directories, run the development server via npm run dev
. Once you have the servers ready, keep reading 🙂
Querying with REST
Our RESTful API contains a few endpoints:
Endpoint | Description |
---|---|
/movies | returns an Array of objects containing links to our movies (e.g. [ { href: ‘http://localhost/movie/1’ } ] |
/movie/:id | returns a single movie with id = :id |
/movie/:id/actors | returns an array of objects containing links to actors in the movie with id = :id |
/actors | returns an Array of objects containing links to actors |
/actor/:id | returns a single actor with id = :id |
/actor/:id/movies | returns an array of objects containing links to movies that the actor with id = :id has acted in |
Note: Our simple data model already has 6 endpoints that we need to maintain and document.
Let’s imagine that we are client developers that need to use our movies API to build a simple web page with HTML and jQuery. To build this page, we need information about our movies as well as the actors that appear in them. Our API has all the functionality we might need so let’s go ahead and fetch the data.
If you open a new terminal and run
curl localhost:3000/movies
You should get a response that looks like this:
[
{
"href": "http://localhost:3000/movie/1"
},
{
"href": "http://localhost:3000/movie/2"
},
{
"href": "http://localhost:3000/movie/3"
},
{
"href": "http://localhost:3000/movie/4"
},
{
"href": "http://localhost:3000/movie/5"
}
]
Continue reading %REST 2.0 Is Here and Its Name Is GraphQL%
LEAVE A REPLY
You must be logged in to post a comment.