GraphQL VS REST an Overview (Part 1/3) | Sovereign Solutions

GraphQL VS REST an Overview (Part 1/3)

Overview

GraphQL is a framework that describes how to ask for and load data from a server to a client. GraphQL lets the client specify exactly what data it needs via a custom query, allows aggregation of data from multiple sources and uses a type system to describe data.  The focus of this posting is to show how GraphQL would compare to the typical REST API approach for applications and illustrate the key differences between the approaches.  This post is the first in a three-part series providing a high-level comparison of GraphQL and REST.

There are two ways to send data over HTTP.  What’s the difference?

GraphQL often presented as a revolutionary new way to think about APIs when compared to REST. Instead of working with rigid, server-defined endpoints, a consumer can send queries to get all the data of interest in a single request. Although GraphQL can be transformative because it enables frontend and backend teams to collaborate and share data in a more streamlined manner, in practice both approaches involve sending an HTTP request and receiving some result and GraphQL has many elements of the REST model built-in.

What’s the real difference on a technical level? What are the similarities and differences between these two API paradigms?  GraphQL and REST are not so different, but that GraphQL has some small changes that make a big difference when building and consuming APIs.

Let’s identify some properties of an API and discuss how GraphQL and REST handle them.

 

RESOURCES

The core idea of REST is the resource. Each resource is identified by a URL and are retrieved by sending a GET request to that URL. Most requests would get a JSON response that looks similar to this:

GET /books/1{
“title”: “Black Hole Blues”,
“author”: {
“firstName”: “Janna”,
“lastName”: “Levin”
}
// … more fields here
}

Note: In the example above, some REST APIs would return “author” as a separate resource.

One thing to note in REST is that the type, or shape, of the resource and the way the resource is retrieved are coupled. This would typically be referred to it as the book endpoint.  GraphQL is quite different in this respect, because in GraphQL these two concepts are completely separate. A sample schema is provided using Book and Author types:

type Book {
id: ID
title: String
published: Date
price: String
author: Author
}type Author {
id: ID
firstName: String
lastName: String
books: [Book]
}

Note that the code describes the kinds of data available, but the description doesn’t tell anything about how those objects might be fetched by a client. This is a core difference between REST and GraphQL:  the description of a resource is not coupled to the way it is retrieved.

To be able to access a particular book or author, a Query type must be created in the schema:

type Query {
book(id: ID!): Book
author(id: ID!): Author
}

Here is a GraphQL request similar to the REST request above:

GET /graphql?query={ book(id: “1”) { title, author { firstName } } }{
“title”: “Black Hole Blues”,
“author”: {
“firstName”: “Janna”,
}
}

Upon initial inspection, it is clear there are a few things about GraphQL that are quite different from REST, even though both can be requested via URL and both can return the same shape of JSON response.  First, the URL with a GraphQL query specifies the resource being requested and which fields are needed. Second, rather than the server author deciding that the related Author resource needs to be included, the consumer of the API can determine that for themselves.  Third, and most importantly, the identities of the resources and the concepts of Books and Authors are not coupled to the way those resources are retrieved. A consumer could potentially retrieve the same Book through many different types of queries and with different sets of fields based on the data required by the consumer.

CONCLUSION

Upon initial review, some similarities and differences are already clear:

  • Similarities
    • Both have the idea of a resource, and can specify IDs for those resources.
    • Both can be fetched via an HTTP GET request with a URL.
    • Both can return JSON data in the request.
  • Differences
    • In REST, the endpoint that is called is the identity of that object. In GraphQL, the identity is separate from how it is retrieved.
    • In REST, the shape and size of the resource is determined by the server. In GraphQL, the server declares what resources are available, and the client asks for what it needs at the time.

Go to part 2 GraphQL VS REST click here 


Talk to our experts now to learn how Sovereign Solutions can help your organization meet its business objectives

*This content is being provided for informational and educational purposes, and should not be regarded as a specific recommendation for your situation.