A Guided Walk Through GraphQL

by Mark Mahoney

GraphQL was developed by facebook to handle their massive amounts of traffic. The systems and technologies that almost everyone else in the world uses were no where near sufficient for their needs. So, they built GraphQL to replace a RESTful data fetching service. There are a couple of problems that facebook was trying to solve with GraphQL. When a client receives more data than it needs from an API endpoint this is called over-fetching. In a course registration system the client may ask for a list of all of the students. It may only need the names and ids of the students but the API endpoint may return all of the information about a student (student address, transcript, a headshot picture of the student, etc.). Over-fetching, not surprisingly, causes poor network performance. If your app can ask for and receive exactly the data it needs then the data can get to the app faster and there are fewer wasted resources (memory) in the app. Sometimes an API endpoint doesn't return enough information. This is called under-fetching. In a course registration system the client may ask for a particular student but then have to make additional API calls to get that student's course history. Each additional API call slows down the user experience (and may suffer from over-fetching like I mentioned before). If your app can ask for and retrieve exactly the data it needs then there will be fewer sequential API calls and the app will run faster. The most common approach to solve the over/under-fetching problem using RESTful API's is to increase the number of API endpoints to satisfy every client's needs. This can become hard to manage and requires close interaction between the front-end and back-end teams. A better situation is if there is a single, flexible endpoint where the front end developers can request exactly what they need and not have any over/under-fetching. This is what GraphQL provides. This book of playbacks walks the reader through writing GraphQL queries, creating a GraphQL server, and accessing a server from a client app. I'd like to point out some additional great GraphQL resources, "Learning GraphQL" by Eve Porcello and Alex Banks and https://www.howtographql.com/ are very helpful and I recommend them.