Use custom decorator to add extra fields to TypeGraphQL @ObjectType class

Use custom decorator to add extra fields to TypeGraphQL @ObjectType class


0

Is there a way to create a custom class-level decorator that adds two parameters to a TypeGraphQL @ObjectType class?

Example:

@ObjectType()
@Timestampable // custom decorator
class User {
    @Field()
    email: string;
}

The @Timestampable decorator should add the createdAt and updatedAt props to the User class and the new props should show up in the gql schema.

I am trying to avoid doing this:

@ObjectType()
@Timestampable
class User {
    @Field()
    email: string;

    @Field(() => Date)
    createdAt: Date;

    @Field(() => Date)
    updatedAt: Date;
}

If yes, then is it possible to stack multiple decorators like this:

@ObjectType()
@Timestampable
@Blamable
class User {
...
}

Do you know a better way to achieve this?


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *