Introduction
GraphQL
GraphQL is a query language that provides a new way of coding your API layer end points which is more flexible and efficient compared to REST.
With GraphQL:
client can specify exactly what data it needs and in what format it needs the data
you can specify your data model schemas with strongly typed data types using GraphQL's Schema Language
its easy to aggregate data from multiple sources with just one call compared to multiple calls that may be needed with REST
returns JSON
Comparing to REST
Requires continuous upgrading of documentation
Code Redundancy and increased complexity could easily get introduced if endpoints need to be configured to send different properties of same data
e.g * an endpoint for basic User Profile
an endpoint for detailed user profile with email and profile pic
with REST, you client would receive a response like below:
But what if client is only interested in id
and servicePlan
. With REST, your client has to filter the needed data on the client side or else server has to create another endpoint to return only those properties. With graphQL, client can specify exactly what data it needs in the query and will get response accordingly
My Scribblings
- from what I have understood so far, GraphQL is the next generation of ORM framework. It allows you to set a layer between your API and client that helps the client application to query the data with one single endpoint, with a very simple JSON like query syntax.
- it makes it easier to aggregate data from multiple sources and it also queries the dependent or the joined data entities/data
- when we define the schema of the various entities , we sort of creating type system of our data. we are creating the documentation , so it becomes like its auto documented
- parts to set up GraphQL - schema, query, mutations, resolvers
- on the client side, with frameworks like Angular or react, we can build queries very easily in the components itself thereby minimizing the need of services or libraries like redux.
GraphQL can massage the queried data in the structure client wants it.
- with graphQL-subscription, you can have subscribe your data for events thereby the consumers always have the the latest data always
References
Last updated