首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ref阵列的TypeGraphQL和Typegoose

Ref阵列的TypeGraphQL和Typegoose
EN

Stack Overflow用户
提问于 2021-01-31 18:20:33
回答 1查看 504关注 0票数 0

我很难找到资源,这说明了如何使用TypeGraphQL和Typegoose来处理对对象的一系列引用。

鉴于以下模式:

代码语言:javascript
复制
@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>[];   
}

这是为此代码获得的错误:

代码语言:javascript
复制
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模型的引用/ObejctID数组的可能性。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-01 13:08:00

您需要使用BlogTagInput创建一个单独的@InputType,或者检查FAQ以找到一些方便的、但“做好准备”的解决方法:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65982685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档