I have a GraphQL server implemented with absinthe, dataloader and phoenix.
A sample query is as follows
What I anticipate is that the query lists the first 10 books in database and for each book, the first 10 chapters of the book.
{
listBook(from:0, size:10){
books {
id
name
url
chapters(from: 0, size: 10) {
id
name
book{
id
name
book {
id
}
}
}
}
total
}
}
I am using dataloader to load chapters field.
However, the generated sql is not expected, instead of returning first 10 chapters for each book, the underlying sql actually returns first 10 chapters of all chapters of the first 10 books.
How can I achieve my goal? Make my GraphQL API return first 10 chapters of each book.
[debug] QUERY OK source="books" db=60.4ms
SELECT b0."id", b0."html", b0."info", b0."name", b0."preface", b0."text", b0."url", b0."info_html", b0."preface_html", b0."parent_id", b0."inserted_at", b0."updated_at", b0."parent_id" FROM "books" AS b0 WHERE (b0."parent_id" = ANY($1)) ORDER BY b0."parent_id" LIMIT $2 OFFSET $3 [[291289, 291066, 291009, 290794, 290647, 290409, 290196, 157109, 155130, 153693], 10, 0]
If I do not use dataloader but use a custom resolver, I can achieve my goal but that results in 1+10 queries because for each book the application needs to query database for its chapters.
Is there a way to use dataloader to batch load chapters?
The complete source code is here https://github.com/huangjimmy/wikisource