【发布时间】:2020-09-04 03:19:00
【问题描述】:
我遇到了接口类型和架构公共部分的一些子解析器的问题,这是我的代码和问题。我这样说可能是错误的,但我给了你目标,如果你有任何其他符合需要的设计,请随时与我分享。
我正在使用 NestJS 7+、Typegoose 和 Nest Graphql 以及定义自动注释的 CLI 插件。
我有一个应用程序,其中有两种用户:专业和个人。
在数据库中,它们保存在一个公共文档下,共同的字段存在于文档正文中,专门的字段存在于
individual或legalEntity键(它是一个对象)中。
@ObjectType()
class IndividualData {
@prop()
firstName: string;
@prop()
lastName: string;
}
@ObjectType()
class LegalEntity {
@prop()
tradeName: string;
}
@ObjectType({ description: "DO NOT USE, JUST FOR EXTENDING" })
export class User {
@Field(() => ObjectId)
_id: ObjectId;
@Field(() => Date)
createdAt: Readonly<Date>;
@Field(() => Date)
updatedAt: Readonly<Date>;
@Field(() => EmailScalar)
@prop({ unique: true })
email: string;
@Field(() => PasswordScalar)
@prop()
password: string;
@prop()
validationToken?: string;
@prop()
isValidated: boolean;
@prop()
isVolunteer: boolean;
@prop()
lastLogin: Date;
@prop()
photo?: string;
@prop()
phone?: string;
@prop()
city?: string;
@prop()
country?: string;
@prop()
individual?: IndividualData; // For individuals
@prop()
legalEntity?: LegalEntity; // For pro
@Field(() => [ProductDTO])
@arrayProp({ items: ObjectId })
listings: Types.Array<ObjectId>; // Sale products as user
@Field(() => VolunteersTeam) // Used in join
@prop()
volunteersTeam?: ObjectId;
@Field(() => [Supplier]) // Used in join
@arrayProp({ items: ObjectId })
suppliers?: Types.Array<ObjectId>; // Suppliers space in which user is member
}
@InterfaceType({
resolveType(item: UserDocument) {
return item.legalEntity ? "ProfessionnalUserDTO" : "IndividualUserDTO";
},
})
export class CurrentUserDTO extends OmitType(
User,
["password", "validationToken", "legalEntity", "individual"],
InterfaceType,
) {}
@ObjectType({
implements: [CurrentUserDTO],
})
export class IndividualUserDTO extends CurrentUserDTO {
firstName: string;
lastName: string;
}
@ObjectType({
implements: [CurrentUserDTO],
})
export class ProfessionnalUserDTO extends CurrentUserDTO {
legalName: string;
}
现在这是我的子解析器,它用于两个子字段共用,名为 suppliers
@Resolver(() => CurrentUserDTO)
export class CurrentUserDTOResolver {
constructor(
private readonly supplierService: SupplierService,
private readonly volunteerService: VolunteerService,
private readonly productService: ProductService,
) {}
@ResolveField(() => [Supplier])
async suppliers(@Parent() user: CurrentUserDTO) {
return user.suppliers.length > 0
? this.supplierService.find({ _id: { $in: user.suppliers } })
: [];
}
}
我现在明白了
(node:58160) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'properties' of undefined
有什么想法吗?
看起来 du to @Resolver(() => CurrentUserDTO), CurrentUserDTO 在 compileExternalFieldResolverMetadata 的 this.objectTypes 中不存在
如果我尝试在CurrentUserDTO@InterfaceType 下添加@ObjectType()
我收到Error: Schema must contain uniquely named types but contains multiple types named "CurrentUserDTO". 这很正常……
所以我不知道……
如何为接口类型定义 sub_resolver
我已经死了……
您有什么建议/想法吗?
谢谢你, 安德烈亚斯
【问题讨论】: