Comments

Why Should I Pick GraphQL?

Have you heard of GraphQL and wondering if it would be a good fit for your next project? In this post, we are going to understand what GraphQL is, and its benefits.

Introduction to GraphQL

If you are new to GraphQL, here is the long story short. GraphQL is the new specification that you can use for your API. It is an alternative to writing APIs using REST.

It was developed by Facebook as an internal solution for their mobile apps, and was later open sourced to the community. Ever since then, it has been widely popular among developers and has become a favorite solution for building services.

GraphQL is a query language and is agnostic of the language you use. It is just a specification. The client can query for the data they need from the server. And the server responds back with a JSON response to the query. The interesting thing to note here is that, the client can ask for exactly what they need, and they receive only that. Yes!

Why GraphQL?

Before picking any technology, we need to understand the reason behind choosing it. Let’s dive right it and understand why GraphQL can fit your development needs.

1. Superior Developer Experience

The biggest benefit of using GraphQL, is the superior developer experience that it provides in comparison to alternatives like REST. An interesting thing to note is that with GraphQL, frontend developers stand to benefit a ton. The client driven approach provides a lot of power to the clients to query for what they like by creating custom queries.

No versioning of APIs

This may come as a surprise to some developers. GraphQL has introduced developers to a whole new era in API design and development. Since the client only asks for the data that it needs, it has made API design using GraphQL very flexible. There is no versioning of APIs and adding new fields has no effect on the current client’s call to the APIs. What does this mean? Well, developers do not have to deal with the huge headache of maintaining multiple versions of the API.

The APIs can be continuously evolved without changing versions.

Modifications to GraphQL APIs are non-breaking changes. This principle is very desirable to developers, so that they can continue with their work with no hassle. GraphQL provides a higher level of flexibility in API development that developers have not seen before.

Teams work independently

With GraphQL, multiple teams can work independently in parallel. Have you been in a situation where the frontend team stalls their development, because they are waiting on the backend team to release the latest version of their API or finalize it? It is very common to run into situations where, the frontend/client teams are stalled while they wait on the final version of the API. This causes delays in development and leads to frustration.

With GraphQL, the front-end and back-end teams can work independently. With the strictly typed schema that GraphQL has, the teams can work in parallel. The front-end team can continue working with just a mock version of the API. They can also test the code with it. This gives the developers a pleasant experience without stalling their development work.

2. Declarative Data Fetching

One of the biggest perks of using GraphQL is its declarative data fetching approach.

GraphQL: No multiple rounds trips to server, no over-fetching and no under-fetching of data

Queries can be aggregated to ask for exactly what you want in one trip. With this approach, you can eliminate any over-fetching or under-fetching of data. You ask for what exactly you need and GraphQL gives you back what you asked for.

GraphQL Query in Action

Let’s try to understand this better with an example below. Let’s assume that we want to access the data in the programmingwithmosh.com blog. I want to build an app, that displays the blog author information. I want it to display the name of the author, the blog posts written by the author and the three most recent blog topics written by the author.

GraphQL Query Request

Our query will look like below:

 {
  author (id: 6) {
    name 
    posts (last: 3) {
      title
    }
    topics (last : 3) {
     name
    }
  }
}

We pass in the fields that we need response for. We are looking for the author’s name, last three blog posts written, and the last three topics they wrote. The query is structured to ask for exactly what we need.

GraphQL Query Response

Here is what we get back from the server in the form of a JSON response.

{
  "data" : {
    "author" : {
      "name" : "Adhithi Ravichandran",
      "posts" : [
        { title: "React vs. Vue : A Wholesome Comparison"},
        { title: "React Lifecycle Methods: A Deep Dive"},
        { title: "5 Essential Skills A Frontend Developer Should Possess"}
      ],
      "topics" : [
        { name: "React and Vue"},
        { name: "React"},
        { name: "General"}
      ]
    }
  }
}

We got a JSON response back from the server that has exactly what we asked for. It has returned the author name, the last three posts written by the author and the last three topics authored by author. Nothing more, nothing less and everything in a single trip to the server. Isn’t this amazing?!

3. Strongly Typed Schema

Another reason why you should use GraphQL is because of its strongly typed schema. We know that GraphQL is essentially a query language, and it is strongly typed. What this means is that the GraphQL schema should have types for all the objects that it uses. Thus, the schema acts as a contract between the client and the server. The example below shows how types are defined in a GraphQL schema.

type Person {
 id : ID
 name: String
 age: Int
 gender: String
}

We have defined a complex object Person of type Person. The object comprises of several fields, each with its own type.

The strongly typed GraphQL schema has several advantages.

  • Code is predictable
  • Schema acts as contract between client and server
  • Teams can work independently
  • Can detect errors early

Conclusions

I hope you enjoyed learning about why GraphQL is a good fit for your next project. Here are some resources that will help you get started with GraphQL.
Resources:
If you have any comments, please post your comments below and share this post with your team and friends. To keep in touch, you can follow me on twitter @AdhithiRavi.

 

Adhithi Ravichandran is a Senior Software Engineer based in Kansas City. She is currently working on building mobile apps using React Native for critical healthcare scenarios. Adhithi is a regular conference speaker at various tech conferences. Adhithi graduated with a Masters degree in Computer Science from the University of Kansas and has been in the tech industry for over 7 years. She has worked on several technologies over the years, and one of her recent favorite is React Native.
Tags: , , ,

Leave a Reply

Connect with Me
  • Categories
  • Popular Posts