Upsert in nested mutation

prisma

#1

Imagine the following scenario: A User might have a Profile (optional). So the data model looks like:

type User {
  id: ID! @unique
  role: Role! @default(value: "Member")
  banned: Boolean! @default(value: "false")
  email: String! @unique
  profile: Profile @relation(name: "UserProfile", onDelete: CASCADE)
}

type Profile {
  id: ID! @unique
  firstName: String
  lastName: String
  city: String
  country: String
  about: String
  website: String
  owner: User @relation(name: "UserProfile")
}

Now, I want to define a updateProfile mutation that upserts the profile in one transaction. Therefore, my first thought was to execute updateUser as a nested mutation and perform the profile upsert in there:

  const { profile: { id } } = await context.db.mutation.updateUser(
    {
      where: { id: context.user.id },
      data: {
        profile: {
          upsert: {
            where: {
              id: // What to place here?
            },
            create: {
              ...args.input
            },
            update: {
              ...args.input
            }
          }
        }
      }
    },
    `{profile{id}}`
  );

As you can see, the upsert awaits an id in the where attribute. This is the only identifier that I can use here and which is of course not available when the profile has not created before. My first thought was that due to the fact that the Profile also has a connection to the User I might be able to use the user id as an identifier in the where clause, but that is not possible – something like where: {owner: {id: context.user.id}}.

What would be the proper way here? Sure, I could check if the profile exists and execute a create or update manually, but that is … well … meh. :slight_smile:

Any idea is highly appreciated :slight_smile:


#2

I am not sure, but is this issue related: https://github.com/graphcool/prisma/issues/1947?


#3

Ya, it definitely is. Thanks :slight_smile: