News for hackers. Stuff that matters.
Re:GraphQL Is a Trap? by didip on Friday May 06, 2022 @10:22AM (#31285446)
I am with you. Every time I looked at GraphQL or asked to implement one, I had to say no.

How is this a good thing for the backend or infra engineers? It's a mega facade without a lot of toolings to help the backend.

GraphQL reminded me of common ORM criticisms. Wide API surface area with a lot of rooms for accidents. And GraphQL made it worse by being exposed as a service.

Re:GraphQL Is a Trap? by ishjoh on Friday May 06, 2022 @03:12PM (#31288707)
Infrastructure is one thing that seems to catch a lot of people off guard. So many infrastructure tools are based on monitoring HTTP codes, but even when there are errors graphql servers send 200s unless modified. It turned into quite the headache for us.
Re:GraphQL Is a Trap? by brentm on Friday May 06, 2022 @11:03AM (#31285999)
100% - I can see how it might be great for FB where they have the capacity to optimize but without that engineering capacity it seems like it would turn into a net negative.
Re:GraphQL Is a Trap? by ehutch79 on Friday May 06, 2022 @11:05AM (#31286028)
No one sees the backend. So who cares? /s

With ORMs at least, the developer is likely either thinking about limitations, or really needs the guide-rails an ORM provides.

I doubt a lot of front end engineers, many of which probably have never optimized a DB, are thinking about the consequences of their queries.

Re:GraphQL Is a Trap? by ryanbrunner on Friday May 06, 2022 @04:33PM (#31289366)
ORMs are also pretty easy to use on a case-by-case basis if needed, either by using the escape hatches the ORM provides or by bypassing it altogether. Deciding "oh GraphQL isn't good for this particular use case so I'll spin up a parallel REST API" is a much bigger decision to make.
Re:GraphQL Is a Trap? by freedomben on Friday May 06, 2022 @02:12PM (#31288198)
what you say is unpopular, but it's a lot more true than most people (especially front end people in this case) want to admit. Of course there are plenty of exceptions (people on FE who think about, care about, and know about what happens on the backend), particularly the Venn diagram of FE people reading HN, but the majority in the industry definitely do not. The bigger and/or more specialized the company, the worse that problem gets.

To be clear, this is not just a problem for FE people. extremely normal for humans to become myopic in the areas they spend the most time in. FE does it, BE does it, management does it, everyone does it. Find a standard mobile engineer doing native iOS or Android, and they're going to be even more disconnected from the effects on the backend, and they come by it honestly. If you tend to specialize more in one area, building an awareness of your own biases/perspective, and exercising intentional empathy, can make a huge difference in how easy you are to work with.

When looking at dysfunctional engineering orgs, one of the first things I do is figure out where the "power" is and figure out their backgrounds. The most extreme example might be a company founded by a FE eng for whom backend is just a necessarily evil to support their app. Or a company founded by a BE guy for whom the real value is the API, and the clients are just there to abstract it for normal people.

Taking this in and finding a healthy balance of the way things are structured can help improve a dysfunctional org a lot. FE, BE, DevOps/Infra, etc are important pieces in an overall puzzle. Without a well-functioning team behind each, the company and product suffer.

Re:GraphQL Is a Trap? by blakesley on Friday May 06, 2022 @02:47PM (#31288508)
I'm still pretty new to dev, but what's wrong with ORMs? And, importantly, what would you recommend instead?
Re:GraphQL Is a Trap? by deckard1 on Friday May 06, 2022 @03:56PM (#31289071)
SQL is a transferable skill. ORMs are not. If you already know SQL and have to use an ORM on top of that, then it's a net loss.

It's trivial to use SQL to build objects from individual rows in a database. Which is all an ORM is really good for. Once you start doing reporting or aggregates, then ORMs fall apart. I've seen developers who, because they have a library built up around their flavor of ORM, go and do reporting with that ORM. What happens is this ORM report consumes all the RAM in the system and takes minutes to run. Or crashes.

ORM code hits performance issues because so many objects have to be built (RAM usage) and the SQL queries underneath the framework are not at all efficient. You can use OOP on top of SQL and get decent performance. But you need shallow objects built on top of complex SQL. ORM goes the opposite: large hierarchies of nested objects built from many dumb SQL queries.

This also ties into GraphQL. Think careful about the hierarchies you design. A flat API call that does one thing well is often better than some deeply-nested monster query that has to fetch the entire universe before it can reach that one field you need.

Re:GraphQL Is a Trap? by eurasiantiger on Friday May 06, 2022 @04:05PM (#31289148)
GraphQL is not an ORM. An individual GraphQL server implementation can act as an ORM for a specific use case, but GraphQL can do much more than that.

You should not ever need to implement your own GraphQL server. There are plenty to choose from.

Re:GraphQL Is a Trap? by jimbokun on Friday May 06, 2022 @03:20PM (#31288778)
I am personally fond of:

1. query builder APIs, which can only generate valid queries, but you can control exactly what that query will be, 2. APIs that return basic data structures from the database, like maps or tuples.

Query languages like SQL are very powerful and easy to learn. And in my opinion, preferable to ORM approach of "what method calls do I need to make to trick the engine into executing the SQL I know would make this work?" ORMs add complexity and limitations that, in my opinion, are not worth the benefits.

Re:GraphQL Is a Trap? by ehutch79 on Friday May 06, 2022 @04:24PM (#31289283)
Personally I don't think there's anything wrong with them. They're just tools.

but like any abstraction, if you don't know what's going on behind the curtain, they can turn into foot guns quick.

Re:GraphQL Is a Trap? by ryanbrunner on Friday May 06, 2022 @04:37PM (#31289394)
People will have different opinions, but for me, there's nothing wrong with ORMs themselves, they are a significant productivity boost for 80% of the database interactions in your app. The tricky part is recognizing the 20% where ORMs are a bad idea, which ends up meaning that an ORM is best used not as a replacement for knowing SQL, but as a tool to make you more productive when you already know SQL.
Re:GraphQL Is a Trap? by anthonypasq on Friday May 06, 2022 @03:13PM (#31288713)
ORM's are fine for the majority of simple use cases. When things get complicated you end up either fighting with the ORM or just overriding it and writing the sql yourself anyway.
Re:GraphQL Is a Trap? by ehutch79 on Friday May 06, 2022 @04:28PM (#31289319)
Yes, there definitely need to be escape hatches for situations where you need to write sql.

But that should be rare. If you're commonly bailing to raw sql, I'd say there's something wrong, probably a poor fit of the orm to the problem.

Re:GraphQL Is a Trap? by ryanbrunner on Friday May 06, 2022 @04:39PM (#31289412)
I'll use raw SQL (maybe not as an entire query, but something like a computed column) pretty often, for situations where I want to query things like "give me all foos with a count of bars related to them", or "give me a list of foos with the name of the latest related baz". Most ORMs would want to hydrate the graph of related objects to do that, or at least have multiple round trips to the DB server.
Re:GraphQL Is a Trap? by ehutch79 on Saturday May 07, 2022 @03:06PM (#31297518)
That doesn't sound like a good ORM. They should be lazy until you access the relevant data.

That said.

This is where the knowing what's going on behind the scenes matters.

Re:GraphQL Is a Trap? by ryanbrunner on Saturday May 07, 2022 @08:47PM (#31300179)
Oh they would be lazy, it's just that expressing something like that efficiently (i.e. something like "SELECT foo.*, (SELECT count(1) FROM bar where foo_id = foo.id") is usually really hard to do. Most ORMs I've seen would N+1 on that with a naive approach, and even the "optimized" approach will want to fetch all bars vs. just the counts.
Re:GraphQL Is a Trap? by dustymcp on Friday May 06, 2022 @01:59PM (#31288072)
I can say for sure this is the case
Re:GraphQL Is a Trap? by jopsen on Friday May 06, 2022 @05:25PM (#31289748)
Also REST APIs work nicely with caching proxies and such..
Re:GraphQL Is a Trap? by sailfast on Friday May 06, 2022 @12:26PM (#31287029)
It’s not, really, but it IS a good thing for feature development speed if that’s what you’re into, and might help a team figure out quickly which data is critical to optimize for once you start putting more serious data loads through your APIs?