0

我很难找到解释如何使用 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模型提供存储模型的引用/对象 ID 数组的可能性BlogTag

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

4

1 回答 1

1

您需要创建一个单独BlogTagInput@InputType或查看常见问题解答以获得一些方便但“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

于 2021-02-01T13:08:00.703 回答