使用 prisma graphql apollo 检查记录是否存在

check if record exists using prisma graphql apollo

尝试使用 Prisma 检查 Postgres 中的 table 中是否存在记录,但似乎我只能查询 id 字段,但不能查询 name 和 [=15 等任何其他字段=],这给出了编译器错误

型号schema.prisma

model place {
  id             Int              @id @default(dbgenerated("nextval('place_id_seq'::regclass)"))
  name           String
  location       String @unique
}

生成类型

export type Place = {
  __typename?: 'Place';
  name?: Maybe<Scalars['String']>;
  location?: Maybe<Scalars['String']>;

};

查询解析器

let findPlace = await prisma.place.findUnique(
        {
          where: {
            name: "abc"
          }
        }
)

错误

Type '{ name: string; }' is not assignable to type 'placeWhereUniqueInput'.
  Object literal may only specify known properties, and 'name' does not exist in type 'placeWhereUniqueInput'.ts(2322)
index.d.ts(1361, 5): The expected type comes from property 'where' which is declared here on type '{ select?: placeSelect | null | undefined; include?: placeInclude | null | undefined; rejectOnNotFound?: RejectOnNotFound | undefined; where: placeWhereUniqueInput; }'

这里缺少什么来完成这项工作?

Prisma 不接受 findUnique 条件仅包含非唯一字段(在本例中为名称)的查询。如果只需要查找符合条件的地点记录是否存在,可以使用count API.

let placeCount = await prisma.place.count(
        {
          where: {
            name: "abc"
          }
        }
)
// placeCount == 0 implies does not exist