【问题标题】:Property is missing in type prisma/client/index, when adding a custom type to Prisma schema将自定义类型添加到 Prisma 模式时,类型 prisma/client/index 中缺少属性
【发布时间】:2022-03-17 11:36:11
【问题描述】:

当我收到此错误消息时,我的自定义类型“Observations”似乎没有正确生成

类型中缺少属性“观察” 'import("/Users/thomasandrew/Documents/webApps/social-innovate/boilerplate/packages/api/node_modules/.prisma/client/index").ActionPlan' 但在类型中需要 'import("/Users/thomasandrew/Documents/webApps/social-innovate/boilerplate/packages/api/src/modules/actionPlan/actionPlan.model").ActionPlan'.ts(2741) actionPlan.model.ts(25, 5): 'observations' 在这里声明。

这是我的 Prisma 架构文件(删除了一些无关字段)

model Observation {
  id                      String @id @default(dbgenerated("gen_random_uuid()"))
  actionPlan              ActionPlan  @relation(fields: [actionPlanId], references: [id])
  actionPlanId            String
  meetingDate             DateTime?
  place                   String?
}

model ActionPlan {
    id          String  @id @default(dbgenerated("gen_random_uuid()")) @unique
    testName    String
    observations Observation[]
}

type-graphql 模型

@ObjectType()
export class ActionPlan extends BaseModel implements Prisma.ActionPlan {
    @Field()
    testName: string

    @Field()
    department: string

    @Field()
    code: string

    @Field()
    outcome: string

    @Field()
    hypothesis: string

    @Field(type => [Observation])
    observations: Observation[]
}

但是,即使我成功运行 prisma generate,它也不会将“观察”添加到行动计划中。我错过了什么吗?任何帮助都会很棒。我还附上了生成类型的屏幕截图 node_modules/.prisma/client/index.d.ts

.

【问题讨论】:

    标签: javascript node.js prisma typegraphql


    【解决方案1】:

    由于您在这两者之间存在关系,因此您的代码应该提供一个包含参数,以便您的 @Resolver 和 @Query 可以在响应中填充该实体。
    另外,我不知道它是否在您的ActionPlanService prisma 连接中(可能是的,因为您收到此错误)。
    因此,例如,您的第一个查询应该指向 ActionPlanService 函数:

    async getActionPlan(id: string): Promise<ActionPlan> {
       return await this.prismaService.actionPlan.findFirst({
          where: {
             id,
          },
          include: {
             observations: true,
          }
        });
    
    

    或类似的东西,因为我不知道您如何在该服务中与 prisma 连接

    【讨论】:

      【解决方案2】:

      Prisma 不包含生成类型中的关系,因为并非所有查询都包含它们。 Prisma 查询确实会根据使用的选项返回关系,但在生成的代码中,这是通过复杂的 TypeScript 完成的。

      但您始终可以手动定义包含关系的类型,或使用 ReturnType&lt;T&gt;Awaited&lt;T&gt; 实用程序的组合。

      手动定义示例:

      type ActionPlanWithObservations = ActionPlan & {
        observations: Observation[]
      }
      

      使用 Awaited 和 ReturnType 的示例:

      type ActionPlanWithObservations =
              Awaited<ReturnType<ActionPlanService["getActionPlan"]>>
      

      ActionPlanService 在哪里:

      class ActionPlanService {      
        getActionPlan(id: string) {
          return prisma.actionPlan.findFirst({
            where: {
               id,
            },
            include: {
               observations: true,
            }
          })
        }
      }
      

      注意:Awaited&lt;T&gt; 实用程序自 TypeScript 4.5 起可用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-29
        • 2017-09-28
        • 2022-08-19
        • 2021-11-18
        • 2021-09-13
        • 2021-10-12
        • 1970-01-01
        • 2021-12-20
        相关资源
        最近更新 更多