Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HATEOAS vs GraphQL decision criteria set for microservices? [closed]

I was talking to someone recently who said they are skipping the development of HATEOAS REST endpoints completely in favor of GraphQL. So I'm curious as to what the criteria set is for deciding when to use GraphQL vs. HATEOAS or is GraphQL just a better choice in general for an API Gateway / Edge Server architecture?

like image 887
Ole Avatar asked Sep 05 '17 19:09

Ole


People also ask

When should I use REST API over GraphQL?

If you are aiming to develop an API to be used on a mobile application you should have GraphQL as first option because bandwidth usage matters. If your application requires a robust API, with caching and a monitoring system you should go with REST.

Is Hateoas a GraphQL?

A lot of the cool stuff you can do in GraphQL can be done in REST, but GraphQL has no HATEOAS.

Is gRPC faster than GraphQL?

Regarding performance, gRPC is considerably faster than GraphQL, thanks to the Protobuf and HTTP/2. The payload data is serialized into binary format, which reduces its size and makes it more efficient than text-based formats JSON or XML.

Is GraphQL faster than REST?

GraphQL performance is fast and Rest are multiple network calls take up more time. GraphQL development speed is rapid and Rest are slower.


2 Answers

The pros and cons of each are:

GraphQL

Pro:

  • provides fine control of returned data in avoiding unneeded traffic
  • eliminates needing to go back to the well over and over for attached / "follow-on" data
  • following from the above, it allows the software designer to provide excellent performance by reducing latency - each query specifies all the things it needs, and the GraphQL implementation can assemble and deliver it with only one client<->server transaction
  • possibility of slow deprecations instead of versioned APIs
  • it's a query language
  • introspection is built-in

Con:

  • does not deal with caching (although there are now libraries that will take care of this)

HATEOAS / REST

Pro:

  • caching is a well-understood matter
  • relatively mature and well-understood
  • lots of infrastructure for eg CDNs to spread the load
  • very suitable for microservices
  • file uploads are possible

Con:

  • the "back to the well" problem
  • not as rigidly specified
  • each implementation of server and client(s) must make its own decisions
  • querying is not standard

Conclusions

One interesting comparison is that people use GraphQL as a frontend for REST APIs, but no-one in their right mind would consider doing the converse. If you go for a federated / microservices design, so one GraphQL server fronts for others, they can use a common specification of the API between the frontend and the microservices; this is less certainly true if the microservices are REST.

I think that so long as you have in mind the right questions to ask, GraphQL is going to be an important part of a well-designed system. Whether to skip HATEOAS entirely is unfortunately, "it depends".

Sources

My own experience, plus Phil Sturgeon's GraphQL vs REST: Overview

like image 82
Ed. Avatar answered Sep 18 '22 19:09

Ed.


I love that Ed posted a link to my overview, but there's another article that I believe to be more relevant than that one.

The representation of state is completely different between the two.

https://blog.apisyouwonthate.com/representing-state-in-rest-and-graphql-9194b291d127

GraphQL is entirely unable to offer a series of "next steps" in a meaningful and standardized way, other than maybe shoving an array of strings containing potentially relevant mutations that you should try to hit up.

Even if you do that, it certainly cannot help you communicate with other HTTP APIs, which is a real shame.

Anyway, it's all that article! :)

like image 36
Phil Sturgeon Avatar answered Sep 17 '22 19:09

Phil Sturgeon