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 :

Nowadays, most of the applications are made in a way which server-side and client-side developed separately and they communicate via API, so when we think about API design, the first thing that probably comes to mind is REST Architectural style which describes how data should be transferred between the client-side and the server-side.

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
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

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

So the solution to this is graphql

What is Graphql?

GraphQL is a new API standard that provides a more efficient, powerful and flexible alternative to REST. It was developed and open-sourced by Facebook because of REST limitation and is now maintained by a large community of companies and individuals from all over the world.

Graphql Overview :

  • GraphQL is not a programming language or a graph database,
  • 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 :

  • Here’s a GIF showing how a GraphQL client can make precise queries to a GraphQL API server to fetch exactly the data it needs and in the “shape” that it wants.

This schema is explaining how to start implementing a GraphQL application

Graphql Advantages

  • GraphQL provides clients to specify the fields they want in the API request and server response will only contain those fields.
  • Fetching data with a single API call. :
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?

  • GraphQL merges all your RESTful endpoints into one and uses less network traffic.

Graphql Core concepts :

After we discovered what is Graphlq and its advantages and implementation over REST, let’s discover its core concepts :

Schema :

A GraphQL schema is at the center of any GraphQL server implementation and describes the functionality available to the clients which connect to it

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.

Query:

define what queries you can run on your GraphQL API (This allows the client to read and manipulate how the data should be received)

Mutation :

Mutations are used to insert, update, or delete data

Mutations are defined as a part of the schema.

Subscription :

Instead of sending queries over HTTP back and forth, GraphQL also has a Subscription operation type for creating WebSocket connections, so the server can push continuous updates to the client.

Resolver:

Resolvers provide the instructions for turning a GraphQL operation into data. They resolve the query to data by defining resolver functions.

Graphql tools and libraries :

1 Apollo Client and Server

industry-standard GraphQL implementation, providing the data graph layer that connects modern apps to the cloud.

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

3. Prisma

Prisma is a database abstraction layer that turns your databases into GraphQL APIs with CRUD operations and realtime capabilities. It is the glue between your database and GraphQL server.

4. graphql-playground

GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration)

Conclusion :

GraphQL simplifies client usage in order to make requests and to filter what the client actually needs to consume, avoiding extra data being passed from the server-side to the client-side. It is a good alternative to REST, but it can also be used with REST.

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

<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