I am new using graphql API, so maybe what i am asking is something easy to do
i want to filter nested data in my schema this is my schema whit typeorm, everything is ok wit the relations and i know how to handle te N + 1 problem.
@ObjectType()
@Entity()
export class Product extends BaseEntity
{
@Field()
@PrimaryGeneratedColumn()
Id_Product!: Number;
@Field()
@Colum()
Name: String;
@Field(() => [Product _Photo], { nullable: true })
@OneToMany(() => Product _Photo, product_photo => product _photo .Photo)
Product _Photo: Product _Product [];
@Field(() => [Category], { nullable: true })
@OneToMany(() => Category, category=> category.Category )
Category: Category [];
@Field()
@CreateDateColumn()
CreatedAt: Date;
@Field()
@CreateDateColumn()
UpdateAt: Date;
}
so now to handle the one to many relation i need to make to field resolvers to fetch the data, this is my resolver
@Resolver(Product)
export class ProductResolver
{
@FieldResolver(() => Product)
async Product_Photo(@Root() Product: Product)
{
return await Product_Photo.find({ Id_Product: Product.Id_Product});
}
@FieldResolver(() => Product)
async Category(@Root() Product: Product)
{
return await Category.find({ Id_Product: Product.Id_Product});
}
@Query(() => [Product], { nullable: true })
async GetProduct(): Promise<Product[]>
{
// need to filter sort and paginate the product
// FILTER BY CATEGORY
// Product.find().filter({ Category: ['Art', 'Food'] })
return await Product.find();;
}
}
so i can do a filter in javascript or whit typeorm, the problem is the category field is empty until the GetProduct query runs and in the filed resolver i only can get the category of one product, to filter the products by category i need to do it after field resolvers run, there is a way to do this?, if it is i can do a filter sort and paginate all the data and send the data perfectly.
if is not a way to get the data after all field resolvers run, how can i do this in another way ?, I don't want to run the one to many querys inside the GetProduct query i feel this can be done in a better way