【发布时间】:2019-01-28 20:53:37
【问题描述】:
我有一个类 Template 有一个属性 _id 有来自 class-transformer 和 typed-graphql 的装饰器
import {classToPlain, Exclude, Expose, plainToClass, Type } from 'class-transformer';
import { ExposeToGraphQL } from '../../decorators/exposeToGraphQL';
import { Field, ID, MiddlewareInterface, NextFn, ObjectType, ResolverData } from 'type-graphql';
import { getClassForDocument, InstanceType, prop, Typegoose } from 'typegoose';
/**
* Class
* @extends Typegoose
*/
@Exclude()
@ObjectType()
class Template extends Typegoose {
// @Expose and @Type should be both covered by ExposeToGraphQL
// @Expose()
@Type(() => String)
@ExposeToGraphQL()
@Field(() => ID)
public _id?: mongoose.Types.ObjectId;
}
现在我尝试将这两者组合成一个新的自定义属性装饰器:
/**
*
*/
import { Expose } from 'class-transformer';
import 'reflect-metadata';
const formatMetadataKey: Symbol = Symbol('ExposeToGraphQL');
function ExposeToGraphQL() {
console.log('ExposeToGraphQL');
return Expose();
}
function getExposeToGraphQL(target: any, propertyKey: string) {
console.log('getExposeToGraphQL');
return Reflect.getMetadata(formatMetadataKey, target, propertyKey);
}
export {
ExposeToGraphQL,
getExposeToGraphQL,
};
如果我只返回Expose() 的结果,自定义装饰器就可以工作,但我不知道如何在@ExposeToGraphQL() 中组合@Expose 和@Type。
【问题讨论】:
-
如果你想让
ExposeToGraphQL同时满足Expose和Type的目的,对于初学者来说,它不需要采用与Type相同的参数吗? -
我以为以后会担心参数:)
标签: typescript decorator