简体   繁体   中英

Field on `User` type returns `null` for Query even though data exists

I have a profilePicture field on my User type that is being returned as null even though I can see the data is there in the database. I have the following setup:

// 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!]!
}

Like I said there is data in the database but when I run the below query in Playground I get null :

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

Any idea why?

ctx.prisma.user(({ id }), info); doesn't return profilePicture even though the field exists in generated/prisma.graphql

Fixed it. I had to add a field resolver for profilePicture under User . I've done this before for related fields like posts and comments and I think It's because profilePicture points to the @embedded Content type so is sort of a related field as well.

{
  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()
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM