5

I am trying to create an object with a relationship.

I am using the auto generated amplify mutations

When I create an object without the relationship the operation succeeds. When I create an object with the relationship the operation fails.

The error message I get is

"The variables input contains a field name 'customer' that is not defined for input object type 'CreateCreditcardInput' "

The auto generated mutation is below.

export const createCreditcard = `mutation CreateCreditcard($input: CreateCreditcardInput!) {
  createCreditcard(input: $input) {
    id
    number
    expiration
    customer {
      id
      firstName
      lastName
      phone
      address1
      address2
      city
      state
      postcode
      email
      creditcards {
        nextToken
      }
    }
    payment {
      id
      paymentType
      creditcard {
        id
        number
        expiration
      }
      orderAmount
      order {
        id
        date
        orderStatus
      }
    }
  }
}
`;
Udesh
  • 1,919
  • 3
  • 20
  • 26

1 Answers1

2

The solution was to change the property that contained the relationship ID from a nested object to a string.

The original that produced the error was

{id: "", number: 1212112, expiration: "12/20", customer: {id:"81d86584-e031-41db-9c20-e6d3c5b005a6"}}

The correction that now works is

{id: "", number: 1212112, expiration: "12/20", creditcardCustomerId: "81d86584-e031-41db-9c20-e6d3c5b005a6"}
Udesh
  • 1,919
  • 3
  • 20
  • 26
  • 1
    Such basic functionality for submitting an update. It seems `aws-amplify` should handle that for us, right? I gotta believe there is a way to transform the model as I'm using the same one that came from the query I'm now updating. – Corey Gwin Dec 07 '19 at 21:17
  • The best idea I can come up with is to write a function that takes a nested object then flattens it to comply with what aws-amplify wants. The nested object's property tree would accumulate into a string. So in this exmple creditcard.customer.id => creditcardCustomerId – Udesh Dec 07 '19 at 22:24