When applying to Frontend Developer positions, I have been trying to recognize trends and patterns in some of the job requirements/technologies they ask for. It can be overwhelming seeing the long list of frameworks and languages some postings require. I tried narrowing down a few reoccurring frameworks try to satisfy as many opportunities as possible. One term I noticed that was showing up repeatedly was GraphQL. I had seen it before but had never looked too far into it. In this post I am going to give a base level overview of what GraphQL is, and why it seems to be so popular.
What is GraphQL?
GraphQL is a query language that allows developers to write queries to API’s, and retrieve the specific data they want. Let’s take a look at what a GraphQL query looks like. Here I am using Graphiql, which is an in-browser IDE that is provided with GraphQL for testing queries.
I have set up a GraphQL server here and am looking to retrieve some information on the users of my application. GraphQL queries in the simplest form, are asking for certain attributes on an object. Here we are asking for the names and emails of all the users in our database. As you can see GraphQL returns JSON that mirror the shape of the query. Pretty nice to look at!
Why use GraphQL?
There is a couple important benefits of using GraphQL over a REST API that I will highlight.
- Over fetching and under fetching: When using REST API’s many a time we retrieve more data than we need for our specific use case. For example, If I wanted all of the users name’s, I would have to fetch all the users and all the attributes of those users come along with it. With GraphQL, I can specify the name attribute in my query and I will only get the names of our users.
On the other side of the spectrum, if I need a user, and also the events that the user attended this year, I would probably have to make multiple requests. Using GraphQL, I can get that data in one request saving time and bandwidth.
2. One route to rule them all: In a REST API we would have many different. routes for GET, POST, PUT, PATCH, DELETE requests, you know the deal. These can become frustrating to remember each route address when fetching from the frontend. GraphQL throws this out the window with just one route for all your requests. No more typing “rails routes” in the console to check if you’ve got it right.
3. Don’t need API versions: API’s get updated and improved over the years that is why you see /v2 and /v3 etc… depending on what version of the API you are working with. It can be a hassle maintaining old versions of of API’s. GraphQL allows you to modify your schema with a deprecated annotation signaling which fields of the API should no longer be used. Much easier.
I am still new to using GraphQL but these are a few of the takeaways I have understood so far. As I become more experienced with it I may have more to discuss on it. From what I have tested so far I would definitely recommend giving it a shot in your next project. For more on GraphQL and how to use it visit the docs.