News for hackers. Stuff that matters.
Re:The Performance Cost of Node.js and GraphQL by ttfkam on Sunday October 08, 2023 @11:11PM (#37816552)
All this pain when most of the time folks could just fire up Postgraphile or Hasura, point them at their Postgres database, and go sip mai tais by the pool. I honestly don't understand why folks insist on writing their resolvers by hand in 2023.

"But I don't want folks having direct knowledge of my database schema," I hear as a retort.

1. Most of the time for most projects, the GraphQL schema or REST API is a direct analog of the database.

2. You can always make a new Postgres schema with views to expose just the data features you want with annotations to show where the foreign key relationships should be. Still worlds easier than writing and maintaining resolver and data loader logic by hand.

Re:The Performance Cost of Node.js and GraphQL by Exuma on Sunday October 08, 2023 @11:41PM (#37816709)
Postgraphile looks awesome. Thx for pointing it out
Re:The Performance Cost of Node.js and GraphQL by brycelarkin on Sunday October 08, 2023 @11:31PM (#37816655)
Postgraphile just released their v5 update (still in beta) and it’s even more awesome than v4. No more crazy queries to address the n+1 problem.
Re:The Performance Cost of Node.js and GraphQL by ShadowBanThis01 on Sunday October 08, 2023 @11:58PM (#37816803)
I looked at GraphQL and couldn't get past the fact that it doesn't provide a way to express joins. Isn't that a problem?
Re:The Performance Cost of Node.js and GraphQL by alex_lav on Monday October 09, 2023 @12:19AM (#37816892)
A join is a SQL-ism. GraphQL allows you to express _relationships_, which the underlying database would fulfill via a join.
Re:The Performance Cost of Node.js and GraphQL by tango12 on Monday October 09, 2023 @12:37AM (#37816965)
True - but the important point worth discussing here is that most common GraphQL implementations make it really hard to think in joins.

The resolver pattern and data loader pattern make joins really painful.

eg: implement a simple top-N query (fetch 10 users and the most recent 5 articles for each) in GraphQL with a resolver tree, or even data loader.

Re:The Performance Cost of Node.js and GraphQL by alex_lav on Monday October 09, 2023 @01:23AM (#37817127)
> eg: implement a simple top-N query (fetch 10 users and the most recent 5 articles for each) in GraphQL with a resolver tree, or even data loader.

A most_recent_articles view?

Re:The Performance Cost of Node.js and GraphQL by tango12 on Monday October 09, 2023 @01:32AM (#37817174)
query { Users (first: x) { .. articles (first: y, sortBy: CREATED) { .. } } }

How would one implement the users and articles resolvers - that would be as efficient as a most_recent_views in sql?

Re:The Performance Cost of Node.js and GraphQL by pharmakom on Monday October 09, 2023 @02:44AM (#37817629)
- Resolver for the most recent articles

- Resolver for n users by ID

- Caching layer

This will be quite performant in general since in worst case we do 2 SQL queries but in most cases we hit a cache.

Re:The Performance Cost of Node.js and GraphQL by tango12 on Monday October 09, 2023 @10:01AM (#37820542)
The problem is that it’s hard to get the articles resolver make one query to fetch all the recent articles. So you’ll end up with n+1 fetches from the db.

In the articles resolver, you have to:

Select from articles order by created where article.user_id = X limit 5.

And you’ll have to run this statement n times - once for each user.

Not sure how a data-fetching cache layer at the app server layer will help here.

Re:The Performance Cost of Node.js and GraphQL by alex_lav on Monday October 09, 2023 @11:26AM (#37821417)
Sorry, I feel like there must be something I'm not understanding about the limitation you're trying to convey.

WITH relevant_users AS (

  SELECT id FROM users WHERE id IN (1)
)

SELECT users.name, mra.title

FROM users

INNER JOIN most_recent_articles mra

ON users.id = mra.user_id

INNER JOIN relevant_users

ON users.id = relevant_users.id;

This is a single query that can fetch users filtered by the First: (although I just did in, you could add whatever filtering options you wanted in the relevant_users cte) with the most recent articles view we've discussed previously.

Re:The Performance Cost of Node.js and GraphQL by tango12 on Monday October 09, 2023 @11:41AM (#37821575)
@alex_lav: Your SQL query here is exactly what I meant! In SQL, this is easy, both to write and to optimize perf.

Implementing this with GraphQL resolvers (how folks typically write GraphQL servers) is hard - there's a users function and an articles function that are both called, so it's hard to implement those 2 functions contributing together towards a query plan. This was my point.

In fact a REST endpoint that takes a list of users and runs this query with the `IN` part parameterized is easier to build here. But the GraphQL version is a pain.

Re:The Performance Cost of Node.js and GraphQL by giovannibonetti on Monday October 09, 2023 @09:43PM (#37827675)
Lateral JOIN
Re:The Performance Cost of Node.js and GraphQL by gedy on Monday October 09, 2023 @01:57AM (#37817313)
Hasura.io was mentioned above. I'd recommend you take a look at their schema extensions to do this.

However, please keep in mind graphql is an API convention, much as REST is used. REST doesn't provide a way for joins either. Both are not direct DB access.

Re:The Performance Cost of Node.js and GraphQL by ShadowBanThis01 on Monday October 09, 2023 @04:08AM (#37818147)
And thus I don't really see the point. When it first came out, I hoped GraphQL would allow me to craft my queries in one place, and reduce the tedium and error-proneness of writing client and server code to pack and unpack the data structures. But it doesn't, as far as I could tell.

Thanks for the reply.

Re:The Performance Cost of Node.js and GraphQL by JCharante on Monday October 09, 2023 @11:17AM (#37821317)
You might want to look into tRPC and how the client has the definitions for the server. It’s a really nice dev experience
Re:The Performance Cost of Node.js and GraphQL by ShadowBanThis01 on Tuesday October 10, 2023 @12:57AM (#37828636)
Thanks! I will.
Re:The Performance Cost of Node.js and GraphQL by wdb on Monday October 09, 2023 @06:39AM (#37818946)
Yeah, Hasura is really nice. Saves a lot of time