0

I am trying to pass a product object to an invoice table so in my case I guess it is a one-to-many where one invoice has multiple products. I tried to do something like this:

the schema:

    type Product @model @key(name: "productIDIndex", fields: ["productID"]) {
    id: String!
    name: String!
    handle: String
    description: String
    categories: [String]
    tags: [String]
    images: [Image]
    price: Float
    sellingPrice: Float
    profit: Float
    priceTaxExcl: Int
    priceTaxIncl: Int
    taxRate: Int
    comparedPrice: Int
    quantity: Int
    sku: String
    width: String
    height: String
    depth: String
    weight: String
    extraShippingFee: Int
    active: Boolean
    boughtDate: String
    soldDate: String
    deliveryPrice: Float
    deliveryMwstPrice: Float
    ebayInvoiceNo: Int
    ebayArticleNo: String
    differencePrice: Float
    differenceMwstPrice: Float
    total: Float
    totalMwst: Float
    productID: ID!
}

type Image {
    default: Boolean
    id: String
    url: String
    type: String
}

type Invoice @model {
    id: ID!
    products: [Product] @connection(keyName: "productIDIndex", fields:["id"])
    date: String
}

where the generated schema for the Invoice I have only the id and date as inputs but I need to write the product object as well.

generated schema for CreateInvoiceInput:

input CreateInvoiceInput {
  id: ID
  date: String
}

I tried to pass the product as an object in the request but I got an error on the browser that product is not defined in the input of invoice.

any idea of how can this be solved?

Hassan Chmsdn
  • 172
  • 1
  • 14

1 Answers1

0

For a one-to-many, you want to create the parent object first, then create the child record with the parent record's id set.

So in your case, create the Product record and grab the id from the mutation's response. Then, save the Invoice record with the ProductId property set.

Here are some examples related to this use case: https://docs.amplify.aws/cli/graphql-transformer/connection#has-many

Alex
  • 952
  • 7
  • 12
  • 1
    yes I have checked the docs before, my problem that I need to pass multiple ids or products to one invoice, in case of connection when assign [ID] for example am getting an error that it should scalar or enum only – Hassan Chmsdn Jun 23 '21 at 17:06