typescript typegraphql将查询字段委托给字段解析器

csga3l58  于 2022-11-18  发布在  TypeScript
关注(0)|答案(1)|浏览(125)

我需要一个将其字段委托给下面的字段解析程序的用户查询。我从查询返回什么才允许此行为?

@ObjectType()
class User {
  @Field((_type) => ID)
  id!: string;

  @Field((_type) => [View])
  saved!: View[];
}

@Resolver(of => User)
export class UserResolver {
  @Authorized()
  @Query((_returns) => User, { nullable: false })
  async user(@Ctx() { prisma, uid }: context.AuthorizedContext) {
    return {
      id: uid,
    };
  }

  @FieldResolver()
  async saved(
    @Root() { id }: User,
    @Ctx() { prisma }: context.AuthorizedContext
  ) {
    console.log("HELLO")

    const savedViews = await prisma.saved.findMany({
      where: { userId: id },
      include: { View: true },
    });

    return savedViews.map((dboView) => dboView.View);
  }
}

我尝试过不返回查询中的字段,但得到了Cannot return null for non-nullable field User.saved

eqqqjvef

eqqqjvef1#

是否尝试过从类中删除字段声明?字段解析器足以将字段添加到架构中。

@ObjectType()
class User {
  @Field((_type) => ID)
  id: string;
}

在冲突解决程序中:

@FieldResolver() // this is sufficient!
  async saved( ... )

相关问题