GraphQL vs REST API?

Subscribe to my newsletter and never miss my upcoming articles

GraphQL stands for Graph Query language while REST stands for Representational State Transfer. Both are good API architectures used in developing complex and efficient systems/web applications.

Firstly, what exactly is API architecture/design?
According to APIacademy, the term “API design” or “API architecture” refers to the process of developing a software interface that exposes backend data and application functionality for use in new applications. An API is an application programming interface. It is a set of rules that allow programs to talk to each other.

Understanding a bit about the REST(Representational State Transfer) RESTful API
Basically REST APIs are API endpoints whereby a specific set of data is returned from a specific endpoint. An API endpoint is simply a route or URL.

This is an example of a REST API endpoint: https://www.flowers.com/api/resource

https://www.flowers.com - specifies the root endpoint.

Then the path /api/resource - specifies exactly what you are asking for.

Understanding a bit about the Graph Query language(GraphQL)

GraphQL is known for its flexibility, It uses a fixed endpoint, whereby the structure of the data that is returned from a GraphQl API is not fixed and the client can decide exactly what data is needed.


Overview:

So many debates have sparked up in different conferences meet-ups, etc. about how GraphQL might likely kill of REST. In the actual sense, none will kill each other because they serve different and good purposes.

  • GraphQL is much more complex than REST API, and as a newbie web developer, it's better to start with REST so as to grasp the basic and proper HTTP fundamentals(protocols/requests/methods/status codes etc).

Let us compare deeply further:

  • The Rest API architecture is the best for beginners to start with because, in the process of understanding the architecture, they get to learn more about core HTTP standards like methods, status codes, data format and etc, while GraphQL does not have a standard HTTP format but work with schema types such as; mutation, query, and subscription that works with HTTP.

For example; To test for a user signing up using Rest Api with a Rest Api Client will be like the following:

sample API endpoint: **https://www.flowers.com/api/signup**
- using the POST HTTP method
- with a sample request like the following:
{
    "fullname": "funmi olaiya",
    "email": "funmiolaiya@gmail.com",
    "password": "funmi1234"
}

then a response will be gotten with a status code of 201, which will be specified by the developer.

{
  "status": "201",
  "data": [{
       "fullname": "funmi olaiya",
       "email": "funmiolaiya@gmail.com",
       "password": "funmi1234"
    }],
}

For example; since GraphQl uses a mutation query that helps to modify or add data. Note that the values are also typed.

mutation ($fullname: String!, $email: String!, $password: String!) {
    insert_users(objects: {fullname: $fullname, email: $email, password: $password}) {
      returning {
        id
        fullname,
        email,
        password
      }
    }
}

using query variables such as;

{"fullname": "funmi olaiya", "email": "funmiolaiya@gmail.com", "password": "funmi1234"}

a response can be gotten as such like the following:

{
  "data": {
    "insert_users": {
      "returning": [
        {
          "id": 1,
          "fullname": "funmi olaiya",
          "email": "funmiolaiya@gmail.com",
          "password": "funmi1234"
        }
      ]
    }
  }
}

  • In as much as I will encourage newbie developers to start with the REST API architecture, I believe it totally depends on the path you wish to go with/find yourself. Frontend developers would find the use of GraphQL much easier, especially combining it with serverless providers like Hasura etc. I believe both APIs should always be addressed from the business point of view and core requirements than shallow points/opinions.

  • For Restful APIS, you definitely have to specify different URLs, while GraphQL uses only endpoint/URL in which you can send any query.

  • GraphQL is strongly-typed while REST API does not require its endpoints to be.

  • There is something also called over-fetching or under-fetching whereby the REST API sends more data or less data than it needs while for GraphQL, the client can specify what data it wants to be returned.

These are some key points I found while working with both.

Thanks for reading!

Augusta Ehihebolo's photo

This is really enlightening, thanks. I just subscribed to your newsletter for updates, your contents are great.

Funmilayo E. Olaiya's photo

Thanks Augusta!

Deng Beny's photo

thank you this was informative.