DEV Community

Cover image for What is the difference between REST and GraphQL? An Introduction
Lorenzo Zarantonello for This is Learning

Posted on • Updated on • Originally published at vitainbeta.org

What is the difference between REST and GraphQL? An Introduction

Let's start by saying that REST and GraphQL are two API design styles that can achieve the same thing: transferring data over the internet.

Despite this common goal, the way they go after it differs greatly. Here are three main differences that you should know:

1. REST is an architectural pattern and GraphQL is a query language

This is the first major difference and it stems from the problem that each wants to solve.

  • REST was first introduced in 2000, when internet was still a baby. During those times, many things were wild so REST aimed at standardizing different ways of creating URLs across applications. Therefore, we needed an architectural pattern and REST brought a standard in the Wild Internet!

  • GraphQL was open-sourced in 2015. During those times, REST was reliable and popular but applications grew bigger and bigger and so the amount of data that gets transferred. Sometimes, REST fetched unneccesary data from a server (e.g. over-fetching) so we needed a way to precisely define the data we wanted to fetch. What about a query language?

Lee Byron, Director of the GraphQL Foundation on GraphQL:

When we first created GraphQL, we set out to make product development easier with APIs driven by the needs of mobile and app developers

2. While REST hits multiple endpoints to retrieve data, GraphQL uses a single request

REST APIs allow fetching information in a simple way. However, if you want to get information from two or more objects, you need to perform two or more requests.

GraphQL uses a query language to tailor the request to fetch exactly what you need, from the whole dataset to specific objects or fields within each object.

3. While REST is prone to over-fetching, GraphQL defines what to fetch

REST is prone to fetch too much data (over-fetching) or too little data (under-fetching). While GraphQL allows clients to precisely define the data to fetch.

Other differences between REST and GraphQL

As you can imagine, there are some other differences between the two API design styles. You can find a quick reference on other differences in this article.

So, what is best?

The answer, of course, is: it depends.
As discussed in this article, despite GraphQL being more and more important, REST is still the industry standard.

REST endpoints are mature and this is a very strong point in some industries like finance and banking. While GraphQL simplifies data consumption, REST design standards are strongly favored by many sectors due to cache-ability features, security, tooling community, and ultimate reliability.

Top comments (0)