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.
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.
@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.
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.
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.
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.
"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.