GraphQl and its advantages – Compare GraphQl and REST API

Tram Ho

What is GraphQL?

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understand description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

QL stands for query language which means query language. It can be understood like an SQL query, but not on the Table, but on the APIs (query language for APIs – you can understand more through the example in ⬇️). It is not a technology to replace REST API but more like a library to help overcome REST problems.

1. History of GraphQL

  • REST API was born in 2000, is a common standard of software development, web programming. Combined with HTTP methods GET, POST, PUT, DELETE.
  • GraphQL was born in 2015 developed by Facebook and open-sourced in 2017 and is growing rapidly. GraphQL was born not to replace REST, it is an add-on library for REST. You can use a combination of GraphQL and REST.

2. Advantages of GraphQL

  • Being able to get exactly the data that the Client needs through the query helps to improve, improve the query speed, avoid unnecessary data redundancy: For example, in a normal REST API, it will return possible results. surplus or lack. For example, singer Mono wants to get more song title Waiting for you , usually we have to edit the API to get more information about the song.

  • For GraphQL we just need to add the information we need to get as follows:
  • This saves and reuses our APIs which saves the development process of building the Backend.

3. Example

Nodejs environment, JavaScript.

  • Setting

  • Create the file index.js as follows

  • Run :

Query postman or on http://localhost:4000/graphql (built-in tool, directly queryable here).

4. Conclusion

Share the news now

Source : Viblo