Wire relationship instead of create?

prisma

#1

here is my schema

type Product{
  uuid: String!
  brand: String
}

type AskRecord {
  product: Product!
  userId: String
}

Am I able to create new “AskRecord” using existing “Product” instead of create new?


#2

Yes you can connect related nodes like this.

mutation {
  createAskrecord(data: {
    userId: '1'
    product: {
      connect: {
        id: "<insert productid here>"
      }
    }
  }) {
   userId
  }
}

Reference: Docs


#3

@404_Not_Found, to be able to connect to an existing node, that type needs at least one unique field. In your case uuid looks like a good candidate - and you can always fallback to the special id field anyway.

You can update your uuid field to be unique by adding the @unique directive:

type Product{
  uuid: String! @unique
  brand: String
}

type AskRecord {
  product: Product!
  userId: String
}

Now you should be able to connect to an existing product like so:

mutation {
  createAskrecord(data: {
    userId: '1'
    product: {
      connect: {
        uuid: "some-uuid"
      }
    }
  }) {
   userId
  }
}

You can have a look at @Simskii’s post. That shows the mutation if you choose to use id instead :slight_smile: