type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

feat: Types transformation utils

Open xcfox opened this issue 2 years ago • 11 comments

This PR implements #453 Types transformation utils is a feature that has been on the back burner for a long time.

This PR comes with five utils types as:

  • PartialType: set all fields to nullable
  • RequiredType: set all fields to NON_NULL
  • PickType: only define specified field to a new type
  • OmitType: omit specified field to a new type
  • IntersectionType: combines two types into one new type

Use it like so:

@InputType()
class PartialObject {
  @Field()
  nullableStringField: string;
}

@ArgsType()
class RequiredObject {
  @Field()
  nonNullStringField: string;
}

@InterfaceType()
class PickedObject {
  @Field()
  pickedStringField: string;
}

@ObjectType()
class OmittedObject {
  @Field()
  OmittedStringField: string;
}

@ObjectType()
class SampleObject extends IntersectionType(
  IntersectionType(PartialType(PartialObject), RequiredType(RequiredObject)),
  IntersectionType(
    PickType(PickedObject, "pickedStringField"),
    OmitType(OmittedObject, "OmittedStringField"),
  ),
) {}

It work with class-validator and class-transformer Actually, this code has been in our own repository for a year, it refers to the implementation of nestjs and https://github.com/MichalLytek/type-graphql/issues/453#issuecomment-567049672

In addition, I didn't find a way to implement mapping util that ensures type safety.

xcfox avatar Jun 03 '22 18:06 xcfox

Thank you for your PR ❤️

It work with class-validator and class-transformer

I think class-transformer is not used by TypeGraphQL. Only NestJS is using that.

What I don't like about such monkey-patching is that it does not work with other decorator-based libraries like TypeORM or joiful. Having such official helpers with such issues would be really bad.

What do you think about you publishing this helpers as a npm package and then linking it in the docs here? So that all users know about this but at any time can switch to an alternative implementation supporting other decorator-based libs or even own homecrafted or forked solution? 😉

MichalLytek avatar Jun 05 '22 10:06 MichalLytek

@MichalLytek Publishing this helpers as a npm package is a good idea. Do you think type-graphql-transformation is a good name?

xcfox avatar Jun 06 '22 15:06 xcfox

I like the idea of having a separate npm module for transformation. Can you also update or close #453 @MichalLytek so that anyone who watches there for implementations of your initial proposal knows what the status is? Thank you!

itpropro avatar Jun 25 '22 12:06 itpropro

Hi! We published our transformation utils to the type-graphql-utils package. It exports Pick, Required, Partial, and Omit. It doesn't have any specific code for class-validator like this PR, so I am not sure whether that works as we don't use it.

ChrisLahaye avatar Aug 09 '22 18:08 ChrisLahaye

hello @xcfox, @itpropro @MichalLytek since this has not been published or merged in, i copied the helper functions source-code for transformation into my application and everything works like a charm, my only concern is that i upgraded to typegraphql 2.0 as well. Everything still works fine, i just wanted to know if there were any concerns to adopting this or any issue i'd face in the near future because i see that it uses metadata to understand most of whats going on, so i dont know there might be breaking changes or general concerns

okaforcj avatar Nov 06 '22 09:11 okaforcj

@xcfox @ChrisLahaye i think i found a bug, if you use PartialType/Partial on arrays, it seems like it strips the array and give its parameriterised value i.e. when using partial type on array of strings, it will just return a string as metadata for graphql, so my graphql types are not reflecting the actual types anymore even though they'd have been annotated before passing it through the PartialType function, Any thoughts on this please?

okaforcj avatar Nov 16 '22 09:11 okaforcj

@okaforcj Are you experiencing this issue on my library or the code here?

ChrisLahaye avatar Nov 16 '22 10:11 ChrisLahaye

@okaforcj Are you experiencing this issue on my library or the code here?

both @ChrisLahaye, i tested your library as well before commenting, can you check it out or should i create a sample. like ive checked again and its happening for other util functions like required not just partial. Basically when you try to apply the util function with arrays, it drops the arrays in typegraphql/the graphql schema, even though the class in typescript world is still showing the array. I'm thinking it has something to do with how the metadata is been passed ?

okaforcj avatar Nov 16 '22 10:11 okaforcj

@okaforcj I see the same issue has been reported at https://github.com/ChrisLahaye/type-graphql-utils/issues/3. I will look into it.

ChrisLahaye avatar Nov 16 '22 10:11 ChrisLahaye

@okaforcj I just released version 2.0.0 which should fix this issue. Credits to @caldarellial for reporting this issue and providing the fix.

ChrisLahaye avatar Nov 16 '22 11:11 ChrisLahaye

@okaforcj I just released version 2.0.0 which should fix this issue. Credits to @caldarellial for reporting this issue and providing the fix.

that was quick! thanks for the help!

okaforcj avatar Nov 16 '22 12:11 okaforcj