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

prisma

#1

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 prisma.post({id: postId}).comments()


#2

@mahdi221b

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 prisma.post({id: postId}) is not possible

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

#3

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 {
            user(where:{
                id:"${userId}"
            }){
                name
                posts{
                    title
                    comments{
                        id
                        text
                    }
                }
            }
        }
        `;
    const res = await prisma.$graphql(query)

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

    return post.comments
}

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


#4

Which version are you using, it does indeed work:


#5

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.


#6

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

prisma.yml:

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

test.js:

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

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

    const result2 = await prisma.users();
    console.log(JSON.stringify(result));
    console.log(JSON.stringify(result2));
}

main();

Output:

> node test.js   
{"id":"5cc010cb9bd1160007d3fb3c","name":"test"}
[{"id":"5cc010cb9bd1160007d3fb3c","name":"test"}]

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.


#7

Fyi: the related bug ticket can be found at https://github.com/prisma/prisma/issues/4443