简体   繁体   中英

PRISMA - 'where' with deeply nested relation

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?

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});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM