Instructor: Our next web API is GraphQL.
GraphQL was developed internally in 2012 by Facebook.
And released to the public in 2015.
The biggest advantage of GraphQL
is that it enables a very flexible querying,
updating and subscribing to data tech changes.
Now, what it means is that in contrast to rest
that allows you to query an entity
and then get the whole entity back.
GraphQL allows you to specify exactly
which fields you want to get back,
and in addition, you can specify what parameters
to use in the query itself.
So you get a very, very flexible querying
and operating capabilities.
And in addition, you have a subscribing capabilities
meaning you can get notifications about updates
for the entities that you have retrieved
and merge those updates in the entities.
GraphQL, similar to rest, is based on JSON in and out.
So, let's say an example for it.
Here is a typical query in GraphQL.
As you can see, it begins with query.
Then, it tells us what kind of entity
we want to query for.
In this case, posts.
And then, we can specify which fields
we want to retrieve for the posts.
In this case, we want to retrieve the id, title, body
the publishing date, and the author.
And in the author, we need to specify which field
of the author we want to retrieve.
In this case, we want to retrieve the name of the author.
So, this is how a GraphQL request looks like
and the response looks like this.
And you can see that we've got exactly what we asked for.
We get only the fields that we asked for
including the name of the author
which is a field stored in a different table in this case.
Now, what you need to know about GraphQL is first,
it's very flexible as we just demonstrated
but it requires some upfront dev effort.
In contrast to rest, which is extremely easy to implement,
GraphQL is not so easy
and there is some developments that need to be made
in order to make it useful.
In addition, GraphQL performance wise,
is a little bit, let's say, less optimized than rest.
So you should look very, very closely
on the performance metrics when using GraphQL.
GraphQL is gaining ground all around the world.
GitHub, for example, announced a few months ago
that they are migrating all their APIs
to expose GraphQL instead of rest
because they identified that most users
wants to retrieve only specific fields and almost no one
wanted the whole bunch of fields
that was exposed by the REST API of GitHub.