When talking about API, most of us think of Rest by default but since GraphQL was officially introduced in 2015, it’s been shaking up the API world. And now, as a developer we have more choices to make. GraphQL is not there to replace Rest, each has their own place. Let’s see how different they are so that we can decide which to use in our next project.
1. GraphQL
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.
Source: https://graphql.org/
GraphQl allows us to request exactly what we need instead of retrieving the whole schema and make API development easier through time.
GraphQL has 3 unique traces:
- Allow getting exact information from database
- Get many resources in a single request
- Use a type system to clearly define the available data for each type and field in a GraphQL schema
- Add new fields and types to your GraphQL API without impacting existing queries
2. Representational State Transfer (REST)
REST (Representational State Transfer) is a software architectural style that was created to guide the design and development of the architecture for the World Wide Web.
Source: Wikipedia
REST uses database standards like XML and JSON to transfer data between clients and servers. REST also has design principles to guarantee flexibility and easy maintenance, such as using standard HTTP protocols: GET, POST, PUT, DELETE to retrieve data.
3. What are the differences
a) Data fetching
- In REST, we use multiple endpoints. Each endpoint returns a specific data. For example, if we need to get a person’s info, we use 1 endpoint, and to get all posts from that user, we use another endpoint.
- In GraphQL, we may use 1 query to request specific data that we need. Server will then return a JSON object that matches our request. With this specification, GraphQL solves the problem of over-fetching and under-fetching in REST.
b) Data fetching process
- REST uses Imperative data fetching. When get data from a REST API, we need to do these
- Send HTTP request
- Receive and parse response data from server
- Format data the way we want
- Display on UI
- GraphQL use declarative data fetching, all we need to do are:
- Describe data format
- Diplay on UI
c) Adaptability to changes
In case of changes on client requirements, for example a different information, using REST we’ll need to create another api to query that new piece of information. This would probably reduce productivity, product update speed. Whilst with GraphQL, there is no need for the backend to make any changes, since it’s already querying whatever client requests.
d) Response status
GraphQL always returns HTTP status code 200, no matter whether the query is successful or not. Even when the query fails, the JSON response returns with errors, error message and stack trace, it’s not as easy as in REST when it comes to handling errors.
e) Caching
- REST API uses multiple endpoints so it makes use of HTTP caching in order not to reload the resources.
- With GraphQL, we will need to use a 3rd party library on the client to handle caching.
In the REST vs. GraphQL showdown, it’s like choosing between a classic novel and an interactive choose-your-own-adventure. REST offers familiarity and reliability—the well-thumbed pages you can always count on. GraphQL, on the other hand, invites you to write your own narrative, exploring twists and turns as you go.
カテゴリー: