Ref 数组的 TypeGraphQL 和 Typegoose InputType

TypeGraphQL and Typegoose InputType of Ref array

我很难找到解释如何使用 TypeGraphQL 和 Typegoose 处理对象引用数组的资源。

给定以下模型:

@ObjectType({description: "Blog Tag Model"})
export class BlogTag {

  @Field(() => ID)
  id: number;

  @Field(_type => String)
  @Property({required: true, unique: true, index: true, trim: true })
  name: string;
}

@ObjectType({ description: "Blog post model" })
export class BlogPost {

  @Field(() => ID)
  id: number;

  @Field()
  @Property({required: true})
  title: string;

  @Field()
  @Property({required: true})
  description: string;

  @Field(_type => [BlogTag], {nullable: false})
  @Property({default: [], ref: 'BlogTag'})
  tags: Ref<BlogTag>[];
}


@InputType({ description: "Blog post input" })
export class BlogPostInput implements Partial<BlogPost>{

  @Field()
  @Property()
  title: string;

  @Field()
  @Property()
  description: string;

  // Here is the problem / question
  @Field(_type => [BlogTag], {nullable: false})
  @Property({ref: BlogTag})
  tags: Ref<BlogTag>[];   
}

这是针对此代码获得的错误:

CannotDetermineGraphQLTypeError: Cannot determine GraphQL input type for 'tags' of 'BlogPostInput' class. Is the value, that is used as its TS type or explicit type, decorated with
a proper decorator or is it a proper input value?

我想要实现的是让 BlogPost 模型有可能存储 BlogTag 模型的 References/ObejctIDs 数组。

我是这个框架的新手,我不知道这是否是一个好的做法。 如何创建接受 ID 数组的 InputType?

您需要使用 @InputType 创建一个单独的 BlogTagInput 或查看 FAQ 以获取一些方便但“doatyourownrisk” 的解决方法:

https://typegraphql.com/docs/faq.html#situations-frequently-arise-where-inputtype-and-objecttype-have-exactly-the-same-shape-how-can-i-share-the-definitions