is an open-source
query and manipulation language
tool for APIs
. Graphs
are collections of interconnected objects. GraphQL
allows an API
consumer to request the required specific data from the server nothing else. Spring Boot GraphQL
provides a wrapper over GraphQL Java
In the real world, The best way to represent data is with a graph data structure
Social Networking
Sites such as Facebook
or Twitter
are examples of Connected Graphs
, Facebook Friends
are Bidirectional Graphs
and Twitter Following
are examples of Unidirectional Graphs
.
Facebook, Instagram, GitHub, Airbnb, Yelp, Pinterest, Twitter, New York Times, Coursera, and Shopify runs on GraphQL
In the 2010s, Facebook
had more than a billion active users and supported Desktop
, web
, mobile web
, IOS applications
, and Android applications
, all these applications had different data requirements but were served by the same Rest API
.
In 2012, Facebook
patented a new API
architecture and named it SuperGraph
which was later before GraphQL
. in 2018 Facebook
created a GraphQL Foundation
having top-level members of the Linux Foundation
, and Cloud Native Computing Foundation
(CNCF).
GraphQL is backend, frontend, technical stack, database, transport layer, data representation agnostic
Let's take a look at this with an example.
Look at the below JSON REST API
response, It is a list of apples with their ID, name, and taste. assume there is a front-end, client
, that is consuming this API response
, but the front-end client
only requires the name of the apple, nothing else, since the API contract
is to return all the variables, the client has to accept all the variables and process them, this is also called as Over-fetching
.
Over-fetching is getting or fetching more data from an API endpoint or Database, which is not required.
[
{
"appleId": 1,
"appleName": "Macintosh",
"taste": "sweet"
},
{
"appleId": 2,
"appleName": "Fuji",
"taste": "tangy"
},
{
"appleId": 3,
"appleName": "Gala",
"taste": "bitter"
},
{
"appleId": 4,
"appleName": "Jonagold",
"taste": "sour"
}
]
GraphQL allows an API consumer to request the required specific data from the server nothing else.
GraphQL
provides API consumers
an option to request what is required as the below API query
query FindAllApple {
findAllApple {
appleName
}
}
and the response
from the API endpoint
for the above query will be
{
"data": {
"findAllApple": [
{
"appleName": "Macintosh"
},
{
"appleName": "Fuji"
},
{
"appleName": "Gala"
},
{
"appleName": "Jonagold"
}
]
}
}