Dataloader with many to one relationship #275
-
Hi, I cannot figure out how to use the dataloader with a many relationship. For example: I have a user with many comments. The user_id is stored with the comments and I want to fetch all users with all their comments. In order to not have a n+1 problem I would like to use a dataloader. type User = {
id: number
name: string
}
type Comment = {
id: number;
user_id: number;
message: string
}
const UserObject = builder.objectRef<User>("User")
const CommentObject = builder.objectRef<Comment>("Comment")
const CommentLoadable = builder.loadableObject(CommentObject, {
load: () => ({................}),
fields: (t) => ({
id: t.exposeID("id"),
message: t.exposeString("message")
})
})
UserObject.implement({
fields: (t) => ({
id: t.exposeID("id"),
comments: t.field({
type: [CommentLoadable],
resolve: (user) => user.id
})
})
}) When using a standard graphql/dataloader I would make a CommentDataloaderByUserId and load a group of result by user_id. But the Any help would be highly appreciated! Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Unfortunately this is something that is currently missing from the dataloader plugin. Definitely something I should add support for, but for now, I think you just need to manually use a dataloader. It shouldn't be too hard to add a new field builder method for many-to-one relationships. I'll probably add a I personally have not run into this yet, because most of my 1-to-many relationships could not be batch loaded, and 1-to-many relationships generally aren't re-used. Definitely just an oversight on my part. |
Beta Was this translation helpful? Give feedback.
-
Added support for this in the latest version: https://www.pothos-graphql.dev/docs/plugins/dataloader#loadablelist-fields-for-one-to-many-relations |
Beta Was this translation helpful? Give feedback.
Added support for this in the latest version: https://www.pothos-graphql.dev/docs/plugins/dataloader#loadablelist-fields-for-one-to-many-relations