graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Support for Custom Fields Decorators in Mapped types

Open Copystrike opened this issue 10 months ago • 0 comments

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

The problem is that when using tools like Automapper, Mapped types lose their functionality because Automapper requires each field to be decorated with @AutoMap().

When looking for a solution, I found a Stack Overflow post that describes the same problem I'm experiencing: https://stackoverflow.com/questions/77825626/automapper-and-mapped-type-in-nest-js

Describe the solution you'd like

Here's the solution I believe would be best and flexible:

export class TodoItem extends PartialType(CreateTodoItemInput, {
  customFields(partialObjectType, item) {
    if (item.name !== 'type') {
      AutoMap()(partialObjectType.prototype, item.name);
    }
  },
}) {}

Developers will have the option to use the customFields parameter, which takes two parameters:

interface PartialTypeOptions {
  decorator?: ClassDecoratorFactory;
  omitDefaultValues?: boolean;
  customFields?: (partialObjectType: typeof PartialObjectType, item: PropertyMetadata) => void;
}

With this solution, users can select which fields to decorate or apply decoration to all fields if needed. This offers greater control and customization in field decoration.

Teachability, documentation, adoption, migration strategy

I've already created a custom mapped type available here: https://gist.github.com/Copystrike/bfc5010100aba362002616f9eca7fa25

This Gist demonstrates the functionality I described in "Describe the solution you'd like." However, I haven't submitted a pull request because I believe this functionality should be added to all Mapped Types and thoroughly tested, which I currently don't have time for. But the gist works effectively for me!

Also worth mentioning, when defining the PartialType second parameter it instantly tries to execute the decorator which is not being used which causes an error, I did fix it in my Gist but I don't know if the fix itself is good since I used AI for this.

What is the motivation / use case for changing the behavior?

The motivation for changing the behavior is to provide users with greater flexibility and control over field decoration when using tools like AutoMap in NestJS mapped types. By allowing users to specify which fields should be decorated, they can tailor the mapping configuration to their specific needs.

Copystrike avatar Mar 30 '24 17:03 Copystrike