DEV Community

dow2
dow2

Posted on

Rest API vs GraphQL

If you are deep enough into the study and practice of software development to have come across the concept of Application Programming Interface or API for short then I am more than sure you have come to grips with the fact that there is far more than one way to accomplish a specific goal. The topic we are going to discuss today is not exempt of this basic fundamental truth of development, so we will preface this discussion by admitting that the desired outcome in the implementation of API's could be executed in both restful and GraphQL API's however, the way you go about achieving the desired effect differs greatly. My main task today is to highlight these differences and provide context for when you might want to use one or the other.

First what is an API?

  • An application programming interface is a way for two or more computer programs to communicate with each other.
  • It is a type of software interface, offering a service to other pieces of software.
  • A standard used to describes how to build a connection or interface is called an API.
  • A contract between an information provider and an information use which establishes the content required from the consumer and the content required by the producer… the req and res

In the early 2000's a group of expert developers came to the conclusion that the state of web API's could benefit from a rework in how they were created and implemented into applications. This realization led to the creation of RESTful API's. Due to the fact that there was no industry standard, API's as a whole were comprised of hard to handle complex protocols usually in the form of SOAP. The original goal in the creation of RESTful API's was to to come with a standard that allowed two servers to communicate and exchange information anywhere in the world.

The first early mainstream adopters were Ebay and Amazon in the early 2000’s. These companies benefited greatly due to the fact users could see listings and other pertinent info about the company without explicitly being on the companies website as long as the user was on a website which utilized the rest api.

Characteristics of Rest

  • Has multiple endpoints

GET/users
Post/products

  • Exchanges JSON Data
  • Can work with all server-side and front-end frameworks or languages
  • Stateless
  • URL- Driven - Uses HTTP Verbs along with endpoints and possibly a body to interact with and send data from a server to a client

Image description

Just as in the early 2000’s when Restful API’s were invented to deal with the shortcomings of using raw soap protocols, in 2012 a team of Developers at facebook came up with GraphQL to deal with the shortcomings of the decade old REST practices. It had been widely discovered by this point that restful apis often over fetched and under fetched resources due to their use of multiple endpoints. During development of facebook's news feed feature the shape of the data assumed by the front-end was different than the one being sent by the back-end APIs, creating the need for a new way of passing data along from client to server… GRAPHql was created in response to this problem. GraphQL is a static strong-typed query language which allows clients declaratively specify their data requirements. Clients specify the shape of the data that they need and the server responds back with the exact same data as the response.

Characteristics of GraphQL

  • Has one endpoint

POST/graphql

  • Exchanges JSON Data
  • Can work with all server-side and front-end frameworks or languages
  • Stateless
  • Query Language driven
  • Uses HTTP Verbs along with a single fixed path and a body consisting of a query language

Example of GRAPHql Query and Response
Image description

Top comments (0)