I have a class Template
with a property _id
which has decorators from class-transformer
and typed-graphql
import { Expose, Type, TypeOptions, ExposeOptions } from 'class-transformer';
/**
* Combines @Expose then @Types decorators.
* @param exposeOptions options that passes to @Expose()
* @param typeFunction options that passes to @Type()
*/
function ExposeToGraphQL(exposeOptions?: ExposeOptions, typeFunction?: (type?: TypeOptions) => Function) {
const exposeFn = Expose(exposeOptions);
const typeFn = Type(typeFunction);
return function (target: any, key: string) {
typeFn(target, key);
exposeFn(target, key);
}
}
Then you can use that decorator as follow:
class Template extends Typegoose {
@ExposeToGraphQL(/*exposeOptions*/ undefined, /*typeFunction*/ () => String)
@Field(() => ID)
public _id?: mongoose.Types.ObjectId;
}
You can find official documentation for decorator in this link.
@Expose
and @Type()
are basically Decorator Factories. The main purpose of a decorator factory:
Template
, was defined) with 2 arguments:
Template.prototype
) _id
).If two or more decorators are attached to a same property (called as Decorator Composition), they are evaluated as follow: