paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
687 stars 55 forks source link

Question: Transform output of M:M relationship #215

Closed rryter closed 3 years ago

rryter commented 3 years ago

Hello :-)

I'm currently trying to achieve the following:

schema.prisma

model Post {
  tags Tag[]
}

model Tag {
  name String @unique
  posts Post[]
}

This generates the correct DB tables and queries (pal g).

But now I would like to be able receive tags as an array of strings, and not objects. I would like to be able to query it like this:

Post{
      tags
    }

I would like to avoid the following:

Post{
   tags{
     name
   }
}

Is this even possible? I was thinking I should just be able to add a .map in the resolver but it does not seem to be as simple as that.

Thank you very much.

AhmedElywa commented 3 years ago

sorry you can't because to query tag name from the DB, you must ask it in your query like

Post{
   tags{
     name
   }
}

so my Select plugin can convert to

prisma.post.findUnique({
  select: {
    tags: {
      select: {
        name: true,
      },
    },
  },
})

I see that you take the query result and make your map to return the name in your frontend

rryter commented 3 years ago

Thank you very much for the fast feedback. 👍