Pre-requisite: What is the REST approach for writing and handling APIs and web services.
If you want to listen to this article as a podcast and immerse yourself in a great audio experience:
What is GraphQL
GraphQL is an API standard that provides a more efficient, powerful and flexible alternative to the REST approach. It was developed and open-sourced by Facebook and is now maintained by a large community of companies and individuals from all over the world.
An API (Application Programming Interface) defines how a client can load data from a server. It forms a bridge between the client and the server over which data is exchanged between the two.
At its core, GraphQL enables declarative data fetching where a client can specify exactly what data it needs from an API. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for.
Why GraphQL is a better alternative than REST
- Increased mobile usage creates need for efficient data loading
With more and more products adopting mobile first approach and more and more customers using mobile platforms, it has been more than ever important to have a very efficient data loading. Increased mobile usage, low-powered devices and sloppy networks were the initial reasons why Facebook developed GraphQL. With GraphQL, we get less data transferred over the wire/network and thus it majorly improves applications operating under these conditions.
2. Variety of different frontend frameworks and platforms
Nowadays, there are multiple client platforms and interfaces of a particular product and we want to use the same API endpoint to fetch data for all of them.
Suppose there is an API endpoint
/users/:id which fetches the entire user info. Now, on web application, you want all the user info and on mobile application, you just want the firstName and lastName in a scenario. Now there you have two choices:
- create two different APIs for both
- use the same API and get more unnecessary data transferred over the network
With GraphQL, each client can access precisely the data it needs with the same endpoint.
3. Fast development & expectation for rapid feature development
Client requirement can be varied and keeps on changing. With REST APIs, the way data is exposed by the server often needs to be modified in order to accomodate specific requirements and design changes for the client. This hinders fast development practices and product iterations.
4. Declarative Data Fetching
GraphQL embraces declarative data fetching with its queries. The client selects data along with its entities in one query request. GraphQL decides which fields are needed for its UI, and it almost acts as UI-driven data fetching.
e.g - A search page at Airbnb usually has a search result for homes, experiences, and other domain-specific things. To retrieve all data in one request, a GraphQL query that selects only the part of the data for the UI makes perfect sense. It offers a great separation of concerns: a client knows about the data requirements; the server knows about the data structure and how to resolve the data from a data source (e.g. database, microservice, third-party API).
Who uses GraphQL
Today, GraphQL is used in production by lots of different companies such as Microsoft, GitHub, Twitter, Yelp and Shopify - to name only a few.