I'm trying to implement some kind of a basic social network project. It has Posts
, Comments
and Likes
like any other.
- A post can have many comments
- A post can have many likes
- A post can have one author
I have a /posts
route on the client application. It lists the Posts
by paginating and shows their title
, image
, authorName
, commentCount
and likesCount
.
The graphql
query is like this;
query {
posts(first: 10, after: "123456") {
totalCount
edges {
node {
id
title
imageUrl
author {
id
username
}
comments {
totalCount
}
likes {
totalCount
}
}
}
}
}
I'm using apollo-server
, TypeORM
, PostgreSQL
and dataloader
. I use dataloader
to get author
of each post. I simply batch the requested authorIds
with dataloader
, get authors
from PostgreSQL
with a where user.id in authorIds
query, map the query result to the each authorId
. You know, the most basic type of usage of dataloader
.
But when I try to query the comments
or likes
connection under each post
, I got stuck. I could use the same technique and use postId
for them if there was no pagination. But now I have to include filter parameters for the pagination. And there maybe other filter parameters for some where
condition as well.
I've found the cacheKeyFn
option of dataloader. I simply create a string key for the passed filter object to the dataloader, and it doesn't duplicate them. It just passes the unique ones to the batchFn
. But I can't create a sql query with TypeORM
to get the results for each first
, after
, orderBy
arguments separately and map the results back to the function which called the dataloader.
I've searched the spectrum.chat
source code and I think they don't allow users to query nested connections. Also tried Github GraphQL Explorer and it lets you query nested connections.
Is there any recommended way to achieve this? I understood how to pass an object
to dataloader
and batch them using cacheKeyFn
, but I can't figure out how to get the results from PostgreSQL
in one query and map the results to return from the loader.
Thanks!