REST vs GQL
Question :- What is GQL ?
Answer → GQL is a query language for API developed by META. It provides a schema of the data in the API and gives clients the power to ask for exactly what they need.
Example #1.) Say, User just wants to inquire for title & author :-
Example #2.) Say, User just now wanted to inquire for title, author and year as well :-
Question :- Where does GQL is situated in the flow of request ?
Answer → GQL sits between the Clients and the Backend-services.
Question :- Can GQL also perform Aggregations from multiple resources ?
Answer → GQL could aggregate multiple resource requests into a single query.
Step #1.) Here is how the request can go to multiple Backend-services.
Step #2.) Here is how the aggregated-response comes through GQL layer :-
Question :- What do you mean by GQL-Mutations ?
Answer → Mutation are GQL’s way of applying data-modifications to the resources.
Question :- Explain how does GQL-Subscriptions works ?
Answer → Subscriptions are GQL’s way for clients to receive notifications on data-modifications.
Question :- How are GQL and REST-API similar ?
Answer → Here are the similarities in GQL & REST-API :-
1.) Both REST-API & GQL send Http-requests & receive Http-responses.
2.) Both REST-API & GQL make a request via a URL and both can return a JSON response, in the same shape.
Question :- Explain something about, how does REST-API works ?
Answer → Here is how the REST-API works :-
1.) REST centres around resources. Each resource is identified by a URL.
2.) For example, To fetch a book from a BookStore, API could look something like this :-
Note that, In the above response, we are also getting the authors data as well. Some REST implementations might break them into separate REST calls. For example, like the one shown below :-
3.) We basically send the request to the RESTful-API as shown below :-
Question :- Explain something about, how does GQL works ?
Answer → Here is how the GQL works :-
1.) Basically, we define the Query with GQL as shown below :-
2.) We can also send the request to the GQL-Query as shown below :-
Question :- How are GQL and REST-API different ?
With GQL, the Client specifies the exact resources that they want and also the fields that they are interested into :-
whereas In case of REST-API, API-Implementor decided for us that authors are included as related-resources :-
Question :- What is the important benefit with GQL ?
Answer → GQL doesn’t use URLs to specify the resources, that are available in the API.
Instead, with GQL, we use something called as GQL-Schema :-
Question :- What is the disadvantage of working with GQL ?
Answer → The beauty of REST is that, we don’t need special libraries to consume someone else’s API. Request can be sent using common tools like cURL OR simply a web-browser.
Whereas, GQL requires heavier tooling support, both on client & server sides, which requires a sizeable upfront investment.
Question :- When should we go with GQL ? Is it really beneficial to use GQL?
Answer → Here are the thoughts on preferred usage of GQL :-
Consideration #1.) As we mentioned above that, there is a considerable investment needed to work with GQL, this upfront cost might not be worth it, especially for very simple CRUD APIs.
Consideration #2.) Another criticism of GQL is that, it is more difficult to cache.
REST uses HTTP-GET for fetching resources and HTTP-GET has a well defined Caching behaviour, that is leveraged by Browsers, CDNs, Proxies and Web-Servers.
Whereas, GQL has a single point of entry and uses HTTP-POST by default. This prevents full use of Caching. In short, it’s possible to leverage caching with GQL, but it’s little difficult.
Consideration #3.) While GQL allows clients to query for just the data they need, this also poses a great danger.
Imagine an example, where some Mobile-Application shipped a new-feature, that causes an unexpected table-scan of a critical database of a backend service. This could potentially bring the database down, as soon as the new version of the application goes live.
Conclusion → Software Engineering is about the trade-offs and It depends upon the business and the need, whether GQL is a right fit for someone’s use-case or not ? It’s definitely worthwhile to explore GQL for high-scale production usage.
If you like to learn more about implementing the GQL on your own in JAVA, refer to the links in references. That’s all in this blog. if you liked reading it, do clap on this page. We shall see you in next document.
References :-