即使数据存在,“用户”类型的字段也会为查询返回“空”

时间:2019-04-20 13:08:20

标签: graphql schema prisma prisma-graphql

我在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

1 个答案:

答案 0 :(得分:0)

解决了。我必须在profilePicture下为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()
      }
}