5 ms·
> GraphQL knows all of the data requirements for a UI component up front, enabling new types of server functionality. For example, batching and caching underlyi
by dmitriid 9y ago
> GraphQL knows all of the data requirements for a UI component up front, enabling new types of server functionality. For example, batching and caching underlying API calls within a single query becomes easy with GraphQL.
And immediately after that the article spends two pages of text explaining how insanely complex the "becomes easy with GraphQL" really is, and offers no actual details on the "easy" part.
Oh, your client has to be cache aware. Oh, and there has to be a gateway that's cache aware, and schema aware, and has to cache all responses... And invalidate them... But there are no tools yet. And then the graphql server should be cache aware. Oh, and your database layer should also cache all responses.
Caching is a hard problem, and there's nothing in GraphQL to make it easier. Heck, exclusively relying on POST requests they deliberately remove the most obvious and the easiest part of the equation.
I love how the every next part, starts ,with a sentence that shows how complex caching with GraphQL is (emphasis mine):
> With GraphQL, frontend developers have the capability to work with data in a much more fine-grained way than with endpoint-based systems. They can ask for exactly what they need, and skip fields they aren’t going to use.
I would love to see an explanation how GraphQL makes caching for this easy.
> Schema stitching is a simple concept: GraphQL makes it easy to combine multiple APIs into one, so you can implement different parts of your schema as independent services. These services can be deployed separately, written in different languages, or maybe even owned by different organizations.
It's called REST APIs and we've known how to do them since 2001.
- Cthulhu_ 9y ago> It's called REST APIs and we've known how to do them since 2001. The aim of GraphQL insofar as I understand it is that instead of having to develop two things (a front-end and a back-end REST API that talks to your database or other services), you need just one (a front-end that talks to GraphQL). Mind you, graphQL becomes the back-end-for-front-end then, and will still need a lot of work. The main point is that with GraphQL you'll only need one for all of your apps, while traditionally you'd write one REST API for each client.
- dmitriid 9y ago> you'd write one REST API for each client. Erm... What? Granted, web requirements are a bit different from mobile requirements, but still. Why would you write one REST API for each client?
- jacobr 9y agoBecause requirements are different. Our admin GUI needs much more data and different access controls than our web and mobile apps. Third party consumers have even more different requirements. We never need to bikeshed with developers from all other teams to make sure things are consistent or whatever. We generate endpoints from a Swagger spec. With good abstractions it is not much work. Frontend devs do modifications to these endpoints as well since they are so trivial.
- dmitriid 9y ago> Because requirements are different. Our admin GUI needs much more data and different access controls than our web and mobile apps. Indeed. And how does GraphQL solve the need for different data and different access controls to that data? :)
- jacobr 9y agoNot sure, I haven't seen a good story for it. We'll stick with REST.
- brycesteinhoff 9y agoI don't think APIs in general should have anything to do with authorization, other than passing along a token.
- kazagistar 9y ago> you'll only need one for all of your apps, So where are my clients for statically typed languages? I looked into GraphQL and promptly dropped it cause I couldn't seem to call into it from Java in any sensible way. If all your "apps" are javascript, maybe this is true, but that hardly makes it a universal technology.
- magic_quotes 9y ago> REST APIs and we've known how to do them since 2001. Meh. With the exception of usual html+browser example I've yet to see at least one HATEOAS api.
- dmitriid 9y agoEven non-hateoas services can be quite good ;)
- djmashko2 9y ago(Author of the post here) Thanks for the notes! > And immediately after that the article spends two pages of text explaining how insanely complex the "becomes easy with GraphQL" really is, and offers no actual details on the "easy" part. Hi, thanks for this piece of feedback! I could have done a better job here, since I was summarizing a 38 min talk into a few paragraphs. These parts are actually talking about different concepts: The first is about ensuring that a single fetch from the UI knows about all the data it needs, so you can easily avoid multiple roundtrips to the database. This is something you can do with a basic in-process caching tool called DataLoader: https://github.com/facebook/dataloader https://github.com/facebook/dataloader The second is about caching _across_ requests, something that people usually have special infrastructure for in REST, such as Varnish. This talk was elaborating on how a GraphQL-specific piece of caching infrastructure might work, and sit in exactly the same place as REST caching. > Oh, your client has to be cache aware. I think the intention was to say that it _could_ be cache aware. It doesn't need to be, but you end up with a really nice situation. > I would love to see an explanation how GraphQL makes caching for this easy. My intention here was to say that GraphQL's ability to understand what fields are being asked for makes it easy to return specific cache controls, rather than having to put one on the whole query. So your server basically generates the control for you (this is something that exists today) > It's called REST APIs and we've known how to do them since 2001. I don't think REST has a way to automatically combine multiple REST services, and then query them all in one HTTP request without multiple roundtrips to the client, which was the goal here. Happy to talk more, and thank you for bringing up some of the places where I can communicate more clearly in the future! The audience of the talk was a GraphQL conference, but I should definitely consider next time that people who aren't already bought into the idea of GraphQL will be taking a look as well.
- dmitriid 9y ago> a single fetch from the UI knows about all the data it needs, so you can easily avoid multiple roundtrips to the database. This needs to be backed up by data. In my opinion there's no difference between GET /users/1 GET /users/1/friends and { user(id: 1) { name age friends { name } } } Unless the backend is extremely smart and can generate (optimized!) SQL queries on the fly from GraphQL schemas, this will be two trips to the database. Granted, there's LINQ and some ORMs where you just chain requests. And still... And the above can also be solved by providing an `Accept: application/vnd.my-company.users-full+json` in REST. > My intention here was to say that GraphQL's ability to understand what fields are being asked for makes it easy to return specific cache controls, rather than having to put one on the whole query. How? Skimmed through the video. Oh. Right. By providing custom `maxAge` fields to the returned data so that a custom-built gateway (that has to be schema-aware) could build a cache of that data. An exact same thing can be done in REST: add custom fields, develop a custom resolver/caching layer, voila. There is a reason no one is doing that :)