How to share common data between types?

prisma

#1

So, I have defined a 1 to many relationship between two types which both share common information (title, description, mainDescription, price):

interface ProductDetails {
  id: ID! @unique
  title: String!
  description: String!
  mainDescription: String!
  image: String
  largeImage: String
  price: Int!
  quantity: Int!
  user: User!
  cartitems: [CartItem!]!
  size: Size!
  color: Color!
}

type Item implements ProductDetails {
  id: ID! @unique
  title: String!
  description: String!
  mainDescription: String!
  image: String
  largeImage: String
  price: Int!
  quantity: Int! @default(value: "1")
  user: User!
  cartitems: [CartItem!]! @relation(name: "CartItemsOfItem" onDelete: CASCADE)
  itemvariants: [ItemVariants!]! @relation(name: "ItemVariantsOfItem" onDelete: CASCADE)
  size: Size!
  color: Color!
}

type ItemVariants implements ProductDetails {
  id: ID! @unique
  title: String!
  description: String!
  mainDescription: String!
  image: String
  largeImage: String
  price: Int!
  quantity: Int! @default(value: "1")
  user: User!
  cartitems: [CartItem!]! @relation(name: "CartItemsOfItemVariants" onDelete: CASCADE)
  item: Item! @relation(name: "ItemVariantsOfItem") #relationship to Item. 
  size: Size!
  color: Color!
}

What I in effect want to do in my mutation would be equivalent to as follows, is this or anything similar at all possible?:

mutation createvariant_Mutation(
    $title: String!,
    $description: String!,
    $mainDescription: String!,
    $price: Int!,
    $image: String,
    $largeImage: String,
    $quantity: Int!,
    $userId: ID!,
    $color: String!,
    $size: String!
){
  createItemVariants(
    data: {
      user: {
        connect: {
          id: $userId
        }
      }
      ... on Item {
         title
      }
      ... on Item {
         description
      }
      ...
      quantity: $quantity
      color: {
        connect: { name: $color }
      }
      size: {
        connect: { name: $size }
      }
    }
  ){
    id
    quantity
    image
    largeImage
    color {
      name
      label
    }
    size {
      name
      label
    }
  }
}

What I’m trying to avoid having to do is item: { connect: {id: "XYZ"} } followed by some front end code manipulation to pull the corresponding Item data into the ItemVariants fields.


#2

Looks like this will not be possible as you can’t really do inline fragments in inputs in graphql.

You will require to connect the item if it is a relation, that is how we link between them otherwise there will be no way we can link those in the underlying database. Even if ... on Item is possible we will not be able to reference which item you are trying to reference. But I think you can share data if you are willing to add additional type but then you will require to connect the data


#3

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.