October 9, 2021

4 Reasons to use GraphQL as your next API

On a recent project, I was tasked with creating a new administration site to replace a piece of a monolithic legacy application. The legacy system did not have any API’s that we could reuse. We were going to have to create the API’s from scratch. Since the data would probably be needed elsewhere during the rewrite of the entire application I really wanted the API to be reusable. With a traditional REST API, when you hit an endpoint you get exactly the structure it was programmed to give you. What happens if I need more data for the other functions or less data, I would have to either make another request or create a new endpoint to solve the problem. One of the huge problems plaguing the legacy application is that the speed of the application was terrible and there are literally pages that have 150 server calls. My goal was to provide a new system that was quick and that would require less rework throughout the process of moving other services out of the legacy application.  I came across GraphQL several months ago and decided that it was not just cool but provided some simplifications to creating API’s and web development in general. GraphQL is a query language for your API originally developed by Facebook. It has many advantages over a simple REST API. One of the huge advantages to GraphQL is that you have just one API endpoint and the client posts a query or mutation to get the exact result that you need. Here is a short list of why you should use GraphQL for your next API. 

  1. With GraphQL you get only the data that you request. GraphQL is a query language, therefore with each request you specify exactly the fields that you need. No more, no less. In subsequent requests you can also add or subtract fields from the requests to get more or less data all through the same endpoint. With REST, you will either have an endpoint that returns all the Data for that Schema or you will have to create multiple endpoints to provide for when you need less data. In addition, GraphQL has filtering built into the language so you can add a where clause that allows you to get a filtered result set.  This would be accomplished with REST by changing the signature of the API endpoint. 
  2. Schema Stitching allows you to have a GraphQL gateway with multiple micro sites behind it. The great thing about this is that the micro site can be a REST site or GraphQL site. The gateway GraphQL site then allows you to query and set up relationships for the data from each site within the same query. This has many advantages that will be addressed in a later post.
  3. With GraphQL, versioning is not required. GraphQL allows the front end to specify only the fields that are needed. When you add fields to a schema, you will not break consumers of the API. You also do not have to create a mobile version and a desktop version of your API. The consumers can just specify the data that is needed for their needs.  
  4. Save time and bandwidth. As Stated above, The client is able to specify only the data that they want and they can get multiple schema’s in the same request. This prevents the client from having to make multiple round trip requests to the server. The end result is that the client is able to save time (both development and application) and reduce the bandwidth required to support the application.