Having issues with returning data for relations wi...
# orm-help
e
Having issues with returning data for relations with Mongo Connector My datamodel looks like this:
Copy code
// datamodel.prisma

type User {
  id: ID! @id
  firstName: String!
  lastName: String!
  username: String @unique
  email: String! @unique
  password: String!
  posts: [Post!]! @relation(link: INLINE)
  following: [User!]! @relation(name: "Following", link: INLINE)
  followers: [User!]! @relation(name: "Followers", link: INLINE)
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt
}

type Post {
  id: ID! @id
  caption: String
  image: String
  published: Boolean @default(value: false)
  author: User
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt
}
My
user
resolver looks like this:
Copy code
user (_, { id }, ctx, info) {
    return ctx.prisma.user({ id }, info);
  },
If i try to query for a userโ€™s posts:
Copy code
{
  user(id: "5c7c04e824aa9a0007495114") {
    firstName
    posts {
      id
    }
  }
}
I get
Cannot return null for non-nullable field User.posts.
and not sure why? I can see the data is there in the
Posts
collection via Mongo Compass but dont seem to be able to query it.
b
@ezeikel insert
@relation(link: INLINE)
after
author: User
๐Ÿ˜„. Actually, you don't have to use
@relation
in this situation, but if you use it, you have to declare it at both
User
and
Post
which version of prisma are you using? because its syntax is a little bit different ๐Ÿ˜„
e
Adding
@relation(link: INLINE)
to
author: User
doesn't work either. Only one side must have the relation apparently
Also, using mongodb with prisma so datamodel syntax differs slightly in places to normal I think
h
@ezeikel You will require to implement a type resolver this to resolve each user's posts
e
Yup thanks
Literally just implemented it and now working
h
If you use nexus-prisma, you can even avoid this. See https://github.com/prisma/nexus-prisma
e
Copy code
resolvers: {
      Mutation,
      Query,
      User: {
        posts(parent) {
          return prisma.user({ id: parent.id }).posts()
        }
      },
      Post: {
        author(parent) {
          return <http://prisma.post|prisma.post>({ id: parent.id }).author()
        }
      }
    }
h
Yes, looks good ๐Ÿ™‚
๐Ÿ‘ 1
e
Ooo ok ill have a look at this