【问题标题】:nestjs graphql with entity relations - how to decorate it?带有实体关系的nestjs graphql - 如何装饰它?
【发布时间】:2021-04-11 09:51:44
【问题描述】:

当我有关系时,我在“装饰”类时遇到问题,这些类应该用作 ObejctType 和 InputType。应该可以在其他类中使用关系类

例如有地址的客户,有地址的用户

@ObjectType()
@InputType()
export class Address {
  @Field()
  street: string;

  @Field()
  city: string;
}


@ObjectType()
@InputType()
export class Customer {
  @Field()
  name: string;

  @Field(() => Address)
  address: Address;
}


@ObjectType()
@InputType()
export class User {
  @Field()
  name: string;

  @Field(() => Address)
  address: Address;
}

我也想使用 Customer 和 User 作为 Mutations 的输入参数。但我没有成功。我尝试了 InputType 和 ObjectType 的“isAbstract”选项的变体,但我对这个解决方案不满意。

谁能给我一个提示如何做到这一点。

【问题讨论】:

    标签: graphql nestjs


    【解决方案1】:

    如果遇到冲突错误,则需要明确指定 ObjectTypes 和 InputTypes 的名称。

    所以试试这样的:

    @ObjectType("AddressType")
    @InputType("AddressInput")
    export class Address {
      @Field()
      street: string;
    
      @Field()
      city: string;
    }
    

    看到这个Use the same class as Input and Object type in GraphQL in NestJS。

    【讨论】:

    • 感谢您的回答。是的 InputType 和 ObjectType 的“命名”有效。
    猜你喜欢
    • 2021-06-02
    • 2019-07-24
    • 2020-07-24
    • 2019-08-24
    • 2022-01-06
    • 2021-10-25
    • 1970-01-01
    • 2021-11-14
    • 2022-09-27
    相关资源
    最近更新 更多