0

我的类型中有一个profilePicture字段User被返回为 null,即使我可以看到数据库中有数据。我有以下设置:

// datamodel.prisma

enum ContentType {
  IMAGE
  VIDEO
}

type Content @embedded {
  type: ContentType! @default(value: IMAGE)
  url: String
  publicId: String
}

type User {
  id: ID! @id
  name: String
  username: String! @unique
  profilePicture: Content
  website: String
  bio: String
  email: String! @unique
  phoneNumber: Int
  gender: Gender! @default(value: NOTSPECIFIED)
  following: [User!]! @relation(name: "Following", link: INLINE)
  followers: [User!]! @relation(name: "Followers", link: INLINE)
  likes: [Like!]! @relation(name: "UserLikes")
  comments: [Comment!]! @relation(name: "UserComments")
  password: String!
  resetToken: String
  resetTokenExpiry: String
  posts: [Post!]! @relation(name: "Posts")
  verified: Boolean! @default(value: false)
  permissions: [Permission!]! @default(value: USER)
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt
}

// schema.graphql

type User {
  id: ID!
  name: String!
  username: String!
  profilePicture: Content
  website: String
  bio: String
  email: String!
  phoneNumber: Int
  gender: Gender!
  following: [User!]!
  followers: [User!]!
  verified: Boolean
  posts: [Post!]!
  likes: [Like!]!
  comments: [Comment!]!
  permissions: [Permission!]!
}

就像我说的数据库中有数据但是当我在 Playground 中运行以下查询时,我得到null

// query
{
  user(id: "5c8e5fb424aa9a000767c6c0") {
    profilePicture {
      url
    }
  }
}
// response
{
  "data": {
    "user": {
      "profilePicture": null
    }
  }
}

知道为什么吗?

ctx.prisma.user(({ id }), info);即使profilePicture该字段存在于generated/prisma.graphql

4

1 回答 1

0

修复。我必须为profilePictureunder添加一个字段解析器User。我以前为相关字段做过这个postscomments我认为这是因为profilePicture指向@embedded Content类型,所以也是一个相关字段。

{
  User: {
    posts: parent => prisma.user({ id: parent.id }).posts(),
    following: parent => prisma.user({ id: parent.id }).following(),
    followers: parent => prisma.user({ id: parent.id }).followers(),
    likes: parent => prisma.user({ id: parent.id }).likes(),
    comments: parent => prisma.user({ id: parent.id }).comments(),
    profilePicture: parent => prisma.user({ id: parent.id }).profilePicture()
  }
}

于 2019-04-20T23:55:32.557 回答