GraphQL | A Query Language For Your API | Rest API

GraphQL is a query language (or “QL” ) that describes how exactly a client should  request information through an application programming interface called API. It is a syntax that developers can use to ask for specific data structures from multiple resources. Once client defines the data structure in the  request as needed, exactly the same structured JSON data will return from the server. Before being publicly released in 2015, GraphQL was developed internally by Facebook in 2012.

Compatible with all popular coding languages like C#, PHP, Python, Ruby, Javascript, etc. GraphQL provides developers with a comprehensive view of structured data from an API, that provides an ability to only receive relevant data and an architecture which makes APIs easier to scale with very little overtime.
 
So rather than use a typical SQL query like:
  1. SELECT id, name, address FROM users 
You would simply describe the object and the fields you’d like in structured format from that object:{  
  1.   users {  
  2.     id  
  3.     name  
  4.     address  
  5.   }  

Environment Setup or Supported Options,
  • Web browser supported, recommended latest Chrome browser
  • Supported in Linux, Windows, and macOS
  • Latest NodeJs should be installed, LTS version is recommended.
  • Visual Studio Code with an extension of GraphQL

Three major characteristics you should learn before working with GraphQL APIS


Schema - GraphQL has a predefined type of language that is used to write schema. Its a human-readable schema syntax called as Schema Definition Language(SDL), which is supported with any language or framework that you want.
 
For example schema of User as below
  1. type User {  
  2.   id: ID!  
  3.   name: String!  
  4.   email: String!  
  5.   age: Int  
Types - It's a most important feature of GraphQL. It's a custom object that represents how exactly your API is going to look. Types have fields and return a specific type of data.
 
For example, types should look as below
  1. type User {  
  2.   id: ID!  
  3.   name: String!    
  4.   email: String!  
  5.   age: Int  
In GraphQL schema, you will deal with three main concepts
  • Queries - to get data from the server.
  • Mutations - to modify data on the server and get updated data back (create, update, delete).
  • Subscriptions - to maintain a real-time connection with the server.
Resolver - Where we write the query, this tells GraphQL how and where data can be found about the given fields. Without a resolver, the GraphQL server would not know how to handle any query.
 

Benefits of GraphQL

  • Provides desired output as a response
  • Takes less execution time with stability
  • It's having strongly-typed fields that alerts developers of error messages before running a query
  • Returns only predictable result of complex queries
  • Easy to understand and reduce complexity, gives best resolution between objects
  • Fetch data with only a single API Call

Comparison Of Popular API Techniques

 
 

Summary


In this article, we have learned basic fundamentals or the introduction of GraphQL API, with very optimal development of code makes GraphQL very fast executable with predictable output only. as per my suggestion, you should use GraphQL APIs with at least React application.

We will learn basic set-up and configuration of GraphQL API in an application.
 
Learn more about GraphQL - Official Site