TriPSs / nestjs-query

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

Issue with Remove Relation/set relation to null in Many to One relation #243

Open coupster74 opened 7 months ago

coupster74 commented 7 months ago

Describe the bug QueryService setRelation and removeRelation don't work as expected for ManyToOne relationships.

Have you read the Contributing Guidelines?

yes

To Reproduce if you have a many to one relation on an entity, you can set it via code like this:

event = await this.eventService.setRelation(
          'company',
          input.id,
          input.update.companyId,
        );

however, there is no convenient way to remove the relationship. I would think you could set it to null, but this does not work:

event = await this.eventService.setRelation(
          'company',
          input.id,
          null,
        );

and for some reason, removeRelation still requires the ID to be removed, which while that makes sense if the relationshp was many to many and I was plucking on out of a collection, in a many to one, we are just trying to clear the single value, so removeRelation should not need a relationId.

Consider the following, simple, clean code:

 if ('companyId' in inputEvent) {
      event = await this.eventService.setRelation(
        'company',
        event.id,
        inputEvent.companyId,
      );
    }

if setRelation supported a null companyId to clear the relationship, this is all I'd need to support setting and removing the relationship.

so.. options: setRelation - could take a null for relationId and remove the relationship (similar in nature to the collection approach where you can just set the new collection). removeRelation - remove the relationId parameter and just remove whatever relationship may exist

Desktop (please complete the following information):