Re:Many small queries are efficient in SQLite
by allknowingfrog on Tuesday February 16, 2021 @01:01PM
(#26156774)
This seems a little pedantic. It may not be literally required by the technology, but GraphQL certainly makes it easier to introduce N+1 queries than a traditional REST API.
[ Parent
| on HN ]
Re:Many small queries are efficient in SQLite
by a-priori on Tuesday February 16, 2021 @01:19PM
(#26156978)
In practice, I find REST APIs also have N+1 problems the same way GraphQL does. They just get spread across multiple requests.
Re:Many small queries are efficient in SQLite
by Groxx on Tuesday February 16, 2021 @02:51PM
(#26157975)
If anything I'd say REST is dramatically worse in this respect. There's no structure for nested queries (or anything except thing/:id really), so the only broadly compatible option is to pull every piece as a separate GET request.
Sure, you can use query params... but there's no implied support nor semantics, so one site will do one thing and another will do something from a completely different universe of architectural patterns, while a third will just have nothing, and there's no way to reconcile those in a consistent way.
Re:Many small queries are efficient in SQLite
by travellingprog on Tuesday February 16, 2021 @03:43PM
(#26158578)
You don't need to have 1:1 equivalence between API resources and database models, though. For example, you can create an API resource called TweetActivity, and then the backend code for GET /tweet-activity could put together a database query that grabs all the tweet likes, comments and basic commenter info (name, profile image), from different database tables, into a single new object. You can give that object the same ID as the tweet itself, and you can put a cache layer around that endpoint to, for example, save that response for the next 1 minute.
That being said, one thing you give up is providing a standard way for the client to specify which fields it needs returned. For example, a client might want to dig deeper into the commenter profile info. GraphQL's resolvers architecture opens up that possibility immediately.
Re:Many small queries are efficient in SQLite
by Groxx on Tuesday February 16, 2021 @04:07PM
(#26158822)
Agreed on pretty much all points, but you've just described a way to optimize an N+1 system without changing the fundamental N+1 aspect. The caller is still making N+1 requests and incurring round-trip latencies (end-to-end cost can at best be reduced to 2 with parallelism), and the server still has to receive N+1 requests. REST has no semantics to change this.