【问题标题】:findUnique query returns null for array fieldsfindUnique 查询为数组字段返回 null
【发布时间】:2021-04-08 21:20:22
【问题描述】:

我阅读了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
    }
  }
}

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

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

【问题讨论】:

    标签: prisma prisma-graphql prisma2 graphql-playground


    【解决方案1】:

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

    改变

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

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

    【讨论】:

    • 你不知道我在这玩了多少小时。谢谢!!
    猜你喜欢
    • 2020-10-16
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    相关资源
    最近更新 更多