TriPSs / nestjs-query

Easy CRUD for GraphQL.
https://tripss.github.io/nestjs-query/
MIT License
164 stars 46 forks source link

UnPagedRelation Setup need help #258

Closed KCH0000 closed 6 months ago

KCH0000 commented 6 months ago

I can't set up UnPagedRelation.

There is a One to Many connection (Part -> Part Price) my code

part.dto.ts

@ObjectType('part')
@FilterableRelation('manufacturer', () => ManufacturerDTO)
@UnPagedRelation('partPrice', () => PartPriceDTO)
@QueryOptions({
  pagingStrategy: PagingStrategies.OFFSET,
  maxResultsSize: 50,
})
export class PartDTO {
  @IDField(() => ID)
  id!: string;

  @FilterableField(() => ID)
  manufacturer!: ManufacturerDTO;

  @FilterableField(() => [PartPriceDTO])
  partPrice: PartPriceDTO[];

  @FilterableField()
  name!: string;

  @FilterableField()
  number!: string;

  @Field()
  universal: boolean;

  @Field()
  unit: number;
}

part.entity.ts

@Entity('part')
export class PartEntity {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @ManyToOne(() => ManufacturerEntity)
  @JoinColumn({ name: 'manufacturer_id' })
  manufacturer!: ManufacturerEntity;

  @Column({ type: 'varchar', length: 255, nullable: false })
  name!: string;

  @Column({ type: 'varchar', length: 64, nullable: false })
  number!: string;

  @Column({ type: 'bool', default: false })
  universal: boolean;

  @Column({ type: 'int2', default: 1 })
  unit: number;

  @OneToMany(() => PartPriceEntity, (price) => price.part)
  partPrice: PartPriceEntity[];
}

part-price.dto.ts

@ObjectType('partPrice')
export class PartPriceDTO {
  id: string;

  part: PartDTO;

  @Field({ nullable: true })
  price: number;

  @Field(() => GraphQLISODateTime, {
    nullable: true,
    name: 'updated',
  })
  updated: Date;
}

part-price.entity.ts

@Entity('part_price')
export class PartPriceEntity {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @ManyToOne((): ObjectType<PartEntity> => PartEntity, (part) => part.partPrice)
  @JoinColumn({ name: 'part_id' })
  part!: PartEntity;

  @Column({ name: 'price_amount', type: 'int8', nullable: true })
  price: number;

  @UpdateDateColumn({
    name: 'updated_at',
    type: 'timestamp with time zone',
    nullable: true,
    default: () => 'CURRENT_TIMESTAMP',
  })
  updated: Date;
}

Error

Error: Unable to create filter comparison for [null].
    at getTypeName (/home/kach/projects/automagistre/backend_nest/packages/query-graphql/src/types/query/field-comparison/field-comparison.factory.ts:66:9)
    at getComparisonTypeName (/home/kach/projects/automagistre/backend_nest/packages/query-graphql/src/types/query/field-comparison/field-comparison.factory.ts:79:13)
    at createFilterComparisonType (/home/kach/projects/automagistre/backend_nest/packages/query-graphql/src/types/query/field-comparison/field-comparison.factory.ts:95:21)
    at /home/kach/projects/automagistre/backend_nest/packages/query-graphql/src/types/query/filter.type.ts:109:37
    at Array.forEach (<anonymous>)
    at /home/kach/projects/automagistre/backend_nest/packages/query-graphql/src/types/query/filter.type.ts:105:12
    at MapReflector.memoize (/home/kach/projects/automagistre/backend_nest/packages/core/src/common/reflect.utils.ts:107:20)
    at getOrCreateFilterType (/home/kach/projects/automagistre/backend_nest/packages/query-graphql/src/types/query/filter.type.ts:61:20)
    at DeleteFilterType (/home/kach/projects/automagistre/backend_nest/packages/query-graphql/src/types/query/filter.type.ts:158:10)
    at DeleteManyInputType (/home/kach/projects/automagistre/backend_nest/packages/query-graphql/src/types/delete-many-input.type.ts:18:29)

Desktop (please complete the following information):

Then i use

@Field(() => [PartPriceDTO])

Error: No fields found to create GraphQLFilter for PartPriceDTO

and if empty Field

@Field()

Error: Undefined type error. Make sure you are providing an explicit type for the "partPrice" of the "PartDTO" class.

TriPSs commented 6 months ago

Think you need to remove:

 @FilterableField(() => [PartPriceDTO])
  partPrice: PartPriceDTO[];

As that is already done by the @UnPagedRelation('partPrice', () => PartPriceDTO)

You would also need to add the id fields, otherwise the mapper will not be able to map to the correct entities.

@ManyToOne((): ObjectType<PartEntity> => PartEntity, (part) => part.partPrice)
@JoinColumn({ name: 'part_id' })
part!: PartEntity;

@Column({ name: 'part_id' })
partId!: PartEntity;
KCH0000 commented 6 months ago

Thanks for the help I deleted

@FilterableField(() => [PartPriceDTO])

and did not delete it

partPrice: PartPriceDTO[];

so that Resolver would not output a type error in Entity and DTO

and rename fied part -> partId, to avoid making a record

@Column({ name: 'part_id' })
partId!: PartEntity;
Error: No fields found to create GraphQLFilter for PartPriceDTO

I'm trying very hard, but I don't understand how it works :)

TriPSs commented 6 months ago

Hmm, could you create repo with the issue? Then I can check it out for ya.

KCH0000 commented 6 months ago

https://github.com/KCH0000/graphgq-nest-issue

TriPSs commented 6 months ago

Thanks, if you update one field in PartPriceDTO to have FilterableField it will work.

KCH0000 commented 6 months ago

Oh my God). Thank you so much, you saved me a lot of time. I would never have guessed before these changes.