GraphQL – The Best Thing Facebook Invented

What is GraphQL?

GraphQL is a framework and specification of how to retrieve data via APIs. It was developed by Facebook and publicly released in 2015 and the project has moved to a newly-established GraphQL Foundation, hosted by the non profit Linux Foundation.

This is important to allow for the framework to be come independent of FB and become open source without any conflicts of interest.

Here’s a Quick Primer:

Technical Breakdown

  • GraphQL has both the server component and client components which are detailed below.
  • Server component:
    • API end point which includes:
      • Type definitions – shape of data i.e. Schema
      • Resolvers – function that maps to the schema i.e. how to map the incoming query to the data objects that should returned
    • The server takes type definitions + resolvers and exposes them through an end point
    • Only one end point is needed
  • Client component:
    • Allows the front end to be able to query from a GraphQL endpoint

Example Query:

{
	user (id: 3500401) {
		id,
		name,
		isViewerFriend,
		profilePicture(size: 50) {
			uri,
			width,
			height
		}
	}
}

Example Response

{
	"user" : {
		"id": 3500401,
		"name": "Dale Sun",
		"isViewerFriend": true,
		"profilePicture": {
			"uri": "http://youmust.construct/morepylons/jpg",
			"width": 50,
			"height": 50
		}
	}
}

The “So What”?

GraphQL solves for following challenges:

  • Relational queries saves the number of rounds trips instead i.e.
    • Get Ids using API endpoint 1
    • Then use Ids that are returned for endpoint 2
  • Over-fetching data
  • Required detailed documentation for each endpoint

REST vs GraphQL

This is a great video that looks at Rest vs GraphQL

Here is a summary of key takeaways from the video in table format:

Benefits

REST BenefitsGraphQL Benefits
Indefinite scalingEasy to start
High performance (esp. over HTTP2)Time to market for servers and clients
Proven and works with any representation and media typesAmazing development experience
Decoupling of client and server = independent evolutionEasier to keep consistent and govern
Links across APIs

Costs

REST CostsGraphQL Costs
Large entry barrier to training and learningNeglects challenges of distributed systems (microservice architecture)
Huge paradigm shift from SOAP = challenge for enterprise level developmentServer and Clients coupled, leading to greater risk and technical debt
Requires client side discipline to meet specsLimited media type support
Limiting API description formatsQuery optimisation
Challenge to maintain (governance and consistency)Low but increasing adoption

Use cases

Use REST ifUse GraphQL if
Building a system that lastsTalking to yourself (frontend-backend)
In need of content negotiation (language) Instead of so-called-REST
Precise authentication, authorization rate limiting is neededShort term project
Interlinking resource between APIsUse-cases are
Using mixed media typesJust accessing data without the need for infrastructure/caching
Scaleability is a factorWanting to achieve Amazing Design Experience with little effort

Other Resources