I have nested schema in graphql and I am trying to use the update function created by amplify to update a nested part of the schema, for a specific Information ID. I have the ID of the table I want to update and want to update hasBooks section. Trying to use my current code I get two errors depending on what I try:
"The variables input contains a field name 'hasBooks' that is not defined for input object type 'UpdateInformationInput' "
and
"Variable 'input' has coerced Null value for NonNull type 'UpdateInformationInput!'"
schema:
type Information @model @searchable{
id: ID!
firstName: String!
lastName: String!
phoneNumber: Int
password: Int
timestamp:String!
hasBooks: [Books] @connection (name:"InformationBooks")
}
type Books @model {
id: ID!
firstName: String!
lastName: String!
title: String!
information: Information @connection
(name:"InformationBooks")
}
export const updateInformation = /* GraphQL */ `
mutation UpdateInformation(
$input: UpdateInformationInput!
$condition: ModelInformationConditionInput
) {
updateInformation(input: $input, condition: $condition) {
id
firstName
lastName
phoneNumber
password
timestamp
hasBooks {
items {
id
firstName
lastName
title
}
nextToken
}
}
}
code:
const initialState = {
firstName: '',
lastName: '',
title:''
}
const [formState, setFormState] = useState(initialState)
const [contactInfos, setContactInfos] = useState([])
async function addContactInfo() {
try {
const contactInfo = { ...formState }
setContactInfos([...contactInfos, contactInfo])
await API.graphql(graphqlOperation(updateInformation,
{input: { hasBooks: contactInfo, id:caseID}}))
} catch (err) {
console.log('error creating todo:', err)
}
}
needed a one to many @connection with a @key
type Information @model @searchable {
id: ID!
firstName: String!
lastName: String!
phoneNumber: Int
password: Int
timestamp:String!
books: [Books] @connection (keyName:"byInformation", fields:["id"])
}
type Books @model
@key (name:"byInformation", fields:["informationID"]) {
id: ID!
informationID:ID!
firstName: String!
lastName: String!
title: String!
}
and then create info by:
const initialState = {
firstName: '',
lastName: '',
title:'',
informationID: caseID
}
const [contactInfos, setContactInfos] = useState([])
async function addContactInfo() {
try {
const contactInfo = { ...formState }
setContactInfos([...contactInfos, contactInfo])
await API.graphql(graphqlOperation(createBooks, {input:
contactInfo}))
} catch (err) {
console.log('error creating todo:', err)
}
}