Create bidirectional connection in one transaction

prisma

#1

Is it possible to connect two nodes on different fields in one transaction? The use case is a typical follower / following scenario. Consider the following model:

type User {
  followers: [User!]! @relation(name: "UserFollowers")
  follows: [User!]! @relation(name: "UserFollows")
}

Based on that model, I want to create a mutation follow that connects two users with each other: User A follows User B. So UserB.follows should be connected to UserA and UserA.followers should be connected to UserB.

In the best case, I want to execute that in one transaction a.k.a. one nested mutation. Something à la:

mutation {
  updateUser(data: {
    follows: {
      connect: {
        id:"UserA.id",
      },
      update: {
        data: {
          followers: {
            connect:{id:"UserB.id"}
          }
        }
        where: {id:"UserA.id"}
      }
    }
  }, where:{id:"UserB.id"}  {
    id
  }
}

This approach doesn’t work obviously. Is that even possible? Any advice is highly appreciated :slight_smile:


#2

There is no support for transactions across multiple mutations. Here is some discussion about this feature request.

From what I can see, the mutation you propose should already work minus the transactional guarantee. Is that not the case?


#3

Thanks @nilan for getting back to this topic. No, it runs :slight_smile: – Thanks for pointing me to this discussion. Will mark this here as resolved.


#4

Great! It should be noted that a single nested mutation indeed runs in one transaction.