My current dilemma revolves around a specific issue related to the definition of my Cart type, which is structured as follows:
@ObjectType()
export class Cart {
@Field(() => ID)
id: string;
@Field((_type) => String)
ownerId: String
@Field((_type) => User)
owner: User;
}
To resolve the owner of each cart effectively, I have implemented a type resolver:
@FieldResolver((_type) => User)
async owner(@Root() cart: Cart): Promise<User> {
return (await UserModel.findById(cart.ownerId))!
}
However, I have encountered inefficiencies when querying multiple carts due to the FieldResolver being called individually for each cart. As a solution, I am looking for a way to optimize this process by fetching all cart IDs and retrieving the corresponding owners in a single query.
If there are any insights or solutions on how to achieve this more efficiently, your input would be greatly appreciated. Thank you.