News for hackers. Stuff that matters.
Re:GraphQL Is a Trap? by arinlen on Friday May 06, 2022 @10:21AM (#31285441)
> GraphQL is a great experience when you consume it and the service fulfills your query needs. Because you just ask stuff and you get them. It's really cool.

How about caching? It feels like GraphQL tries to win some (arguable) flexibility in putting together clients and in the process throws out most of the operational advantages of resource-based APIs with significant disadvantages in both how to put together a backend.

Re:GraphQL Is a Trap? by lexx on Friday May 06, 2022 @12:10PM (#31286835)
One solution is "Persistent Queries". Other solution is to throw a Varnish Cache and cache the hell out of POST requests :P

Is it elegant? No.

Do you have another service to manage? Yes

Will you pay someone else to do it for you? Possibly

I think there are already products trying to do that. But how many layers of abstractions and dependencies are you willing to have in your everyday processes?

Re:GraphQL Is a Trap? by tracyhenry on Friday May 06, 2022 @12:31PM (#31287095)
Checkout Relay.js: https://relay.dev/

It does a lot of client-side caching for you. The documentation is atrocious though IMO. I'm not sure if there exists a similar framework for backend caching.

Re:GraphQL Is a Trap? by arinlen on Friday May 06, 2022 @01:44PM (#31287916)
> Checkout Relay.js: https://relay.dev/

Relay is a GraphQL client. That's the irrelevant side of caching, because that can be trivially implemented by an intern, specially given GraphQL's official copout of caching based on primary keys [1], and doesn't have any meaningful impact on the client's resources.

The relevant side of caching is server-side caching: the bits of your system that allow it to fulfill results while skipping the expensive bits, like having to hit the database. This is what really matters both in terms of operational costs and performance, and this is what GraphQL fails to deliver.

[1] https://graphql.org/learn/caching/

Re:GraphQL Is a Trap? by tracyhenry on Friday May 06, 2022 @02:00PM (#31288083)
https://relay.dev/docs/principles-and-architecture/thinking-...

Take a look at this. Either you didn't know what's challenging about caching nested graph data, or we have different definitions of triviality/interns.

Re:GraphQL Is a Trap? by arinlen on Friday May 06, 2022 @02:06PM (#31288136)
> Take a look at this.

I repeat: client-side caching is not a problem, even with GraphQL.

The technical problems regarding GraphQL's blockers to caching lies in server-side caching.

For server-side caching, the only answer that GraphQL offers is to use primary keys, hand-wave a lot, and hope that your GraphQL implementation did some sort of optimization to handle that corner case by caching results.

Don't take my word for it. It's really that bad.

https://graphql.org/learn/caching/

Re:GraphQL Is a Trap? by RedShift1 on Friday May 06, 2022 @10:56AM (#31285909)
You can execute GraphQL queries via GET and set a cache up for it like REST. Technically it's also allowed to cache POST requests but I guess anyone who comes across that is going to raise their eyebrows.
Re:GraphQL Is a Trap? by arinlen on Friday May 06, 2022 @11:36AM (#31286406)
> You can execute GraphQL queries via GET and set a cache up for it like REST.

Does it, though? It seems it really doesn't, nor was GraphQL designed with HTTP caching in mind.

The only references to caching in GraphQL are vague hand-waiving arguments about how theoretically GraphQL implementations might be implemented with some sort of support for caching primary keys.

But any type of HTTP caching is automatically excluded from GrahQL.

To put it differently, is there any third-party caching solution for GraphQL? As far as I could gather, the answer is no.

Re:GraphQL Is a Trap? by RedShift1 on Friday May 06, 2022 @11:50AM (#31286595)
It looks like you are fixated on caching in GraphQL, but that's unnecessary, you can just cache GraphQL like REST, because in the end they are just GET requests. Just cache the GET request.
Re:GraphQL Is a Trap? by arinlen on Friday May 06, 2022 @11:54AM (#31286641)
> It looks like you are fixated on caching in GraphQL, but that's unnecessary

Oh so one of the most basic feature of any API, one which has a direct impact on scalability and motivates entire product lines and businesses like CDNs, is now "unnecessary"?

> you can just cache GraphQL like REST,

Go ahead and show one example, please.

Re:GraphQL Is a Trap? by jensneuse on Friday May 06, 2022 @02:53PM (#31288551)
Here's how we do it: https://wundergraph.com/docs/overview/features/caching We "persist" all operations using their name (unique) as part of the path. Variables are passed via query parameters. We also add a unique hash as query param which is generated by the configuration. This way, each "iteration" of the application invalidates the cache automatically. Additionally, we generate ETags for each request to reduce response payloads to zero when nothing changed. (https://wundergraph.com/docs/overview/features/automatic_con...) Combined with the generated type-safe client, this is a very solid setup.
Re:GraphQL Is a Trap? by RedShift1 on Friday May 06, 2022 @12:59PM (#31287405)
It's just a GET request. Same as REST. I don't know what you want me to show as an example. You can use Apache, nginx, squid, any proxying webserver worth its salt...
Re:GraphQL Is a Trap? by arinlen on Friday May 06, 2022 @01:48PM (#31287946)
> It's just a GET request. Same as REST.

I'm not sure you understand the problem at all.

Are you actually able to show an example or not? Because changing the HTTP verb doesn't magically change the problem, and passing query parameters as a request document renders these queries uncacheable.

> You can use Apache, nginx, squid, any proxying webserver worth its salt...

Great, pick the one you're familiar with, and just show the code. Well, unless you're not "worth its salt" or are completely oblivious to the problem domain.

Re:GraphQL Is a Trap? by RedShift1 on Friday May 06, 2022 @04:58PM (#31289547)
Here's the code: https://github.com/RedShift1/graphql-cached-get

Note that I made a very primitive implementation. Depending on which GraphQL node is queried, the request will be cached by the proxy or not. Apollo GraphQL server has much more fine grained methods of allowing caching (see https://www.apollographql.com/docs/apollo-server/performance...) however I left the example code crude so you see exactly what's going on under the hood.

Re:GraphQL Is a Trap? by Xenoamorphous on Friday May 06, 2022 @02:03PM (#31288106)
A GET request is a GET request and anything that can cache one doesn’t know or care whether it’s for REST, GraphQL, a binary file or anything else.
Re:GraphQL Is a Trap? by RedShift1 on Friday May 06, 2022 @01:58PM (#31288049)
Check back in a couple of hours, I'm on my mobile right now
Re:GraphQL Is a Trap? by spion on Friday May 06, 2022 @01:20PM (#31287654)
Re:GraphQL Is a Trap? by arinlen on Friday May 06, 2022 @01:52PM (#31287990)
> see https://www.apollographql.com/docs/apollo-server/performance...

Sorry, that doesn't cut it at all. Far from it. Being able to cache a response is not the same thing as optimizing queries that hit your database. Being able to cache a response means not having to hit your database to begin with, and save up on things like traffic going into your database, and round trip time.

With REST APIs I can get a web service to return responses that are HTTP cacheable, put a nginx instance between the ingress controller and the web service, and serve responses to REST clients without even touching the web service that provides the REST API. I can even deploy nginx instances in separate regions.

What's GraphQL's response to this basic usecase?

Re:GraphQL Is a Trap? by spion on Friday May 06, 2022 @02:50PM (#31288530)
Did you take the time to read the article? I'll try a more specific link to a section https://www.apollographql.com/docs/apollo-server/performance...
Re:GraphQL Is a Trap? by pycal on Friday May 06, 2022 @12:47PM (#31287287)
i share your sentiment; in neo4j, a popular graph store, you can’t even get primary keys without a third-party plug-in (apoc)