Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PRISMA - 'where' with deeply nested relation

Tags:

prisma

I'm trying to use 'where' on the deeply nested relation (we can paraphrase 'on relation from relation').

Unfortunately as for 2022-06 in Prisma documentation there is only info about using 'include' with deeply nested relations, nothing about deeply nested relations with 'where'.

Simplified types in Schema.graphql:

type Entity {
  id: ID!
  company: Company
}

type Company {
  id: ID!
  entity: Entity!
  status: CompanyStatus!
}

type CompanyStatus {
  id: ID!
  companies: [Company]
}

I want to query Entities and get only these ones, that have companies, that have exact status (let's say with status that has an ID: 1).

I've tried in multiple ways:

1)

function entities(parent, args, context, info) {
  const where = {
    company: { status: {id: 1} }
  }
  return context.prisma.entities.findMany({where});
}

Returns:

Unknown arg `status` in where.company.status for type CompanyRelationFilter. Did you mean `is`? Available args:\ntype CompanyRelationFilter {\n  is?: CompanyWhereInput | Null\n  isNot?: CompanyWhereInput | Null\n

It says to try with 'is', so here we have a try with 'is':

2)

function entities(parent, args, context, info) {
  const where = {
    company: { is: { status: {id: 1} } }
  }
  return context.prisma.entities.findMany({where});
}

Returns:

Unknown arg `is` in where.company.is for type CompanyWhereInput. Did you mean `id`? Available args:\ntype CompanyWhereInput {\n  AND?: CompanyWhereInput | List<CompanyWhereInput>\n  OR?: List<CompanyWhereInput>\n  NOT?: CompanyWhereInput | List<CompanyWhereInput>\n  id?: IntFilter | String\n  entity_id?: IntFilter | Int\n  entity?: EntityRelationFilter | EntityWhereInput\n  status?: CompanyStatusRelationFilter | CompanyStatusWhereInput\n  status_id?: IntFilter | Int

So the advice about using 'is' doesn't seem to work. It advises to use 'status' or 'status_id', which doesn't work (as in the first example).

Anyone knows whether use 'where' with deeply nested relations is possible with Prisma? If yes, how to perform it?

like image 689
Nick493 Avatar asked Mar 02 '26 18:03

Nick493


1 Answers

A friend on mine helped me to find a solution. You need to use double 'is':

function entities(parent, args, context, info) {
  const where = {
    company: { is: { status: { is {id: 1} } } }
  }
  return context.prisma.entities.findMany({where});
}
like image 120
Nick493 Avatar answered Mar 06 '26 02:03

Nick493