Deeply Nested Create Mutations

prisma

#1

Hi All,

I’m wondering if it’s possible to have deeply nested create mutations?

My query is along the lines of:

mutation {
  createProgramme(
    title: "Programme One"
    description: "This is a description of the programme, ain't that amazing!"
    active: true
    weeks: {
      create: [
        {
          number: 1
          title: "Week One"
          description: "This is the first week of the programme."
          classes: {
            create: [
              {
                class: {
                  connect: {
                    id: "cjeeu4m2r00100124quldgnji"
                  }
                }
              },
              {
                class: {
                  connect: {
                    id: "cjeeu4m2r00100124quldgnji"
                  }
                }
              }
            ],
          }
        },
...

Schema:

type Programme {
  id: ID! @unique
  weeks: [ProgrammeWeek!]! @relation(name: "ProgrammeWeek")
  classes: [ProgrammeClass!]! @relation(name: "ClassesInProgramme")
  users: [ProgrammeUser!]!

  title: String! @unique
  lowerTitle: String! @unique
  description: String
  active: Boolean! @default(value: "true")
}

type ProgrammeWeek {
  id: ID! @unique
  classes: [ProgrammeClass!]!
  programme: Programme! @relation(name: "ProgrammeWeek")

  title: String
  description: String
  number: Int!
}

type ProgrammeClass {
  id: ID! @unique
  class: Class!
  week: [ProgrammeWeek!]!
  programme: [Programme!]! @relation(name: "ClassesInProgramme")
  completedBy: [ProgrammeUser!]! @relation(name: "CompletedBy")
  activeOn: [ProgrammeUser!]! @relation(name: "ActiveOn")
  active: Boolean! @default(value: "true")
}

type ProgrammeUser {
  id: ID! @unique
  user: User!
  activeProgramme: Programme
  activeWeek: ProgrammeWeek
  activeClass: ProgrammeClass @relation(name: "ActiveOn")
  completedClasses: [ProgrammeClass!]! @relation(name: "CompletedBy")
}

When I run the mutation I get the newly created Programme, ProgrammeWeek's and ProgrammeClass's, however if i run the following query:

query {
  programmes {
    title
    id
    description
    classes {
      id
    }
    weeks {
      id
      classes {
        class {
          id
        }
      }
    }
  }
}

I get the following the result:

{
  "data": {
    "programmes": [
      {
        "title": "Programme One",
        "id": "cjelprx7k02mp01248a6kbccc",
        "description": "This is a description of the programme, ain't that amazing!",
        "classes": [],
        "weeks": [
          {
            "id": "cjelprx7m02mq01240cqq2k82",
            "classes": [
              {
                "class": {
                  "id": "cjeeu4m2r00100124quldgnji"
                }
              },
              {
                "class": {
                  "id": "cjeeu4m2r00100124quldgnji"
                }
              }
            ]
          },
          {
            "id": "cjelprx7o02mt0124mfchsr4b",
            "classes": [
              {
                "class": {
                  "id": "cjeeu4m2r00100124quldgnji"
                }
              },
              {
                "class": {
                  "id": "cjeeu4m2r00100124quldgnji"
                }
              }
            ]
          }
        ]
      }
    ]
  }
}

As you can see the classes exits inside of the ‘Week’ but not inside of the ‘Programme’ ("classes": [],) is this expected behaviour? If so, how do I make it so the the ProgrammeClasses get added to both the ProgrammeWeek and Programme?

Thanks!


#2

This is an excellent question @JamieHalvorson!

The issue you are running into is that you want two created nodes that are not directly linked by the structure of the nested mutation to be connected.

Your nested mutation creates the following relations:

Programme -> ProgrammeWeek -> ProgrammeClass -> Class

In addition you also want to create the following relation:

Programme -> ProgrammeClass

Currently there is no way to do this with a single mutation in Prisma

I think this is a very valuable feature. Would you mind creating a feature request on Github and link to this conversation?


#3

Thanks @sorenbs, that clears things up!

Feature request created, ticket: https://github.com/graphcool/prisma/issues/2069