Best Practice for Nested Updates in Redwood JS

I have the following tables in my schema

model Order {
  id          String @id @default(uuid())
  location_id String
  job_id      String
  account_id  String

  order_type   OrderType
  scheduled_at DateTime  @default(now()) // timestamp for scheduled arrival
  service_time Int       @default(300) // time to service at order destination place (in seconds)

  job      JobRequest @relation(fields: [job_id], references: [id], onDelete: Cascade)
  location Location   @relation(fields: [location_id], references: [id])
  account  Account    @relation(fields: [account_id], references: [id])

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
}

model JobRequest {
  id          String  @id @default(uuid())
  driver_id   String?
  user_id     String
  dealer_id   String
  category_id Int
  account_id  String

  time_requested DateTime  @default(now())
  time_accepted  DateTime? // may not get accepted until later
  time_completed DateTime?
  notes          String?
  description    String?
  contact_name   String
  contact_phone  String

  status JobStatus @default(OPEN)

  category   JobCategory @relation(fields: [category_id], references: [id])
  dealership Dealership  @relation(fields: [dealer_id], references: [id])
  driver     Driver?     @relation(fields: [driver_id], references: [id])
  account    Account     @relation(fields: [account_id], references: [id])
  requester  User        @relation(fields: [user_id], references: [id])
  orders     Order[]

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
}

I have a form which updates the info of an existing job request and its associated orders. As of right now I am using the following mutation to edit 3 records anytime the user wants to change a job request, pickup order, and dropoff order.

const UPDATE_JOB_REQUEST_MUTATION = gql`
  mutation UpdateJobRequestMutation(
    $job_id: String!
    $dropoff_id: String!
    $pickup_id: String!
    $job: UpdateJobRequestInput!
    $dropoff: UpdateOrderInput!
    $pickup: UpdateOrderInput!
  ) {
    updateJobRequest(id: $job_id, input: $job) {
      id
      driver_id
      user_id
      dealer_id
      category_id
      account_id
      time_requested
      time_accepted
      time_completed
      notes
      description
      contact_name
      contact_phone
    }
    pickup: updateOrder(id: $pickup_id, input: $pickup) {
      id
      location_id
    }
    dropoff: updateOrder(id: $dropoff_id, input: $dropoff) {
      id
      location_id
    }
  }

My question is if there is a better way to do nested updates? Maybe using the prisma and editing the update queries in the api/src/services folder?

1 Like

Have you considered writing a custom service that takes in as an input just the ids and info needed to do that transaction?

Perhaps start with a Prisma nested update and work back from the data you need and then make a custom input sdl type to be used by that service.

Redwood makes the crud services for your models, but that doesn’t mean you are limited to just those — you can code up the ones that best fit your ui ad experience.

You can put these services and sdl in their own name so that you can still run generators for the crud models and not have them overwrite the new services and types.

2 Likes