How to add object in an array?

Welcome @secretshardul!

Your mutation should work after removing the “id” field in the task node as below. A new task will be added to the existing user. ID will be generated by Dgraph.

mutation addTaskToUser {
  updateUser(input: {
    filter: {username: {eq: "alice@dgraph.io"}},
    set: {
      tasks: [{
        title: "new task",
        completed: true
        
      }]
    }
  }) {
    user {
      username
      name
      tasks {
        id
        title
        completed
      }
    }
  }
}

You can then query for data as below:

query MyQuery {
  getUser(username: "alice@dgraph.io") {
    username
    tasks {
      id
      title
      completed
    }
  }
}

Result I got on Slash.

{
  "data": {
    "getUser": {
      "username": "alice@dgraph.io",
      "tasks": [
        {
          "id": "0x4e22",
          "title": "new task",
          "completed": true
        }
      ]
    }
  }
1 Like