How do I query an @embedded type using Prisma Client?



so i’m using mongodb and i have the following datamodel in Prisma :

type User{
  id: ID! @id
  name: String!
  posts: [Post]

type Post @embedded {
  id: ID! @id
  text: String! 
  comments: [Comment]

type Comment @embedded{
  id: ID! @id
  text: String

now i want to get all the comments of a given post, how can i do that?
since i’m using an embedded type instead of a link relation i can’t just do{id: postId}).comments()



I think we automatically fetch the embedded types. So you can access them in the parent object. Also, you can’t directly query an embedded object so even{id: postId}) is not possible

const user  = await prisma.user({id:userId}) ;
// {
//   "id": "5cbee7cee03dd80008e26f8c",
//   "name": "test",
//   "posts": [
//     {
//       "id": "5cbee7cee03dd80008e26f8d",
//       "text": "test",
//       "comments": [
//         {
//           "id": "5cbee7cee03dd80008e26f8e",
//           "text": "test"
//         }
//       ]
//     }
//   ]
// }


hi @pantharshit00 , prisma.user({id:userId}) doesn’t return the embedded posts and comments for that user, it just return the scalar types (id, name) , in order to get the posts of that user you have to implement the resolver function posts which is fairly easy, you just have to return prisma.user({id:userId}).posts() , now my problem is implementing the resolver function comments in order to get the comments of a certain post. i didn’t find any function in prisma-client that can do that for me, so i just had to do some work around : for that i try to get all the comments of every single post using prisma.$query(myquery) and then filter only the comments of the desired post, so here is what my resolver function comments looks like :

async function comments(userId, postId){
    const query = `
        query {
    const res = await prisma.$graphql(query)

    //finding the post that i want to show it's comments
    post = res.user.posts.find((post) => == postId)
    return null

    return post.comments

i wish there was a better way to do that in pirsma-client though.


Which version are you using, it does indeed work:


indeed, it’s working, i’m not sure what i was thinking, thank you.
the bad thing though is that if you want to grab a single post with all it’s comments you would have to query every single post with every single comment and then apply the filtering, which i’m afraid will reduce the performance of the app.


Hi @pantharshit00

Sorry for hijacking this discussion, however I have the same problem. I’m using the latest Prisma versions so maybe you can verify again if this is not a bug in a new published version?

Prisma CLI version: prisma/1.31.1 (darwin-x64) node-v8.14.1
Prisma server version: 1.31.0
Connector: MongoDB


endpoint: ${env:PRISMA_ENDPOINT}
datamodel: prisma/datamodel.prisma
  - generator: javascript-client
    output: ./dist/


const {prisma} = require('./dist');

async function main() {
    const result = await prisma.createUser({
        name: "test",
        posts: {
            create: [
                    text: "hello"

    const result2 = await prisma.users();



> node test.js   

When checking the collection in MongoDB, it shows that the post has been created:

Thank you for your support and let me know if you need additional information.


Fyi: the related bug ticket can be found at


This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.