findUnique 查询为数组字段返回 null

问题描述

我阅读了 Prisma Relations 文档,它修复了我的 findMany 查询,该查询能够返回有效数据,但我得到的结果与 findUnique 不一致。

架构

model User {
  id       Int       @id @default(autoincrement())
  fname    String
  lname    String
  email    String
  password String
  vehicles Vehicle[]
}

model Vehicle {
  id      Int    @id @default(autoincrement())
  vin     String @unique
  model   String
  make    String
  drivers User[]
}

类型定义

const typeDefs = gql'
    type User {
      id: ID!
      fname: String
      lname: String
      email: String
      password: String
      vehicles: [Vehicle]
    }

    type Vehicle {
      id: ID!
      vin: String
      model: String
      make: String
      drivers: [User]
    }

    type Mutation {
      post(id: ID!,fname: String!,lname: String!): User
    }

    type Query {
      users: [User]
      user(id: ID!): User
      vehicles: [Vehicle]
      vehicle(vin: String): Vehicle
    }
'

这个有效

users: async (_,__,context) => {
        return context.prisma.user.findMany({
          include: { vehicles: true}
        })
      },

然而,出于某种原因,findUnique 版本不会解析“车辆”的数组字段

这个不行

user: async (_,args,context) => {
     const id = +args.id
     return context.prisma.user.findUnique({ where: {id} },include: { vehicles: true}
     )
},

这是它返回的内容

{
  "data": {
    "user": {
      "id": "1","fname": "Jess","lname": "Potato","vehicles": null
    }
  }
}

我正在阅读有关片段的信息,并试图查找有关 graphql 解析器的文档,但我没有找到任何可以解决此问题的相关内容

任何见解将不胜感激!谢谢!

解决方法

您需要修正传递给 findUnique 的参数。注意 {} 的排列。

改变

return context.prisma.user.findUnique({ where: { id } },//                                                  ^
  include: { vehicles: true}
)

return context.prisma.user.findUnique({
  where: { id },include: { vehicles: true }
})