GraphQL for the RESTless

CS Corollary: A system design can be made much more resilient to change by using abstract data types to communicate between its layers.

Samuel Brice
4 min readOct 8, 2018

Whats is GraphQL?

GraphQL is a down-to-earth abstraction layer over your RESTless pieces.

Whereas REST is strongly tied to the HTTP protocol, GraphQL is protocol agnostic and favors defined types as the interface. It boils down to How (HTTP) vs What (Type).

In the RESTful API paradigm clients have to progressively query the server to discover what data (or type) is available:

With GraphQL the client knows all about the datums and is able to make specific requests.

The ultimate benefit is reduced (usually billable) HTTP traffic. But wait, there’s more!!

Types! Even when it was the Bears…

When you look at the circumstances surrounding the invention of GraphQL it proved to be a successful means of making clients more type safe. And by clients I mean the monopsony that is JavaScript.[1]

Compared to Facebook’s other attempts at making JavaScript type safe <cough>Flow</cough> this is definitely a favorite. Not only because it makes sense but also because it couples very nicely with other distributed paradigms like Cassandra (more on that later).

Smarter Dumb-Client, Dumber Smart-Server

Looking closely at the swap from REST to GraphQL what you’re interacting with is no longer an API Server but an API Resolver.

https://howtographql.com/basics/3-big-picture/

It’s very likely the GraphQL Server and API Server happen to be running on the same machine (or process, or code block) but that’s an implementation detail irrelevant to the client. Adding one more field to the Query Type can equate to one more resolver to the server as opposed to an ambiguous REST path.

At its simplest, GraphQL is about asking for specific fields on objects.
REST largely tells you How and Where.

Cassandra Is Highly Structured

Believe her or not Cassandra is highly structured. You just can’t do traditional joins [2]. Or to be crystal, you shouldn’t do traditional joins [3]. It’s perhaps best described as Not-SQL not NoSQL.

Regardless, with the GraphQL abstract data types it only becomes easier to do joins.

JIT Optimization

https://imgur.com/

(J)avaScript (I)s (T)oooohard!!

false == [];

I mean…JavaScript sucks.

true == ![];

It’s much easier to make tweaks in the (usually single) server to evolve a service than to make tweaks in the (usually many) clients.

Let’s say you start off with this Cassandra schema…

Not drawn to complexity.

…and at some point you realize you need to join tables for some exotic query.

Dramatization, may not have happened.

This is where Cassandra’s highly structured nature starts becoming undeniable.

Depending on your performance requirements on the server you have several options. You can either (1) join with unstable performance but low source-diff (2) create a materialized view or (3) refactor a new schema and backfill it.

The important thing is you do have options and (whatever compromise you chose) may all your disgraces be private [4].

Closer to the client it’s simple. With GraphQL your only option is adding a new field to the SDL. No need for a new REST endpoint (or entire API) to manage. One option is harder to screw up.

Scale and Experiment Joins

The focus of the above join example was being able to hide the implementation details of the data layer from the higher level components. A kicker with GraphQL resolvers (your resolvers are asynchronous right?) is that you can scale the join logic in a way that doesn’t excessively tax your Cassandra cluster or schema. Meaning you may experiment by first creating (e.g.) a serverless join microservice then later optimize your schema if that experiment proves fruitful.

The Lesson: Modularization

A system design can be made much more resilient to change by using abstract data types to communicate between its layers.
- Yaron G.

GraphQL facilitates modularization as a mechanism for improving the flexibility of your system while allowing the shortening of development time [5]. Because What is a less confusing interface than How.

PS: Apollo and Ajax and Cassandra

Somewhat disappointed I couldn’t usefully write Apollo and Ajax and Cassandra into the article so…here’s a picture:

  1. https://code.facebook.com/posts/1691455094417024
  2. https://opencredo.com/how-not-to-use-cassandra-like-an-rdbms-and-what-will-happen-if-you-do/
  3. https://www.datastax.com/2015/03/how-to-do-joins-in-apache-cassandra-and-datastax-enterprise
  4. https://blog.acolyer.org/2018/05/21/semantics-and-complexity-of-graphql/
  5. https://www.win.tue.nl/~wstomv/edu/2ip30/references/criteria_for_modularization.pdf

--

--

Samuel Brice
Samuel Brice

Written by Samuel Brice

This page intentionally left blank.

No responses yet