News for hackers. Stuff that matters.
Re:REST vs. GraphQL – A search for evidence on which is better by emilecantin on Tuesday December 15, 2020 @12:39PM (#25432617)
As someone who's worked with both approaches, at all levels of the stack (both backend and frontend), I think the paper (at least, its abstract) is missing the point.

While I do "feel" faster when working with GraphQL, I think the main benefit is that it abstracts away the busywork of transferring data between frontend and backend.

Writing a backend becomes more about describing what data is available, on what terms, how it fits together, and the operations you can do on it (mutations). Gone is the busywork of writing routes, handling CRUD in a zillion slightly different ways, etc.

On the frontend, Apollo-client lets me just write my app, not having to handle data-fetching, loading & caching for the zillionth time. I need an additional field? Just add it to the query! No need to get everyone involved.

Of course, there are traps you can easily fall into with Apollo's caching, N+1 queries on the backend, etc. But in my experience, while you do have to "drop down a level" sometimes to fix these, you spend most of your time at the "higher level".

Re:REST vs. GraphQL – A search for evidence on which is better by zanellato19 on Tuesday December 15, 2020 @12:41PM (#25432655)
I have the opposite experience working on the backend. GraphQL is too prone to N+1s and other inefficiencies because its in direct opposition of the way data is stored. Its much harder to make an efficient resolver than it is making an efficient REST endpoint.

Errors are harder, because HTTP codes are not used and monitoring is a lot more complicated. I see value in GraphQL - but I think it brings a lot of cost to the table.

Re:REST vs. GraphQL – A search for evidence on which is better by TheCoelacanth on Tuesday December 15, 2020 @12:53PM (#25432807)
In my experience, N+1 queries are roughly as easy to run into with naive GraphQL as with naive REST. When you are optimizing the queries, it's slightly easier to get rid of the N+1 behavior in GraphQL than in REST because you can see exactly what data the client is trying to get and can fetch data differently based on that instead of needing to come up with your own way for the API to describe what data to fetch.
Re:REST vs. GraphQL – A search for evidence on which is better by SkyPuncher on Tuesday December 15, 2020 @08:07PM (#25437753)
> In my experience, N+1 queries are roughly as easy to run into with naive GraphQL as with naive REST.

We're currently dealing with this on a REST API right now. If you have highly relational data, you run N+1 risks regardless. I think people misplace blame on REST/GraphQL when they will have fundamental data issues either way.

We're exploring a few ways of solving it, but they honestly all resemble GraphQL.

Re:REST vs. GraphQL – A search for evidence on which is better by gabereiser on Tuesday December 15, 2020 @12:48PM (#25432745)
Can you give some concrete examples on the N+1 problem? I’ve heard this before but never experienced it.
Re:REST vs. GraphQL – A search for evidence on which is better by baumandm on Tuesday December 15, 2020 @01:03PM (#25432953)
Imagine you're working on a bespoke blog engine for your company, so you have a GraphQL API that lets you query for all articles or just a single article.

Now the product folks want to add comments, so you add a new comments field on your article, with a new field resolver that queries the database for comments like SELECT * FROM comments WHERE article_id = ?.

This works great when viewing a single article: the frontend makes a single query to the GraphQL API, which makes 2 database queries: one for the article details and one for the comments

But then the product folks want to update the article search page with a expandable comment section for each article, so you can preview the comments for an article. Easy enough, all you have to do is add the comment field to the GraphQL query for the article search page, it's a GraphQL success story!

But now when the article search page loads, it runs 1 GraphQL query that retrieves 20 articles, and for each one it runs the SQL query to load comments. So instead of making 2 database queries it's 21.

Now we have performance scaling along with the number of results on the page, which is not ideal.

Re:REST vs. GraphQL – A search for evidence on which is better by valenterry on Tuesday December 15, 2020 @06:49PM (#25437056)
This problem exists in REST APIs in the exact same way though, unless you specifically optimize for this way of querying. But then you can do the same thing with GraphQL, so in the end GraphQL is not worse off, actually rather better because at least the problem exists only between backend and database, not between frontend and backend and database.
Re:REST vs. GraphQL – A search for evidence on which is better by fastball on Tuesday December 15, 2020 @10:48PM (#25438829)
The idea is that there is a sort of contract between the backend and the frontend.

If I conscientiously create an API endpoint that allows you to fetch articles with their comments, I'm gonna craft that query to avoid the N+1 problem. So when someone uses it, perf isn't scaling linearly.

With GraphQL, you can have a frontend person add comments without anyone from the backend team modifying anything. They say "yay it works, GraphQL is amazing", and nobody realizes this is causing scaling problems because nobody on backend actually thought about it.

Re:REST vs. GraphQL – A search for evidence on which is better by valenterry on Tuesday December 15, 2020 @10:55PM (#25438863)
> With GraphQL, you can have a frontend person add comments without anyone from the backend team modifying anything

Well, they can do that with REST as well. They will just make a bunch of requests. That is what usually happens in the real world.

However, the difference is that with REST, it all just looks like isolated independent requests. With graphQL it becomes more clear that someone wants to query all comments in just one query, so it's much more easy to detect and optimize for that case.

For me, GraphQL wins here.

Re:REST vs. GraphQL – A search for evidence on which is better by fastball on Wednesday December 16, 2020 @12:19AM (#25439357)
I don't know, I still think you're much more likely to have a frontend dev get annoyed by having to kick off 20 requests (and seeing that perf impact in their own devtools) and ask backend to give them an endpoint that can get all the content in one go.
Re:REST vs. GraphQL – A search for evidence on which is better by valenterry on Wednesday December 16, 2020 @12:40AM (#25439464)
Maybe, but is it really a good idea to rely on the laziness of developers and hope they give you feedback? I've not seen this work out well so far.
Re:REST vs. GraphQL – A search for evidence on which is better by fastball on Wednesday December 16, 2020 @12:56AM (#25439544)
To my mind, a system architecture that does not take into account is the flawed one, not the other way around.
Re:REST vs. GraphQL – A search for evidence on which is better by valenterry on Wednesday December 16, 2020 @04:44AM (#25440679)
Taking into account is one thing, but relying on lazyness... I don't know, I'm not convinced
Re:REST vs. GraphQL – A search for evidence on which is better by fastball on Wednesday December 16, 2020 @04:05AM (#25440439)
*does not take human nature into account
Re:REST vs. GraphQL – A search for evidence on which is better by gabereiser on Tuesday December 15, 2020 @11:36PM (#25439111)
Same. This is why I was looking for concrete examples because the problem isn’t a graphql problem, it exists in any interface. Kinda a trick question, but it was good reading people’s reasoning ;)
Re:REST vs. GraphQL – A search for evidence on which is better by baumandm on Wednesday December 16, 2020 @02:03AM (#25439871)
I agree with GP as well, but I do think there are unique circumstances with GraphQL.

One difference is that if the front-end makes N+1 REST calls, it's (hopefully) obvious to the front-end developer. It's also generally easy to map the REST requests to the database queries being made.

Swap it all out for a single GraphQL query and now you have no idea how it will perform or whether it was optimized for the specific fields you are requesting.

Another difference is that REST-style solutions won't work for GraphQL. Imagine you're making a bunch of REST calls, e.g. querying for a list of articles then querying for a list of comments for each one. You can ask the backend team for a new endpoint that returns them all in one query, easy enough.

But with GraphQL schemas, the potential graph of data is too large to write custom SQL queries that efficiently fetch everything in one batch. For example:

  {
    articles {
      title
      contents
      author {
        name
        articles {
          title
          contents
          comments {
            content
            author {
              ...
            }
          }
        }
      }
      comments {
        content
        author {
          name
        }
      }
    }
  }
Maybe a bit contrived, but it illustrates my point. Due to the ability to traverse relationships it's much easier to find yourself in a situation where the implementation of the GraphQL resolvers is not ideal for the usage, but it theoretically will work.
Re:REST vs. GraphQL – A search for evidence on which is better by valenterry on Wednesday December 16, 2020 @02:27AM (#25439976)
The GraphQL equivalent to creating a new, specialized REST route would be to create a new, specialized query.

E.g. the following REST

    /articles-with-comments?number=20
which returns title, contents etc. would map to a completely new query in graphql

    articlesWithComments(number: Integer) {
      title
      contents
      ...
    }
so it is exactly as easy to optimize. Of course this is not very composable, but that is equally true for both solutions.
Re:REST vs. GraphQL – A search for evidence on which is better by gabereiser on Wednesday December 16, 2020 @12:55PM (#25445783)
and this is the pattern we see in the wild mostly.
Re:REST vs. GraphQL – A search for evidence on which is better by Aeolun on Wednesday December 16, 2020 @07:30AM (#25441772)
Apparently you could solve this using the dataloader pattern/library.

Every layer would send off one query, unless they’re dependent on each other, but it’d be a far cry from N+1

Re:REST vs. GraphQL – A search for evidence on which is better by zachrip on Tuesday December 15, 2020 @01:20PM (#25433157)
The solution for this is to use something like dataloader btw. It essentially waits for all these queries (I believe it uses queueMicrotask under the hood) and batches them. Not unlike other db batching proxies.
Re:REST vs. GraphQL – A search for evidence on which is better by valeness on Tuesday December 15, 2020 @02:43PM (#25434155)
Wouldn't it be more resource efficient to traverse the graph ahead of time and prefetch all related resources with a single query?
Re:REST vs. GraphQL – A search for evidence on which is better by zachrip on Tuesday December 15, 2020 @03:21PM (#25434707)
This is more or less what dataloader accomplishes. Generally speaking when creating a gql service, graph traversal is not app code but library code.
Re:REST vs. GraphQL – A search for evidence on which is better by zanellato19 on Tuesday December 15, 2020 @03:45PM (#25435012)
Dataloader avoids the N+1 but I wouldn't call it a good solution, you have to twist your app code to make it work. Its an inelegant solution, imo.
Re:REST vs. GraphQL – A search for evidence on which is better by Aeolun on Wednesday December 16, 2020 @07:32AM (#25441788)
Not more than you’re already twisting it using resolvers? I’ll admit it will look like magic though.
Re:REST vs. GraphQL – A search for evidence on which is better by WiseWeasel on Tuesday December 15, 2020 @09:18PM (#25438232)
If that’s a problem, have the frontend run a separate query for the article’s comments upon expansion of the section, like the REST version would have done. You still have easier caching in the GQL version.
Re:REST vs. GraphQL – A search for evidence on which is better by btilly on Tuesday December 15, 2020 @10:29PM (#25438713)
Use something like Postgraphile to create your graphql endpoint and it will issue a single query then massage the results into what it needs them to be.

I don't see this as a major problem.

Re:REST vs. GraphQL – A search for evidence on which is better by tynorf on Tuesday December 15, 2020 @12:57PM (#25432875)
I think the classic example is article list with author details. You load the articles (one query), then for each article you load its author (N queries). Naive use of an ORM causes this:

  articles = Article.load(limit=10)
  for article in articles:
    # .author implicitly runs a query
    author_name = article.author.name
This can be avoided in ORMs providing prefetching (e.g. `Article.load(eager_load='author')`).
Re:REST vs. GraphQL – A search for evidence on which is better by dudul on Tuesday December 15, 2020 @12:56PM (#25432845)
a query that lets you fetch a list of posts, each post has an author field. So if you ask for these, a naive backend will "resolve" the author for each post individually (so N queries to the DB to fetch 1 author each time) as opposed to a single resolution for the batch of authors.
Re:REST vs. GraphQL – A search for evidence on which is better by chris_st on Tuesday December 15, 2020 @12:47PM (#25432731)
This is definitely my experience, particularly in using Hasura (particularly via the wonderful NHost[0], a fantastic database service). I believe that Hasura knows how to not do N+1 queries.

[0] https://nhost.io

Re:REST vs. GraphQL – A search for evidence on which is better by 5Qn8mNbc2FNCiVV on Tuesday December 15, 2020 @06:23PM (#25436798)
Try looking at the generated sql. Afaik it's (almost) always a single sql query being ran
Re:REST vs. GraphQL – A search for evidence on which is better by yen223 on Tuesday December 15, 2020 @08:15PM (#25437814)
Hasura generates one SQL query per top-level field. Hasura takes advantage of Postgres's JSON features to compile a deeply-nested graphql query into a single query.

It's pretty impressive too. For any deeply-nested query, like fetching the 1-to-many children of 1-to-many children of some entity, a lot of ORMs will often have to resort to doing N+1 queries, which adds a surprising amount of latency to a request.

Re:REST vs. GraphQL – A search for evidence on which is better by vmware513 on Tuesday December 15, 2020 @12:48PM (#25432742)
Agree, a good library on frontend level simplify the whole development process.

Ember Data solves almost everything so you can focus on features and deliver the app in no time.

Re:REST vs. GraphQL – A search for evidence on which is better by emilecantin on Tuesday December 15, 2020 @02:27PM (#25433982)
Sounds interesting, is it tied to Ember or does it just share the name?

That's one thing that's great about Apollo; it's not too tied to any other tech / framework (except GraphQL, of course).

Re:REST vs. GraphQL – A search for evidence on which is better by vmware513 on Wednesday December 16, 2020 @03:13AM (#25440197)
Ember - Ember Data live together nicely. If you would prefer an independent modern Data library, try out Orbit.js. https://orbitjs.com/