The Core Difference Between REST API and GraphQL

Introduction

GraphQL is often touted as an alternative to REST APIs. In this section, we will look at the gap between GraphQL and REST with an example and look at how they both can synchronize and complement each other.

GraphQL is typically introduced in comparison to REST, but at this point, these comparisons are very common and cover many of the basics differences between GraphQL and REST. Instead of reiterating these points, this article will focus on a few more variation differences between GraphQL and REST.

REST API

What is REST API?

With REST, we may have a /authors/:id endpoints to fetch an author and another /authors/:id/posts endpoints to fetch the post of that particular author. At last, we could have /authors/:id/posts/:id/comments endpoints that fetch the comments on the post.

Client-Server

In other words, I should be able to make changes to my mobile application without impacting either the data structure or the database design on the server. At the same time, I should be able to update the database or make changes to my server application without affecting the mobile client.

Stateless

Instead, each call has the necessary data in itself, such as the API key, access token, user ID, etc. This also helps increase the API’s reliability by having all of the data necessary to make the call, instead of relying on a series of calls with server state to create an object, which may result in partial fails.

HTTP Methods

HTTP Methods
HTTP Methods
HTTP Methods

HTTP Response Status Code

HTTP Response Status Code
HTTP Response Status Code
HTTP Response Status Code

Pros

  • High performance (especially over HTTP2)
  • Proven for decades
  • Works with any representation
  • Affordance-centric

Cons

  • Poor or no tooling for clients
  • No framework or tooling guidance
  • Requires discipline on all sides
  • Challenging to keep consistency and any governance

GraphQL

What is GraphQL?

GraphQL has three main characteristics:

  • It lets the client specify exactly what data it needs.
  • It makes it easier to aggregate data from multiple sources.
  • It uses a type system to describe data.

Pros

  • Easy to produce and consume
  • Lots of hand-holding
  • Contract-driven by nature
  • Built-in introspection

Cons

  • Query optimization
  • You are on your own with scaling and performance
  • JSON representation only

About GraphQL mutations

If queries are the GraphQL equivalent to GET calls in REST, then mutations represent the state-changing methods in REST (like DELETE, PUT, PATCH, etc).

A query to fetch all the pets from the app might look like this:
query GetAllPets {
pets { name petType }
}

And then a mutation that adds a new pet might look a little something like this:
mutation AddNewPet ($name: String!, $petType: PetType) {
addPet(name: $name, petType: $petType) {
id name petType }
}

Example:
Query:-
query { hero { name height mass } }

Response:-
{ "hero": { "name": "Luke Skywalker", "height": 1.72, "mass": 77 } }

Schemas

But to complete a schema, you usually need to add GraphQL operations. The GraphQL operations provide the information needed to generate and validate operations on the data graph.

  • Object type
  • Scalar type
  • Query type
  • Mutation type

type BlogPost {
...
}

type Author {
...
}

Types

The data fields for the Blogpost entities are title and content, and both are type String. If a field is mandatory, specify this with an exclamation point.
type Blogpost {
title: String!
content: String!
}

type Author {
name: String!
}

Conclusion

Originally published at Reveation Labs Blog Page

Custom Software Development Solutions For Startups and Enterprise Businesses.