最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Property is missing in type prismaclientindex, when adding a custom type to Prisma schema - Stack Overflow

programmeradmin4浏览0评论

My custom type "Observations" doesn't seem to be generating correctly as I am getting this error message

Property 'observations' is missing in type 'import("/Users/thomasandrew/Documents/webApps/social-innovate/boilerplate/packages/api/node_modules/.prisma/client/index").ActionPlan' but required in type 'import("/Users/thomasandrew/Documents/webApps/social-innovate/boilerplate/packages/api/src/modules/actionPlan/actionPlan.model").ActionPlan'.ts(2741) actionPlan.model.ts(25, 5): 'observations' is declared here.

This is my Prisma schema file (removed some extraneous fields)

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 model

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

    @Field()
    department: string

    @Field()
    code: string

    @Field()
    oute: string

    @Field()
    hypothesis: string

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

However even when I run prisma generate successfully, it doesn't add the 'observations' to the Action Plan. Am I missing something? Any help would be great. I also attached screenshot of generated types at node_modules/.prisma/client/index.d.ts

.

My custom type "Observations" doesn't seem to be generating correctly as I am getting this error message

Property 'observations' is missing in type 'import("/Users/thomasandrew/Documents/webApps/social-innovate/boilerplate/packages/api/node_modules/.prisma/client/index").ActionPlan' but required in type 'import("/Users/thomasandrew/Documents/webApps/social-innovate/boilerplate/packages/api/src/modules/actionPlan/actionPlan.model").ActionPlan'.ts(2741) actionPlan.model.ts(25, 5): 'observations' is declared here.

This is my Prisma schema file (removed some extraneous fields)

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 model

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

    @Field()
    department: string

    @Field()
    code: string

    @Field()
    oute: string

    @Field()
    hypothesis: string

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

However even when I run prisma generate successfully, it doesn't add the 'observations' to the Action Plan. Am I missing something? Any help would be great. I also attached screenshot of generated types at node_modules/.prisma/client/index.d.ts

.

Share Improve this question asked Feb 3, 2022 at 20:03 unicorn_surpriseunicorn_surprise 1,1096 gold badges27 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Prisma doesn't include relationships in generated types because not all queries include them. Prisma queries do return relationships according options used, but in the generated code this is done through plex TypeScript.

But always you can define types with relationships included manually or using a bination of ReturnType<T> and Awaited<T> utilities.

Example defining manually:

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

Example using Awaited and ReturnType:

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

Where ActionPlanService is:

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

Note: Awaited<T> utility is available since TypeScript 4.5

Since you have a relation between those 2, your code should provide an include argument, so that your @Resolver & @Query can populate that entity in the response.
Also, I don't know if it is inside your ActionPlanService the prisma connection (probably yes, since you get this error).
So, for example, your first query should point to the this ActionPlanService function:

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

or something similar, since I don't know how you connect with prisma in that service

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论