Example: Which is Better for APIs?

Let’s take the same pizza store example and understand how it works in GraphQL Query.

1. RESTful Approach

To order pizza in REST API approach, we may have to create two separate endpoints for pizzas and toppings. If you want a pizza with specific toppings, you have to make multiple requests.

Get Pizza Menu (GET /pizzas):

GET /pizzas

Server responds with a list of predefined pizzas and their toppings.

Select Pizza (GET /pizzas/{pizzaId}):

GET /pizzas/123

Fetch details for a specific pizza, but toppings are predefined and cannot be customized.

Place Order (POST /orders):

    POST /orders
{
"pizzaId": 123,
"deliveryAddress": "123 GFG Office"
}

Place an order with a predefined pizza and toppings.

2. GraphQL Approach

With GraphQL, You have the option to order a pizza with your customization.

Write a Query:

query {
pizza(id: 123) {
size
crust
toppings {
name
}
}
}

Request the specific details you want for your pizza in a single query. A server-side query can be answered in a particular way with the use of arguments. It comes with a key:value pair and a field. It can either be a variable or a literal in the fields.

Get Custom Pizza:

{
"data": {
"pizza": {
"size": "Large",
"crust": "Thin",
"toppings": [
{"name": "Pepperoni"},
{"name": "Mushrooms"},
{"name": "Bell Peppers"}
]
}
}
}

Receive a custom-made pizza with the exact toppings you specified. Now you can order your customized pizza with your favourite toppings.

GraphQL vs REST: Which is Better for APIs?

In the world of web development, communication between a client (like a web or mobile app) and a server is crucial. Traditional REST APIs have been the go-to solution for many years, but GraphQL is emerging as a powerful alternative that offers more flexibility and efficiency. GraphQL is a query language for API and a server-side runtime engine used for data query and manipulation. It was developed by Facebook, and later made open source, and is now managed by GraphQL foundation hosted by Linux foundation. Many popular public APIs like Facebook, GitHub, Yelp, Shopify, and Google Directions API adopted GraphQL as the default way to access their services.

Similar Reads

What is GraphQL?

GraphQL is an open-source query language and server-side runtime engine used to query and manipulate data using API. GraphQL uses declarative data fetching where the client can describe exactly what data it wants rather than getting a whole lot of data from a traditional API or using multiple end-points to give the client the required data. GraphQL is compatible with Java and frameworks like Spring, NodeJS, and Express and can also be used in Django....

Key Concepts in GraphQL

1. Schema...

GraphQL Query

Query is one of the operation of GraphQL where other two are Mutations, and Subscriptions. A GraphQL query is used to fetch data, GraphQL server executes the query and gives result....

Example: Which is Better for APIs?

Let’s take the same pizza store example and understand how it works in GraphQL Query....

Differences Between GraphQL and REST API

GraphQL REST API GraphQL uses single endpoint for every operation. REST API uses multiple endpoints for different operations In GraphQL client defines what data is required. REST API fetches data using pre-defined rules. GraphQL reduces over-fetching and under-fetching. Over-fetching and under-fetching are the common issues with Rest API. GraphQL supports real-time updates with subscriptions REST API relies on polling for real-time data GraphQL is a growing technology with various tools and libraries. REST APIs are well established ecosystem with multiple libraries and tools....

Characteristics of GraphQL

1. Declarative Data...

Applications of GraphQL

Web and Mobile Applications...

Conclusion

Compared to conventional REST APIs, GraphQL offers a more effective and adaptable substitute, enabling developers to create better apps. GraphQL enhances client-server communication by enabling clients to define their data requirements, which leads to more efficient and effective applications. Understanding and utilizing GraphQL is a useful skill as you move ahead towards advance in web development since it will improve your capacity to design responsive, and effective applications....