import { uniqBy } from 'lodash'; import { Ctx, Field, ObjectType } from 'type-graphql'; import { Column, Entity, ManyToOne, OneToMany } from 'typeorm'; import { RetailerProductVariant } from '@/entities'; import { ContextType } from '@/types'; import { AggregateProduct } from '../AggregateProduct'; import { AbstractRetailerProductData } from './AbstractRetailerProductData'; @Entity({ name: 'retailer_products' }) @ObjectType({ implements: AbstractRetailerProductData.objectTypeImplements, }) export default class RetailerProduct extends AbstractRetailerProductData { @OneToMany(() => RetailerProductVariant, (variant) => variant.product) variants!: RetailerProductVariant[]; @Field(() => [RetailerProductVariant], { name: 'variants' }) async variantsResolver( @Ctx() { dbDataLoader }: ContextType ): Promise { return uniqBy( await dbDataLoader.retailerProductVariantsByRetailerProductId.load( this.id ), 'retailer' ); } @ManyToOne( () => AggregateProduct, (aggregateProduct) => aggregateProduct.retailerProducts, { nullable: false, } ) @Field(() => AggregateProduct) aggregateProduct!: AggregateProduct; @Column() aggregateProductId!: string; }