2. 2
NodeXperts
Topics
- Why we use GraphQL?
- Before GraphQL.
- After GraphQL.
- Problems in REST?
- GraphQL in the logical architecture.
- Next Session Topic
3. 3
NodeXperts
Why we use GraphQL?
In our last session we have learnt that GraphQL is use to create API.
There are three operations in GraphQL to create API.
- Query
- Mutation
- Subscription.
But we have ways to create API before GraphQL. Let's look into that.
4. 4
NodeXperts
Before GraphQL?
Before GraphQL we create API’s using REST
method in which we have HTTP method like -
- GET
- POST
- PUT
- DELETE
So, we have all the operations required to query or
manipulate the data from database, then why we
need GraphQL? Let’s look on GraphQL now.
5. 5
NodeXperts
After GraphQL?
Now we know that GraphQL has three operations,
let’s look explore its operations.
- Query is same as we have GET request in HTTP.
- Mutation is same as we have POST, PUT,
DELETE request in HTTP.
- Subscription this is new introduced in GraphQL.
So, now we know that what Query and Mutation is,
but we have a new entry here, Subscription. What
is that? Let’s explore it.
6. 6
NodeXperts
The team structure of
each DAN POD is
optimised per set
hierarchy
After GraphQL
So, before GraphQL when we have REST API if there is any change in data in database the client is unaware
of that. So, how client will get the new data.
- If the page is reloaded then it will send the request to the server and in response server returns the new
data.
- Second method is implement websocket to get the new data. As client gets the real-time data in
websocket but it takes time to implement the websocket.
To get the new data on client using REST API either we need to reload the page or implement websocket.
Here comes Subscription in picture, it ensures client that it will keep client updated on any data change.
7. 7
NodeXperts
Query Complexity
- Fetching multiple resources require making
multiple calls to the server. With GraphQL
query we can do that in a single query.
As we can see in diagram, if I want user we make
on request, if I want all the posts related to user
we make another request to fetch that.
Problems in REST
8. 8
NodeXperts
Problems in REST
Over Fetching and Under Fetching
- In REST Api we end up with getting lot of extra
data or incomplete data for which we have make
extra calls to get complete data. Using graphQL
we get the exact data what we want in a single
request.
In the image we can see that we want only the
picture of user we get name and location along with
that, it is called over fetching.
9. 9
NodeXperts
Problems in REST
Validation and type Checking
- In REST Api we do not have any method for type checking or if we want that then we need to
implement typescript in that. Apart from that Typescript is also implements check until compile time.
In GraphQL we have type check available at run time. So, client knows in well advance that what is
available for the query.
Autogenerating API Documentation
- In REST we spend lot of time in documentation, we use tools like swagger to document API. In
GraphQL the API documentation is automatically done with each changes in the schema. We will see
that when we are writing the schema.
10. 10
NodeXperts
GraphQL in the logical Architecture
Before graphQL if we want data to be fetched
from multiple resources we use to process and
aggregate the data on client which increase the
complexity on client.
After graphQL now we can get the data on
graphQL and process data and send it to client.
So client receives only that data which it requires
to show to the end user.