How can I create a mutation in GraphQL that contains a subdocument in MongoDB?

I have a collection in my MongoDB instance witch is basically a shopping list, with a subdocument for items to be bough. But for some reason, when I try to run a mutation to add a Shopping List, I got the error ID cannot represent a non-string and non-integer value.

This is my schema for GraphQL - I'm using Apollo Server with Node

type Item {
    name: String!
    quantity: String!
}

type ShoppingList {
    _id: ID!
    title: String!
    location: String
    dayTime: String
    items: [Item!]!
    owner: User!
}

input ItemInput {
    name: String!
    quantity: String!
}

input ShoppingListInput {
    title: String!
    location: String
    dayTime: String
    items: ID!
    owner: ID!
}

type Query {
    lists: [ShoppingList!]!
    list(id: ID!): ShoppingList!
    # items: [Item!]!
    # item(id: ID!): Item!

}

type Mutation {
    createShoppingList(data: ShoppingListInput!): ShoppingList!
    updateShoppingList(id: ID!, data: ShoppingListInput): ShoppingList!
    deleteShoppingList(id: ID!): Boolean
}

And this is the Mutation I'm trying to run

mutation {
  createShoppingList(data:{
    title: "Weekly shopping List"
    location: "Walmart at 5th Avenue"
    dayTime: "Friday 10 at 3pm"
    items:[{
        name: "Full fat milk"
        quantity: "One galon"
    },
    {
        name: "Olive Oil"
        quantity: "One bottle"
    }]
    owner:"606be72f3879a84c34f46744"
  }){
    _id
  }
}

The full error message is below:

{
  "error": {
    "errors": [
      {
        "message": "ID cannot represent a non-string and non-integer value: [{name: \"Full fat milk\", quantity: \"One galon\"}, {name: \"Olive Oil\", quantity: \"One bottle\"}]",
        "locations": [
          {
            "line": 3,
            "column": 114
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "GraphQLError: ID cannot represent a non-string and non-integer value: [{name: \"Full fat milk\", quantity: \"One galon\"}, {name: \"Olive Oil\", quantity: \"One bottle\"}]",
              "    at GraphQLScalarType.parseLiteral (/home/croves/code/api_shopping_list/node_modules/graphql/type/scalars.js:269:13)",
              "    at isValidValueNode (/home/croves/code/api_shopping_list/node_modules/graphql/validation/rules/ValuesOfCorrectTypeRule.js:125:28)",
              "    at Object.ListValue (/home/croves/code/api_shopping_list/node_modules/graphql/validation/rules/ValuesOfCorrectTypeRule.js:40:9)",
              "    at Object.enter (/home/croves/code/api_shopping_list/node_modules/graphql/language/visitor.js:323:29)",
              "    at Object.enter (/home/croves/code/api_shopping_list/node_modules/graphql/utilities/TypeInfo.js:370:25)",
              "    at visit (/home/croves/code/api_shopping_list/node_modules/graphql/language/visitor.js:243:26)",
              "    at Object.validate (/home/croves/code/api_shopping_list/node_modules/graphql/validation/validate.js:69:24)",
              "    at validate (/home/croves/code/api_shopping_list/node_modules/apollo-server-core/src/requestPipeline.ts:514:14)",
              "    at Object.<anonymous> (/home/croves/code/api_shopping_list/node_modules/apollo-server-core/src/requestPipeline.ts:300:32)",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (/home/croves/code/api_shopping_list/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)",
              "    at processTicksAndRejections (internal/process/task_queues.js:93:5)"
            ]
          }
        }
      }
    ]
  }
}


Read more here: https://stackoverflow.com/questions/66997057/how-can-i-create-a-mutation-in-graphql-that-contains-a-subdocument-in-mongodb

Content Attribution

This content was originally published by Croves at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: