“From REST to GraphQL” — Wait, GraphQL? SQL? Are they the same?
1.0 🚀 What is GraphQL?
GraphQL is a powerful query language and runtime for APIs that was created by Facebook in 2012. It provides a complete and understandable description of the data in your API by giving the power to ask for exactly what they need and nothing more.
We can read, edit and update the data through GraphQL query.
GraphQL is a query language for your API and a server-side runtime for executing queries using a type system you define for your data. GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data.
2.0 🤨 GraphQL vs SQL? Same or different?
They are different. SQL (Structure Query Language) is a language to manage relational databases ( Relational databases have tables with rows, columns, and fixed schema).
SQL provides a way to interact with data in a structured way using a set of pre-defined tables, columns, and relationships.
On the other hand, GraphQL is a query language for APIs. It allows clients to request only the data they need, and nothing more, by defining the structure and types of data based on the schema.
GraphQL provides a more efficient way to retrieve data compare to traditional ( GET, POST, DELETE ) REST APIs.
3.0 🏎️ Why do we need GraphQL? How efficient is it?
For example, we have a REST API with two endpoints,
`/users` and `/address`.
A request to `/users` would return a list of all users, while a request to `/address` would return a list of all addresses.
We would need to send 2 different GET requests using traditional REST API to get the data we want as below:
GET /user?id=1
GET /address?user_id=1
However, we only need to send 1 query to get the same data using graphQL.
query {
user(id:1){
id
address
}
}
And the results :
{
"users": {
"id": 1,
"address": "Malaysia"
}
}
}
From the above example, we can clearly see that the benefit of using GraphQL is that it prevents data overfetching and underfetching issues.
As with GraphQL, we can fetch exactly the data we need in a single request without any unnecessary data or additional requests.
4.0 👨🏻💻 GraphQL Operator
Unlike traditional REST APIs operation (GET, POST, DELETE, etc), GraphQL only has 3 main operations ( Queries, Mutations, Subscriptions).
📖 Queries (Read data) :
query{
user {
id
name
}
}
✍🏻 Mutations (Write Data):
mutation{
create (name: "user"){
id
}
}
👂🏻 Subscriptions (Listen to Data):
subscription{
onCreate{
id
name
}
}
⭐️⭐⭐️️You can try out GraphQL using this Hasura site.⭐️⭐️⭐️
✅ Queries (Read Data):
We can also send the same request using Postman. (Make sure you have provided the mentioned headers in the request.)
Hence, we can write our tests in the tests tab.
✅ Mutations (Write Data)
Now, let’s modify the data of the id (74560).
Next, let’s delete the data using the id,
✅ Subscription (Listen to Data)
A Subscription is a GraphQL operation that enables you to subscribe to events on the server. You will get real-time updates from the server each time the event you subscribed to occurs.
An event can represent a record insertion, modification, or deletion. Using the todo application as an example, we can use GraphQL Subscriptions to notify us each time a new todo is added to the database.
GraphQL Subscriptions are a critical component of adding real-time or reactive features to your applications.
GraphQL clients and servers that support subscriptions allow you to build great experiences without dealing with WebSocket code!
We learned that GraphQL is very powerful in fetching the appropriate data that we need from the APIs, avoiding multiple API calls and data that are not needed (overfetching).
🚀 In this article, I only cover the main operators and basic usage of GraphQL. There are much more arguments and features that you can implement in your use cases.
👉🏻 For more details, visit the GraphQL official site.
🚀 Let’s recap:
- GraphQL is a query language for your API.
- GraphQL is NOT SQL. They are different.
- GraphQL operators : Total 3 (Query, Mutation and Subscriptions)
✅ Related Articles:
- Cloud Computing, let’s talk about it [Part 2]
- AWS! The terms you must know 🤫
- ISTQB 🧙🏻♀️The software testing Myth, do you need that?
- Why is software testing needed in the first place?
- What is Agile Methodology?
- Java 101 for software testing (Using fruits and Vehicle as examples)
- What do you need to know to test using Selenium in Java? (that I wished somebody had told me)
- “Understand what Docker is in 5 minutes: Challenged Accepted”
- Black Box or White Box Testing? (How many boxes do you need ?)
✅ ☎️👇🏻
Or connect with me on LinkedIn to discuss more!
#Softwaretesting #softwaretestere #SDLC #STLC #ISTQB #Java101 #SeleniumJava #blackboxtesting #whiteboxtesting #graphql #sql #api