Evanss
Evanss

Reputation: 23153

Overide entire relation field with Prisma?

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

Upvotes: 6

Views: 2637

Answers (2)

XiaoQiang Li
XiaoQiang Li

Reputation: 29

You can use set replace connect

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

Upvotes: 3

nburk
nburk

Reputation: 22751

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.

Upvotes: 2

Related Questions