I'm trying to update a document in the "Inventories" collection. I'm passing the id of the inventory and the newItems that should be saved as the new inventory.
When I try to run a test, such as:
mutation{
updateInventory(id:"5d72e794a00da51083c56615",newItems:[{name:"Test", quantity:3},{name:"Test2",quantity:3}]){
items{
name
}
}
}
I get the error:
"{
"errors": [
{
"message": "Inventory validation failed: items: Cast to Array failed for value \"[ [Object: null prototype] { name: 'Test', quantity: 3 },\n [Object: null prototype] { name: 'Test2', quantity: 3 } ]\" at path \"items\"","
Where might I be going wrong?
Code snippets:
Resolver -
updateInventory: async (parent, args) => {
let selectedInventory = await Inventory.findById(args.id)
selectedInventory.items = [...args.newItems]
await selectedInventory.save()
return selectedInventory
}
Relevant TypeDefs -
type Item{
name: String!
quantity: Int!
}
input ItemInput{
name: String!
quantity: Int!
}
type Inventory{
items: [Item]!
}
type Mutation{
updateInventory(id: String!, newItems: [ItemInput]!): Inventory!
}
Edit
I am aware that there is another question that mentions "Cast to Array" - The selected answer for that question deals with needing to not use "type" as a key. I am not using "type" as a key.