What is GraphQL API?
Graphql is a Query language API that offers an alternative, better, optimized and client’s requirement based model to develop APIs with detailed description. It’s a stateless, client-independent API for exchanging data with higher query flexibility. As per GraphQL Official Docs, “GraphQL is a language for querying data. Unlike most query languages (such as SQL), you don’t use GraphQL to query a particular type of data store (such as a MySQL database). Instead, you use GraphQL to query data from any number of different sources.”
Why use GraphQL API over REST API? Although REST API provides a flexibility to exchange data between client and server, then why will anyone use GraphQL API? REST API does not provide flexibility to overcome situations of over-fetching and under-fetching. Over-fetching: In traditional approaches like REST, you often end up over-fetching — asking for more data than you need. With GraphQL, you can ask for exactly what you need. The benefit is reduced bandwidth, which may be especially important on mobile and low-energy devices.
Under-fetching: The other end of the fetching problem in REST is not getting all the data you need in a single round-trip. Consider fetching your friends list in a social media app and needing to batch individual queries to get each friend’s profile picture. GraphQL helps solve this problem.