1
00:00:00,330 --> 00:00:03,450
Instructor: Our next web API is GraphQL.

2
00:00:03,450 --> 00:00:07,950
GraphQL was developed internally in 2012 by Facebook.

3
00:00:07,950 --> 00:00:11,010
And released to the public in 2015.

4
00:00:11,010 --> 00:00:12,930
The biggest advantage of GraphQL

5
00:00:12,930 --> 00:00:15,480
is that it enables a very flexible querying,

6
00:00:15,480 --> 00:00:18,630
updating and subscribing to data tech changes.

7
00:00:18,630 --> 00:00:21,960
Now, what it means is that in contrast to rest

8
00:00:21,960 --> 00:00:24,330
that allows you to query an entity

9
00:00:24,330 --> 00:00:26,610
and then get the whole entity back.

10
00:00:26,610 --> 00:00:29,610
GraphQL allows you to specify exactly

11
00:00:29,610 --> 00:00:32,040
which fields you want to get back,

12
00:00:32,040 --> 00:00:34,740
and in addition, you can specify what parameters

13
00:00:34,740 --> 00:00:36,900
to use in the query itself.

14
00:00:36,900 --> 00:00:39,510
So you get a very, very flexible querying

15
00:00:39,510 --> 00:00:41,010
and operating capabilities.

16
00:00:41,010 --> 00:00:43,650
And in addition, you have a subscribing capabilities

17
00:00:43,650 --> 00:00:46,533
meaning you can get notifications about updates

18
00:00:46,533 --> 00:00:48,870
for the entities that you have retrieved

19
00:00:48,870 --> 00:00:51,483
and merge those updates in the entities.

20
00:00:53,550 --> 00:00:57,840
GraphQL, similar to rest, is based on JSON in and out.

21
00:00:57,840 --> 00:01:00,210
So, let's say an example for it.

22
00:01:00,210 --> 00:01:03,030
Here is a typical query in GraphQL.

23
00:01:03,030 --> 00:01:06,030
As you can see, it begins with query.

24
00:01:06,030 --> 00:01:09,008
Then, it tells us what kind of entity

25
00:01:09,008 --> 00:01:10,920
we want to query for.

26
00:01:10,920 --> 00:01:12,483
In this case, posts.

27
00:01:13,320 --> 00:01:15,870
And then, we can specify which fields

28
00:01:15,870 --> 00:01:18,210
we want to retrieve for the posts.

29
00:01:18,210 --> 00:01:21,690
In this case, we want to retrieve the id, title, body

30
00:01:21,690 --> 00:01:24,690
the publishing date, and the author.

31
00:01:24,690 --> 00:01:27,630
And in the author, we need to specify which field

32
00:01:27,630 --> 00:01:29,910
of the author we want to retrieve.

33
00:01:29,910 --> 00:01:33,600
In this case, we want to retrieve the name of the author.

34
00:01:33,600 --> 00:01:36,570
So, this is how a GraphQL request looks like

35
00:01:36,570 --> 00:01:39,300
and the response looks like this.

36
00:01:39,300 --> 00:01:42,630
And you can see that we've got exactly what we asked for.

37
00:01:42,630 --> 00:01:45,030
We get only the fields that we asked for

38
00:01:45,030 --> 00:01:47,070
including the name of the author

39
00:01:47,070 --> 00:01:50,910
which is a field stored in a different table in this case.

40
00:01:50,910 --> 00:01:53,820
Now, what you need to know about GraphQL is first,

41
00:01:53,820 --> 00:01:57,480
it's very flexible as we just demonstrated

42
00:01:57,480 --> 00:02:00,390
but it requires some upfront dev effort.

43
00:02:00,390 --> 00:02:03,630
In contrast to rest, which is extremely easy to implement,

44
00:02:03,630 --> 00:02:05,550
GraphQL is not so easy

45
00:02:05,550 --> 00:02:08,130
and there is some developments that need to be made

46
00:02:08,130 --> 00:02:09,543
in order to make it useful.

47
00:02:10,470 --> 00:02:13,260
In addition, GraphQL performance wise,

48
00:02:13,260 --> 00:02:18,260
is a little bit, let's say, less optimized than rest.

49
00:02:18,600 --> 00:02:20,850
So you should look very, very closely

50
00:02:20,850 --> 00:02:23,313
on the performance metrics when using GraphQL.

51
00:02:24,510 --> 00:02:27,870
GraphQL is gaining ground all around the world.

52
00:02:27,870 --> 00:02:31,200
GitHub, for example, announced a few months ago

53
00:02:31,200 --> 00:02:33,870
that they are migrating all their APIs

54
00:02:33,870 --> 00:02:36,720
to expose GraphQL instead of rest

55
00:02:36,720 --> 00:02:39,210
because they identified that most users

56
00:02:39,210 --> 00:02:42,480
wants to retrieve only specific fields and almost no one

57
00:02:42,480 --> 00:02:44,820
wanted the whole bunch of fields

58
00:02:44,820 --> 00:02:47,577
that was exposed by the REST API of GitHub.

