doug-martin / nestjs-query

Easy CRUD for GraphQL.
https://doug-martin.github.io/nestjs-query
MIT License
823 stars 142 forks source link

Mongoose SubDocument #1543

Open hanza93 opened 2 years ago

hanza93 commented 2 years ago

Have you read the Contributing Guidelines?

Yes.

Hey, before the question here is a context: I'm using mongoose and I have 2 models (JobCard, Part) where JobCard has multiple Parts with different statuses for each Part, so I decided to go with subdocument, and here the problem started.

JobCard schema

@Schema({ timestamps: true })
export class JobCard extends Document {
  // ... some properties here
  @Prop({ type: [JobCardPartModel], default: [] })
  parts: JobCardPart[];
}
export const JobCardModel = SchemaFactory.createForClass(JobCard);

JobCardPart schema

@Schema({ timestamps: true })
export class JobCardPart extends Document {
  @Prop({ type: SchemaTypes.ObjectId, ref: 'Part', required: true })
  part!: Types.ObjectId;
}
export const JobCardPartModel = SchemaFactory.createForClass(JobCardPart);

and the DTOs are as follows

JobCard Dto

@ObjectType('JobCard')
@KeySet(['id'])
export class JobCardDto {
  @IDField(() => ID)
  id!: string;
  // some properties here
  @Field(() => [JobCardPartDto])
  parts: JobCardPartDto[];
}

JobCardPart Dto

@ObjectType('JobCardPart')
@KeySet(['id'])
export class JobCardPartDto {
  @IDField(() => ID)
  id!: string;

  @Field({ nullable: true })
  part: PartDto;
}

Part Dto

@ObjectType('Part')
@KeySet(['id'])
export class PartDto {
  @IDField(() => ID)
  id!: string;

  @Field()
  name: string;

  @Field()
  serialNumber!: string;
}

when I try to run this query

query GetOneJobCard {
  jobCard (id: "62a4592867fe1f9e232b3fbe") {
       id
       parts {
         part {
         id
      }
      status
      declinedReason
    }
  }
}

I'm getting this error ID cannot represent value: { type: \"Buffer\", data: [Array] }.

  1. Am I doing it right?
  2. Is there a better way to do it?

Thanks in advance.

hanza93 commented 2 years ago

@doug-martin sorry but I really stuck on this

kuznetsov-kd commented 2 years ago

@doug-martin sorry but I really stuck on this

This library doesn't work with mongoose v6. You need to use mongoose v5.

smolinari commented 2 years ago

Or try the Typegoose version. Also the updated fork (look through the issues). This package has been dead for a while.

Scott

hanza93 commented 2 years ago

already using the mongoose v5 @DedWatson :(

kuznetsov-kd commented 2 years ago

already using the mongoose v5 @DedWatson :(

You made a mistake. Look at this https://doug-martin.github.io/nestjs-query/docs/persistence/mongoose/relations

@ObjectType('JobCard')
@KeySet(['id'])
@Relation('parts', () => JobCardPartDto, { disableRemove: true })
export class JobCardDto {
  @IDField(() => ID)
  id!: string;
}