Introduction To GraphQL(REST alternative)

Hi, this is my first blog of a series in which I will publish IT content every month (2020 blogs challenge)

I will start by a topic that I’m so excited to learn and to implement : “GraphQL”

Introduction :

Image for post
Image for post
source: https://itnext.io/javascript-fundamentals-an-introduction-to-rest-apis-7cbe8a809d3b

In 2000, when REST was developed, client applications were relatively simple. REST was a good solution for many applications that need to communicate and exchange data over the internet anywhere in the world. And it was a good alternative for the existing solution SOAP (Simple Object Access Protocol). REST gave us important concepts for API design and offered us advantages over SOAP.

REST gave us important concepts for API design and offers us advantages over SOAP

  • Stateless servers and structured access to resources
  • REST-based applications can be written using any programming language
  • Allows a greater variety of data formats
  • Provides superior performance, particularly through caching for information
Image for post
Image for post
Source: https://restful.io/rest-api-back-to-basics-c64f282d972

However, since that time APIs have gotten more complex and data-driven affected by the following factors:

  • Increased mobile usage created a need for more efficient data loading.
  • A variety of different clients: REST makes it difficult to build an API that satisfies their needs, as it returns a fixed data structure.
  • Expectations for faster feature development: To make a change on the client-side in REST, often we have to adjust the server side to support it, which slows product iterations.

And to adjust REST API implementation wit the client needs it will cause :

  • A lot of endpoints
  • It’ll be much harder for developers to learn and understand your API
  • There is over- and under-fetching of information
Image for post
Image for post

The client still requests complex combinations of data from the server,

So the solution to this is graphql

Image for post
Image for post

What is Graphql?

Graphql Overview :

  • GraphQL It’s just specification or implementation between the client and the server
  • GraphQL is a new API standard that provides a more efficient, powerful and flexible alternative to REST
  • GraphQL is a modern data query language.
  • GraphQL is a new way to think about building and querying APIs.
  • GraphQL is not a library, but a language spec.
  • GraphQL is the SQL for APIs.
  • GraphQL is a query language for the APIs for getting your data
  • GraphQL is an alternative for the REST APIs.
  • It is not specific for a single platform and works for all types of clients including Android, iOS or web.
  • GraphQL is a language that enables you to provide a complete and understandable description of the data in your API.
  • GraphQL, a modern alternative to the REST-based architecture,
  • GraphQL is a language for querying databases from client-side applications. On the backend, GraphQL specifies to the API how to present the data to the client.
  • GraphQL is a query language and an alternative to REST. It started as an internal project at Facebook.

How Does GraphQL Work :

Image for post
Image for post

This schema is explaining how to start implementing a GraphQL application

Image for post
Image for post

Graphql Advantages

  • Fetching data with a single API call. :
Image for post
Image for post
GraphQL aggregating data from multiple places into a single API, Source: How to GraphQL
  • No over- and under-fetching problems (by allowing the client to request only the needed data, GraphQL solves both over-fetching and under-fetching issues by allowing the client to request only the needed data)
  • API evolution without versioning.
  • Detailed error messages.
  • Rapid application prototyping
  • Declarative Data Fetching
  • Combining requests (While each microservice defines its own GraphQL schema and has its own GraphQL endpoint, one GraphQL API gateway consolidates all schemas into one global schema., When migrating from a monolithic backend application to a microservice architecture, a GraphQL API can help handle communication between multiple microservices by merging them into one GraphQL)
  • Increased mobile usage created a need for more efficient data loading.
  • Implement an API that can satisfy A variety of different client needs
  • Avoid doing multiple calls : n case of REST APIs we have to maintain multiple endpoints.

What is different in GraphQL over REST?

Image for post
Image for post

Graphql Core concepts :

Schema :

Schemas are written using what’s known as the Schema Definition Language or SDL

Schemas are used to describe the shape of your data graph.

This schema defines a hierarchy of types with fields that are populated from your back-end data stores. The schema also specifies exactly which queries and mutations are available for clients to execute on your data graph.

Image for post
Image for post

Query:

Image for post
Image for post

Mutation :

Mutations are defined as a part of the schema.

Image for post
Image for post

Subscription :

Image for post
Image for post

Resolver:

Image for post
Image for post

Graphql tools and libraries :

1 Apollo Client and Server

Image for post
Image for post

2 graphiql ( An in-browser IDE for exploring GraphQL.)

Image for post
Image for post

3. Prisma

Image for post
Image for post

4. graphql-playground

Image for post
Image for post

Conclusion :

It isn’t a REST replacement, but an alternative.

Written by

<script>alert('try your best')</script>

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store