Skip to content

Commit f0a6bbd

Browse files
committed
graphQL vs REST
1 parent 7edf010 commit f0a6bbd

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
GraphQL is a query language created by Facebook in 2012 which provides a common interface between the client and the server for data fetching and manipulations.
2+
3+
The client asks for various data from the GraphQL server via queries. The response format is described in the query and defined by the client instead of the server: they are called client‐specified queries.
4+
5+
The structure of the data is not hardcoded as in traditional REST APIs - this makes retrieving data from the server more efficient for the client.
6+
7+
#### Drawbacks of REST
8+
The core concept of REST is that everything is a resource. While REST was a great solution when it was first proposed, there are some pretty significant issues that the architecture suffers from right now. Let’s explore some of the most significant limitations of REST today:
9+
10+
#### 1- Multiple Round Trips To Fetch Related Resources
11+
Today’s web and mobile applications are often data-driven and require large sets of data combining related resources. Accessing those data by using a REST-based API often requires us to do multiple round-trips to collect everything what is needed. E.g. imagine you’d like to request information from a post entity. At the same time you’d like to request information of post author (which is a different entity). Typically this is done by sending two request to the REST API (e.g. by using HTTP GET). First to retrieve the post object and second to retrieve the user object.
12+
13+
Endpoints for posts and users:
14+
15+
mydomain.com/posts/:id
16+
17+
mydomain.com/users/:id
18+
19+
#### Over Fetching / Under Fetching
20+
21+
Another common problem which occurs when using RESTful services is the problem of over / under fetching. So what does that exactly mean? Let’s get back to the previous example. By using endpoint mydomain.com/posts/:id we’re fetching data for a specific post. Each post might comprise the following properties: id, title, user, and body. You’ll always get back the complete set of data. There is no way to limit the response to only contain a subset of data like title and user.
22+
23+
24+
#### Explain the main difference between REST and GraphQL
25+
26+
The main and most important difference between REST and GraphQL is that GraphQL is not dealing with dedicated resources, instead everything is regarded as a graph and therefore is connected and can be queried to app exact needs.
27+
28+
This means that you can tailor the request (query) to your exact needs by using the GraphQL query language and describing what you would like to get as an answer. You can combine different entities in one query and you are able to specific which attributes should be included in the response on every level, e.g.:
29+
30+
```
31+
{
32+
post(id: 1) {
33+
title
34+
user {
35+
name
36+
email
37+
courses {
38+
title
39+
}
40+
}
41+
}
42+
}
43+
```

0 commit comments

Comments
 (0)