Include "one-to-*" relation's id


#1

I understand that when fetching a node using prisma-client relations’ data is not included because client would need to do recursive queries, but often you don’t really want the relation data, you just want to know the id which in case of “one-to-*” relations is stored along the other node scalars. Example:

Schema:

type User {
  id: ID!
  name: String!
  team: Team
}

type Team {
  id: ID!
  name: String!
  users: [User!]!
}

Code:

const user = await ctx.prisma.user({ id });

// I just want to know  what team is the user in without doing an extra query
console.log(user.team); 

Is there a way how to achieve this without running an extra DB query?


#2

You can use the follow code in order to get it in one go:

const user = await ctx.prisma.user({ id }).$fragment(`
  fragment EnsureTeam on User {
      id
      name
      team {
           id
           name
        }
    }
`)

#3

Thank you, I am aware of this API, but my question was slightly different. I want to get only the team.id and I want to make sure only 1 DB request is made (since checking ID of a relation is done very often).


#4

this only results in 1 db as prisma server has internal batching and dataloader.


#5

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.