Member-only story
What is GraphQl?
2 min readFeb 13, 2021
GraphQl is a query language. It executes queries by using type systems which we define for our data. GraphQL isn’t tied to any specific language or a database, just the opposite, it is adaptable to our code and our data as well.
Let’s talk a bit about how GraphQL differs from REST:
- GraphQL requires fewer roundtrips to the server and back to fetch all the required data for our view or template page. With REST, we have to visit several endpoints (
api/subjects
,api/professors
,api/students
…) to get all the data we need for our page, but that’s not the case with GraphQL. When we use GraphQL, we create only one query which calls several resolvers (functions) on the server-side and returns all the data from different resources in a single request. - With REST, as our application grows, the number of endpoints grows as well, and that requires more and more time to maintain. But, with GraphQL we have only one endpoint
api/graphql
and that is all. - By using GraphQL, we never face a problem of getting too much or too little data in our response. That’s because we are defining our queries with the fields which state what we need in return. That way, we are always getting what we have requested. So, if we send a query like this one:
query OwnersQuery {
owners {
name
account {
type
}
}
}
response is
{
"data": {
"owners": [
{
"name": "John Doe",
"accounts": [
{
"type": "Cash"
},
{
"type": "Savings"
}
]
}
]
}
}
With REST this is not the case. Sometimes we get more than we need and sometimes less, it depends on how actions on a certain endpoint are implemented.