0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
av0000
  • 1,917
  • 6
  • 31
  • 51
  • 1
    This is an issue with your mongoose model and not your GraphQL schema. – Daniel Rearden Sep 07 '19 at 03:46
  • Awesome, thanks for pointing me in the right direction! Do you have any additional ideas of what might be wrong with my mongoose model? – av0000 Sep 07 '19 at 03:46
  • See the linked dupe. If the provided answers don't help, feel free to edit your question with additional details, like your mongoose model, and vote to reopen the question. – Daniel Rearden Sep 07 '19 at 03:49
  • The linked dupe's answer is for when someone used "type" as a key... Just because it has some of the same words doesn't mean it is the same issue... – av0000 Sep 07 '19 at 03:50

0 Answers0