GraphQL is taking over APIs
Carlos Morales • 2019-02-23
GraphQL is a query language for APIs, which is becoming very popular as we could read in the state of the JS. A GraphQL query specifies exactly what is being fetched and it will return that (no more, no less), this has profound implications.
GraphQL works as a data aggregator, it aggregates multiple queries, while typical REST APIs require loading multiple URLs, GraphQL APIs get all the data in a single request.
Because GraphQL is very flexible, you could modify the query without changing the server GraphQL API. This means adding new fields without impacting existing queries, no need for having new versions or breaking changes.
Experienced teams share their experiences
These are some interesting experiences from well-known companies:
-
Netflix wrote about ”Our learnings from adopting GraphQL”. This company is known to embrace massive microservices architectures, GraphQL allows them to integrate with those services much more straightforward and save a great deal of developer time. UPDATE Jan 2020 Netflix describes their searching system in GraphQL Search Indexing.
-
Paypal engineering team also shared their thoughts in ”GraphQL: A success story for PayPal Checkout”. They list the biggest advantages: performance, flexibility, developer productivity and evolution.
-
Twitter describes in GraphQL at Twitter some of the challenges of using GraphQL at scale. I particularly like how they liberated the GraphQL layer from doing authentication and authorization. UPDATE Sept 2020 Twitter recently wrote a post on Rebuilding Twitter’s public API. The previous API version was back from 2006, this time is based on GraphQL.
-
New York Times focus on future compatibility in their article React, Relay and GraphQL: Under the Hood of The Times Website Redesign, as they consider that future updates to their UI design will have less impact.
And other companies like IBM, Walmart Labs, Shopify, and many more.
Advantages and disadvantages of GraphQL
Advantages
The two most mentioned advantages are performance and the simplification of APIs:
- Improved performance: the web application gets all the data that specifically requires in one single round trip, this reduces the number of requests and the payload of them.
- Velocity of development: GraphQL simplifies the queries to the server, this translates in productivity improvements for the frontend developers and improved developer experience. Less breaking changes directly impacts on productivity improvements.
The advantages of GraphQL are better user experience (UX) and better developer experience (DX)
Disadvantages
As usual in engineering there is no one-size-fits-all solution, so I specially liked the trade-off analysis from Netflix. They mention three disadvantages, although the last one is easily solvable:
- The graphQL resolvers were making many duplicate network requests. That makes sense, for each query to the graphQL it needs to forward it to other services, the unexpected result is most of those forwards were duplicated data. They resolved it introducing caching systems. They do not mention it, but invalidating caching can be very trick.
- Debugging the system became much more difficult. The more pieces you add to an architecture, the more complex the system becomes.
- Monitoring is more challenging, in HTTP REST services the errors are easy to detect in the HTTP error codes. However GraphQL is capable of partial successes and almost all requests succeed with a 200 HTTP code. A possible solution is to track the exceptions per GraphQL query.
Surprisingly, these teams do not mentioned the delay graphQL must add to the system, either it is unnoticeable (<100ms) or the performance gains they mention improve the overall result.
In a nutshell, with graphQL we get better UX and DX with a more complex system. :clap::clap::clap: