GraphQL is an open-source API spec that provides an intuitive and flexible syntax which is a more efficient and powerful alternative to REST. Born out of Facebook, GraphQL is one of the great innovations of the developing world in the last few years. Note that GraphQL is just a spec, Facebook and a large community of open-source contributors maintain many of its popular client and server implementations
The ecosystem around the tech is growing horizontally by offering multiple programming languages, and vertically with libraries like Apollo and Relay.
A GraphQL operation can either be a query (read), mutation (write), or subscription (continuous read).
1. Need for efficient data loading
2. Variety of different front-end frameworks and platforms
3. Rapid feature development
- No more Over-fetching and Under-fetching
- Over-fetching – Client downloads more information than what is required in the app
- Under-fetching – The n+1 request problem. The endpoint doesn’t provide enough of the required information.
GraphQL solves both by declarative data fetching
- Rapid Product Iterations on the Front-end – Structured endpoint provides a flexible iteration.
- Declarative Data Fetching – GraphQL embraces declarative data fetching with its queries. The client selects data along with its entities with fields across relationships in one query request. GraphQL decides which fields are needed for its UI, and it almost acts as UI-driven data fetching.
Data Fetching: REST VS GRAPHQL
With REST API, multiple endpoints are needed to gather information. For example, the following are the endpoints needed to fetch user data and the transactions of that user.
In here, you’d send a single query to GRAPHQL server that includes the data requirements. The server responds with a JSON object where the requirements are fulfilled.
GraphQL is an exciting technology which provides syntax that describes how to ask for data with “smart” endpoints and is generally used to load data from a server to a client. RESTful API’s have a proven record of efficiency and performance for many years. GraphQL overshadows REST’s lags, while REST fulfills GraphQL’s voids.