Search code examples
prismaprisma-graphql

Filter by id of a relation


I want to filter in prisma for a relation id and get the same entity, not the related one back. Simple example:

type User {
  firstName: String!
  lastName: String!

  isOwner: [Meta!]! @relation(link: INLINE, name: "User_Meta_Owner")

  id: ID! @id
}

type Meta {
  owner: User! @relation(link: INLINE, name: "User_Meta_Owner")
  area: Area! @relation(link: INLINE, name: "Meta_Area")

  id: ID! @id
}

type Area {
  id: ID! @id
  name: String!

  meta: Meta! @relation(link: INLINE, name: "Meta_Area")
}

In this case i want all Meta entities which have an owner with the id userID and an Area with the id areaID.

What is possible:

ctx.db.user({ id: 'userID' }).isOwner()

This gets all Meta without a filter for the area.

What i want is something like:

ctx.db.user({ id: 'userID' }).isOwner({ where: { area: 'areaID' })
ctx.db.metas({ where: [{ owner: 'userID' }, { area: 'areaID' }] })

Since the property area is only a relation, prisma doesnt give me the opportunity to filter or even get the ids.

ie:

await ctx.db.user({ id: 'userID' }).isOwner()

will result in an array of objects like:

[{
  id: '...'
}]

My question is, is there any way to get my wanted result without deleting the relation and store simple strings?


Solution

  • I got an answer from prisma member to get this currently only working with model like this:

    type Meta {
      owner: User! @relation(link: INLINE, name: "User_Meta_Owner")
      area: Area! @relation(link: INLINE, name: "Meta_Area")
      ownerId: ID! @db(name: "User")
      areaId: ID! @db(name:"Area")
      id: ID! @id
    }
    

    So the id's needs to be exposed as scalars, so you can filter those.