typescript 添加参数装饰器会破坏Azure函数

toiithl6  于 12个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(106)

我正在将type-graphql与Azure Functions typescript一起使用,除了尝试创建输入类型时,一切正常,如下所示:

@ObjectType()
class Recipe {
  @Field(type => ID)
  id: string;

  @Field(type => [String])
  ingredients: string[]
}

@Resolver(of => Recipe)
class RecipeResolver {   
    @Query(returns => Recipe, { nullable: true })
    recipe(@Arg("title") title: string): Promise<Recipe | undefined> {
      return undefined
    }
}

@Arg(“title”)似乎破坏了Azure函数,当它在那里时,当我尝试在本地部署和测试函数时,我得到以下错误:
[错误]工作进程无法加载函数graphql:'类型错误:无法读取未定义的属性“0”
如果没有@Arg装饰器,它可以很好地构建和运行,我也在代码的其他地方使用装饰器,它可以正常工作。
我是不是漏掉了什么?

b4lqfgs4

b4lqfgs41#

为了防止这个错误,你只需要指定你的参数的类型。

@ObjectType()
    class Recipe {
      @Field(type => ID)
      id: string;
    
      @Field(type => [String])
      ingredients: string[]
    }
    
    
    @Resolver(of => Recipe)
    class RecipeResolver {   
        @Query(returns => Recipe, { nullable: true })
        recipe(@Arg("title", () => String) title: string): Promise<Recipe | undefined> {
          return undefined
        }

        @Query(returns => Recipe, { nullable: true })
        recipeOptional(@Arg("title", () => String, { nullable: true }) title?: string): Promise<Recipe | undefined> {
          return undefined
        }
    }

相关问题