我遇到了接口类型和架构公共部分的一些子解析器的问题,这是我的代码和问题。我这样声明可能是错误的,但我给了你目标,如果你有任何其他符合需要的设计,请随时与我分享。
我将 NestJS 7+、Typegoose 和 Nest Graphql 与定义注释自动的 CLI 插件一起使用。
我有一个应用程序,其中有两种用户:Professional和Individual。
在数据库中,它们保存在一个通用文档下,共同的字段存在于文档正文中,而专门的字段存在于是否
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 中不存在
如果我尝试在@ObjectType()
以下@InterfaceType
添加CurrentUserDTO
我知道Error: Schema must contain uniquely named types but contains multiple types named "CurrentUserDTO".
哪个是正常的......因此我不知道......如何为接口类型定义 sub_resolver 我已经死了......</p>
你有什么建议/想法吗?
谢谢你,安德烈亚斯