New way to REST - Brief intro to GraphQL

This tutorial provides basic introduction to GraphQL. It is for folks who are looking to improve the way of implementing APIs. No prior knowledge is required , basic idea regarding APIs and REST would be helpful.

Let’s Begin 🚀

GraphQL is a new standard for APIs rather new way of fetching them. It is a efficient, powerful and flexible alternative to REST.

But wait , what is an API ? - API stands for Application Programming Interface - An API is a set of definitions and protocols for building and integrating application software. - In a more detiailed explanation , API is a set of functions and procedures allowing the creation of applications that access the features or data of an operating system, application, or other service.

Read more about APIs in this article.

World of REST 🌐

REST (REpresentational State Transfer) is an architectural style for developing web services making it easier for systems to communicate with each other.

Particular advantage of REST is the implementation of the client and the implementation of the server can be done independently without each knowing about the other.

REST is a way of designing web APIs. It offers some great ideas, such as stateless servers and structured access to resources. Read more here

REST API Basics

Image source

However, REST APIs have shown to be too inflexible to keep up with the rapidly changing requirements of the clients that access them.

Why GraphQL ?

From Official docs - > GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data.

Important thing to note here is that GraphQL is not a database ( like Mongo or SQL) , its a query language. Thus, it can be used anywhere where APIs are used.

GraphQL was open sourced by Facebook in 2015. ⭐️

GraphQL was developed to cope with the need for more flexibility and efficiency! It solves many of the shortcomings and inefficiencies that developers experience when interacting with REST APIs.

Advantages of GraphQL over REST -

  • Most important advantage is better performance over data fetching.
  • In REST , we make multiple calls for accessing related data.
  • GraphQL fetches exactly what is required by the user , nothing less or more. Whereas REST has certain endpoints defined , which makes it obvious that all possible endpoints cannot be made. Different users have different requirements of using API.

Read detailed difference here.

Example to demonstrate !!

Lots of theory , let’s understand this with an example ( Not TODO for sure :-P )

We’ll take real world example of say how social media like Twitter works. We will have some endpoints in REST like -

    /users
    /users/:id/articles
    /users/:id/article/:id

We would thereby require 3 different calls to achieve all the information required.

Well , this is where Graphql does the magic. To fetch same amount of information , it would require a single network call and query would be -

query {
    users ( id: 'xyz' ) {
      posts (id : 'abc') {
        title
        comments
      }
    }
}

I guess now you have a feel how fetching of APIs is made simple and efficient using graphql.

Language of GraphQL

GraphQL has its own defined type system and way for querying APIs. GraphQL defined SDL i.e Schema Definition Language which is a concise way of defining schemas.

Schema of above discussed example would be something like this :

type User {
  id: String!
  name: String!
  email: String!
  posts: [Post!]!
}

type Post {
  id: String!
  title: String!
  description: String!
  publishedAt: DateTime!
  likes: Int! @default(value: 0)
  author: User!
}

We can perform basic queries like fetching all posts using -

{
  posts {
    title
    author
  }
}

This would return -

{
  posts : [
    {title : "GraphQL Intro", name: "Mohit Khare"},
    {title : "Deploying Python Package", name: "Mohit Khare"},
  ]
}

We now know , we can read and access data using queries. But to add, delete, modify and update data we need Mutations.

Suppose we need to create a new post , we can create it by mutation -

mutation {
  createPost(title: "New Post", author: "Mohit Khare", description : "...", publisedAt: "...") {
    title
    author
    description
    publishedAt
    ....
  }
}

Interested in GraphQL 💡

Check out these awesome resources:

Feel free to suggest changes and improvements. I’m in learning phase too 👷

Suggest me topics you would like me to 📝. Thanks, Keep sharing the post and knowledge ✅

Connect with me 🚀


See also