【问题标题】:NestJS x Graphql x Typegoose issue on sub resolver子解析器上的 NestJS x Graphql x Typegoose 问题
【发布时间】:2020-09-04 03:19:00
【问题描述】:

我遇到了接口类型和架构公共部分的一些子解析器的问题,这是我的代码和问题。我这样说可能是错误的,但我给了你目标,如果你有任何其他符合需要的设计,请随时与我分享。

我正在使用 NestJS 7+、Typegoose 和 Nest Graphql 以及定义自动注释的 CLI 插件。

我有一个应用程序,其中有两种用户:专业个人

在数据库中,它们保存在一个公共文档下,共同的字段存在于文档正文中,专门的字段存在于 individuallegalEntity键(它是一个对象)中。

@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(() =&gt; CurrentUserDTO), CurrentUserDTO 在 compileExternalFieldResolverMetadata 的 this.objectTypes 中不存在

如果我尝试在CurrentUserDTO@InterfaceType 下添加@ObjectType()

我收到Error: Schema must contain uniquely named types but contains multiple types named "CurrentUserDTO". 这很正常…… 所以我不知道…… 如何为接口类型定义 sub_resolver 我已经死了……

您有什么建议/想法吗?

谢谢你, 安德烈亚斯

【问题讨论】:

    标签: graphql nestjs typegoose


    【解决方案1】:

    由于您无法在 NestJS 中为接口(通常在 grpahql 中)定义解析器,您可以通过执行以下操作来实现该行为:

    // One annotation per things that implement my CurrentUserDTO interface
    @Resolver(() => IndividualUserDTO)
    @Resolver(() => ProfessionnalUserDTO)
    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 } })
          : [];
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-27
      • 2020-01-18
      • 2021-07-27
      • 2018-01-21
      • 2021-04-15
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多