Greetings!
GraphQL is a query language for APIs to query data from the server. We are querying data from the server like we do with the database. Cool isn't it? Instead of getting what the server provides, we query what we want and it gives exactly what is asked! nothing more, nothing less.
"A query language for your API"
A simple query in GraphQL would be like this.
https://www.redhat.com/en/topics/api/what-is-graphql
GraphQL is a query language for APIs to query data from the server. We are querying data from the server like we do with the database. Cool isn't it? Instead of getting what the server provides, we query what we want and it gives exactly what is asked! nothing more, nothing less.
"A query language for your API"
Advantages
- Easy to organize
- Allows API to evolve without breaking existing queries
- A single call to fetch data
- Query exactly what is wanted
- Better performance due to reduced network calls, and fewer data
Common GraphQL terms
- Schema - GraphQL schema describes all the possible data that clients can query. It is made up of object types that define which kind of object you can request and what fields it has.
- Resolver - Each query is attached to a function called resolver that is used to produce data.
- Query - GraphQL query is used to fetch values from the server. This is like the GET in REST.
- Mutation - GraphQL mutation query is used to modify the data. This is like POST, PUT, and DELETE in REST.
Little example
Let's have a quick look at an example as per the documentation.A simple query in GraphQL would be like this.
{
me {
name
}
}
That will
return,{
"me": {
"name": "Manjula Jayawardana"
}
}
Let's say we want to fetch a movie and
director detail in a Movie application. We will have to use two APIs if we
develop it in REST.GET /movies/{id}
GET /directors/{id}
But, in GraphQL we can do this
in a single query.{
movie(id: "123") {
title
director {
name
}
}
}
{
"data": {
"movie": {
"id": "123",
"title": "Aloko Udapadi",
"director": {
"name": "Chathra Weeraman"
}
}
}
}
It looks so simple. However,
when someone (like me) comes from a REST background, we have a lot to learn.
So let's learn ☺References
https://graphql.org/https://www.redhat.com/en/topics/api/what-is-graphql
Comments
Post a Comment