
Introduction to GraphQL
5 July, 2023
1
1
0
Contributors
This is a beginner-level series on getting started with GraphQL. I'm writing this series on the go as I learn new concepts. And in this short blog, we'll try to cover the basic concepts of GraphQL.
Introduction
GraphQL is a query language for the APIs. It was developed by the Meta (Facebook group) as a solution for fetching data efficiently and is mainly used for reading and mutating data in APIs.
Many people have this misconception that GraphQL is similar to a database. Well, it is not a database, but rather a query language that gets data from an API just like how MySQL gets from a database.
Why GraphQL and not REST?
- As the official doc says - "Ask for what you need, get exactly that", we have the ability to explore and request only the data we need. But in REST APIs, even if we need only a specific data field, it will return everything which may not be even required.
Take a look at this Pokemon API example,
We can see that we have a ton of data, which most of the time is not required at all. And in the example, to fetch a particular data, say for example, "egg_groups", we have to request the API "https://pokeapi.co/api/v2/egg-group/10" again and that URL might contain another request to be made.
It is obvious that things are getting a little slow and this reduces the efficiency of fetching the data.
Applications where the data needs to be fetched quickly, have to rely on something faster. This is why Meta developed this GraphQL back in 2015.
With GraphQL, we can fetch the data which is necessary using only one endpoint i.e., /graphql
Fetching in detail using GraphQL will be covered in the further blogs of this series.
- No Under-fetching or Over-fetching:
We may need multiple entities at one time. But as we saw in the example, we have to request recursively to fetch data, i.e., we don't get enough data per request. This is called under-fetching.
Similarly, in some cases, we may just need some subset of data entity but the request fetches a large amount of unnecessary data. This is over-fetching.
But in GraphQL, the client has the flexibility of fetching only the required data he wants.
Before talking about the implementation, it's important to understand two important terms in GraphQL i.e., query and mutation.
Query and Mutation
In simple terms, Queries are used to fetch data from a server, while mutations are used to modify or write server-side data.
Query:
A query is used to retrieve data from the GraphQL server. It is similar to a GET request in REST. With a query, the client specifies the specific fields and relationships it wants to fetch from the server.
For example, if you want to retrieve a user's name and email from a user object, you may write a query like this:
query {
user {
name
email
}
}
The server will then respond with the requested data in the same structure as the query.
Mutation:
A mutation is used to modify or create data on the GraphQL server. It is similar to the POST, PUT, or DELETE requests in REST.
With a mutation, the client specifies the operation it wants to perform, such as creating a new user or updating an existing user, and so on. Mutations also have a defined structure and can include input arguments to pass the necessary data. For example, to create a new user, you would write a mutation like this:
mutation {
createUser(input: { name: "Joe", email: "joe@gmail.com" }) {
id
name
email
}
}
Now the server will execute this and return the result.
Conclusion
In this blog, we have covered some of the basic concepts of GraphQL.
This was just an introduction to GraphQL and in the next blog of this series, we will try to cover the types and queries related concepts.
If you have enjoyed reading this article, like and share it with others.
Till the next article, happy reading!