Overide entire relation field with Prisma?

Viewed 1132

With Prisma is it possible to completely overwrite a relation rather than connecting and disconnecting individual nodes?

Say I have a user with a groups relation to groups 1 and 2:

user: {
    id: "abcd"
    groups: [
        {id: 1},
        {id: 2}
    ]
}

If I want to make this user only connected to group 3:

user: {
    id: "abcd"
    groups: [
        {id: 3}
    ]
}

Do I have to do this?:

mutation {
  updateUser(
    where: { id: "abcd" }
    data: {
        groups: {
            disconnect: {
                id: "1"
                id: "2"
            }
            connect: {
                id: "3"
            }
        }
    }
  ) {
    id
  }
}

Or is there some way of overwriting the entire relation:

mutation {
  updateUser(
    where: { id: "abcd" }
    data: {
        groups: [{id:3}]
    }
  ) {
    id
    name
  }
}
2 Answers

I believe what you're looking for is an API similar to the one of scalar lists:

mutation {
  createUser(data: {
    scores: { set: [1, 2, 3] }
    friends: { set: ["Sarah", "Jane"] }
    throws: { set: [false, false] }
  }) {
    id
  }
}

This is currently not possible with Prisma but there already is an open feature request for that functionality, please leave your if you're interested in that feature.

You can use set replace connect

data: {
    groups: {
        set: {
            id: "3"
        }
    }
}
Related