【问题标题】:Graphql Nestjs add arguments to type fieldGraphql Nestjs 将参数添加到类型字段
【发布时间】:2021-06-19 06:18:53
【问题描述】:

你好,你好吗? 过去一周我一直在学习 Nestjs,并且非常喜欢它。 我目前正在将它与 Prisma 2 一起使用来构建 graphql 服务器。 我想在 graphql 类型字段中添加参数以便能够过滤或 使用 prisma 对结果进行排序。 明确地说,我希望能够执行以下架构

query {
  findUniqueUser(where: { id: 1 }) {
    id
    email
    name
    posts(where: { title: { contains: "a" } }, orderBy: { createdAt: asc }, first: 10, skip: 5) {
      id
      title
      comments(where: { contain: { contains: "a" } }) {
        id
        contain
      }
    }
  }
}

我正在使用库 @paljs/plugins 从 GraphQLResolveInfo 中提取信息并对 prisma 进行查询。 我还使用库prisma-nestjs-graphql 来自动生成所有graphql 类型定义。 有没有人尝试过使用nestjs? 谢谢你,祝你有美好的一天

编辑:我使用的是代码优先的 graphql

编辑 2: 在四处挖掘之后,我找到了一种使用@ResolveField的方法。

@Query((returns) => Patient, { nullable: true, name: "patient" })
    async getPatient(@Args() params: FindUniquePatientArgs, @Info() info: GraphQLResolveInfo) {
        const select = new PrismaSelect(info).value;
        params = { ...params, ...select };
        return this.prismaService.patient.findUnique(params);
    }

    @ResolveField(() => File)
    async files(@Parent() patient: Patient, @Args() params: FindManyFileArgs) {
        return patient.files;
    }

我不知道这是否是最好的方法,所以我暂时不回答这个问题。

【问题讨论】:

    标签: typescript graphql nestjs prisma2


    【解决方案1】:

    我将把它留在这里作为答案

        @Query((returns) => Patient, { nullable: true, name: "patient" })
        async getPatient(@Args() params: FindUniquePatientArgs, @Info() info: GraphQLResolveInfo) {
            const select = new PrismaSelect(info).value;
            params = { ...params, ...select };
            return this.prismaService.patient.findUnique(params);
        }
    
        @ResolveField(() => File)
        async files(@Parent() patient: Patient, @Args() params: FindManyFileArgs) {
            return patient.files;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-21
      • 2018-09-16
      • 2018-06-02
      • 2019-03-05
      • 2021-06-13
      • 2011-09-18
      • 2018-11-01
      • 1970-01-01
      相关资源
      最近更新 更多