DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching.
Handling different parameters in field resolvers with dataloader is tricky.
Let’s have this GraphQL schema:
type Order {
orderId: Int
status: String
}
type User {
userId: Int
name: String
orders(status: String): [Order!]
}
type Query {
GetUsers(search: String): [User!]
}
schema {
query: Query
}
Then we have query:
query {
GetUsers(search: "john") {
userId
name
ordersNew: orders(status: "New") {
orderId
status
}
ordersCompleted: orders(status: "Completed") {
orderId
status
}
}
}
In this batch loader function we need to get orders from a database. If we have all orders stored in one table we can have single query, like:
SELECT * from Orders WHERE userId IN (:userIds) and status IN (:statuses)
Then loop through params and return the result. No real issues, so far.
Now let’s think orders in different statuses can’t be queried with a single query. For example, “New” orders are in “Online” db and “Competed” are moved in some “Archive” db. So, we need to have 2 queries. Then we need to build results properly based on input params. It is doable but even with this simple example not so easy. In real life we may have multiple parameters and we need to handle all permutations of them.
Describe the solution you'd like
I suggest adding a new option to DataLoader constructor. Very similar to existing cacheKeyFn – batchGroupFn?: (key: K) => C;. Based on result of this function batchLoadFn function needs to be called as many times as many different results gives batchGroupFn. I.e. I want to group batches by input keys.
What problem are you trying to solve?
Handling different parameters in field resolvers with dataloader is tricky.
Let’s have this GraphQL schema:
Then we have query:
Here orders resolver with dataloader:
Here ordersLoader with batchLoadFn:
In this batch loader function we need to get orders from a database. If we have all orders stored in one table we can have single query, like:
Then loop through params and return the result. No real issues, so far.
Now let’s think orders in different statuses can’t be queried with a single query. For example, “New” orders are in “Online” db and “Competed” are moved in some “Archive” db. So, we need to have 2 queries. Then we need to build results properly based on input params. It is doable but even with this simple example not so easy. In real life we may have multiple parameters and we need to handle all permutations of them.
Describe the solution you'd like
I suggest adding a new option to DataLoader constructor. Very similar to existing cacheKeyFn – batchGroupFn?: (key: K) => C;. Based on result of this function batchLoadFn function needs to be called as many times as many different results gives batchGroupFn. I.e. I want to group batches by input keys.
For my example it would be:
So for each status it will be separate call of batchLoadFn. It will simplify this function a lot.
Please let me know if this solution is feasible. Or maybe there is alternative/better solution?