How to connect nodes in a relation to another node that you create at the same time


#1

Consider the structure below

mutation createBusiness {
  createBusiness(
    data: {
      ...
      staff: { create: [{ name: "Petros", email: "example@gmail.com" }] }
      services: {
        create: [
          {
            name: "Hair Wash"
            duration: 20
            staff: { ... }
          }
        ]
      }
    }
  ) {
    id
  }
}

So basically a business offers services and has staff . However, the services are run by specific staff members or all of them. So the problem is that at the point of creating the business, the staff members are created and the services need to be created as well.

However I am struggling to connect the services staff members to the staff created at the same time.

Any ideas if at all possible?


#2

Hi,

The best approach for your current situation that I can currently think of is to create business and service in separate calls.

That way will allow you to connect the specific staff members to the service using their ids that are being returned to you in the first call.

Let me know if this approach will work for you :slight_smile:


#3

Hey @pitops,

I think @pantharshit00’s suggestion is probably the way to go. Would you mind posting your full datamodel (or at least the relevant parts with Service, Staff and Business)?


#4

Hey @pantharshit00 so yeah thats what I actually ended up doing. My question is, isn’t that suboptimal? In terms of performance? Or does prisma underneath behave the same way if i were to do it on a single mutation?

@nikolas sure thing - I will post only the relevant parts

type Business {
  id: ID! @unique
  name: String!
  ...
  services: [Service!]!
  staff: [Staff!]!
}

type Staff {
  id: ID! @unique
  ...
  business: Business!
}

type Service {
  id: ID! @unique
  ...
  staff: [Staff!]!
}


#5

There should not be any significant impact in performance Prisma batches up things for you. You should be good here, don’t get too worried :slightly_smiling_face:.