GraphQL - Why Graph QL ?

We often talk about how it differs from REST. With the API's, or application programming interfaces before we encountered REST, or representational state transfer. Now, there are many different flavors of REST, but typically we'll request or update resources on different URI's. 

RESTful APIs follow clear and well-structured resource-oriented approach. However, when the data gets more complex, the routes get longer. Sometimes it is not possible to fetch data with a single request. This is where GraphQL helps. GraphQL structures data in the form of a graph with its powerful query syntax for traversing, retrieving, and modifying data.

Let's say we have some data about Ohio. To fetch all of the data about Ohio, I could find it at the /api/ohio route. Then we might make another get request for some data at the Cleveland route. This all starts out very innocently, but REST endpoints have a way of quickly starting to multiply as our data-fetching needs expand. 

If we're looking for people in Ohio with hats, we might create a custom endpoint for this data. Requesting it would require several different requests, one for Ohio, another for people, then we have to find all of the people with hats in Ohio. The chaos starts to pile on pretty quickly, and due to this, the response time can be slower than we'd like, or slower than is acceptable on mobile networks. An alternative to this is GraphQL. 

With GraphQL, we define a query. A query looks like a JSON object without the values. Then we send this query as a string to a GraphQL server. We notice that the response looks a whole lot like the query. With our query, we specify exactly the data that we need, and we get nothing more than that.
 As we change the data that we want to fetch, we can update the query and fill in the fields. The query is nested and issued at once, so here if we're looking for a state with a name, population, and people, all of those are going to be nested inside of one another. Then, if we need to access the value of fields from one of the objects inside of the people array, we're going to request that once also. This is particularly useful when loading UI components. We'll populate the components with data, and we'll only ask for it in one request. 

Benefits of using GraphQL :

 It defines the shape of the desired data it calls for at once. This way we avoid multiple REST calls, and the performance problems of over and under-fetching. 

GraphQL is also backwards compatible and version-free, meaning you can add new fields to an existing GraphQL server without breaking the current clients and old fields can be deprecated and can still continue to function. 

We can also use GraphQL to wrap around our existing API. So you don't have to set up everything from scratch, you can use it as part of your existing setup. 

Also, GraphQL is language agnostic, so you can implement GraphQL solutions in a range of different languages. In the next video, we'll take a look at one of the most high-profile public GraphQL API's from GitHub.