【问题标题】:How to access graphql field name in nestjs?如何在nestjs中访问graphql字段名称?
【发布时间】:2021-11-17 18:48:11
【问题描述】:

我正在尝试访问以下输入中字段装饰器的名称值。

//myInput.ts

@InputType()
export class MyInput {
  @Field(() => Int, { name: "price" })
  priceValue: number;
}
//myResolver.ts

@Resolver()
export class MyResolver {
  constructor(private readonly myService: MyService){}

  @Query(() => boolean)
  async prices(@Args('input') input: MyInput){
    /*
      So in typescript system the input field is named `priceValue` but in the
      graphql side is named `price` (This is the string i want to access)
    */
  }

}

我尝试了各种方法,例如使用 @Info() 装饰器和 TypeMetadaStorage,但我找不到重命名值。

有什么办法可以访问这个字段吗?

【问题讨论】:

    标签: typescript graphql nestjs


    【解决方案1】:

    我想出了一个使用 TypeMetadataStorage 方法 getArgumentsMetadataByTarget(MyInput);

      const schemaNames = TypeMetadataStorage.getArgumentsMetadataByTarget(MyInput).properties.map(({ schemaName }) => schemaName);
    

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 2019-02-23
      • 1970-01-01
      • 2021-02-23
      • 2016-11-26
      • 2021-04-03
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多