# How can I append element to array wiht graphql

**URL:** <https://discuss.dgraph.io/t/how-can-i-append-element-to-array-wiht-graphql/6188>\
**Category:** GraphQL\
**Tags:** mutation, graphql, kind:question\
**Created:** [March 27, 2020, 3:57am UTC](https://discuss.dgraph.io/t/how-can-i-append-element-to-array-wiht-graphql/6188 "2020-03-27T03:57:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wisgon](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/wisgon/32/3898_2.png) [@Wisgon](https://discuss.dgraph.io/u/Wisgon)\
**Post date:** [March 27, 2020, 3:57am UTC](https://discuss.dgraph.io/t/how-can-i-append-element-to-array-wiht-graphql/6188/1 "2020-03-27T03:57:57Z")

</div>

Hi, I’m using graphql with Dgraph2.0.  
Here is my schema:

```auto
type User {
    name String! @id
    interest [String!]
}

```

Then, how can I add an element to the “interest” array?  
I tried something like this:

```auto
mutation {
    updateUser(input: {
        filter: {name: {eq:"Somebody"}},
        set:{interest: append("football")}
    }) {
            user{
                name
                interest 
            }
        }
}

```

But it didn’t work, so how can I make the mutation with graphql?  
I don’t want to write the entire array like this:` set:{interest:["cook", "reading", "football"]}`, just want to append element what I want.

---

<div class="post-metadata">

**Author:** ![Wisgon](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/wisgon/32/3898_2.png) [@Wisgon](https://discuss.dgraph.io/u/Wisgon)\
**Post date:** [March 28, 2020, 9:36am UTC](https://discuss.dgraph.io/t/how-can-i-append-element-to-array-wiht-graphql/6188/2 "2020-03-28T09:36:03Z")

</div>

Oh, my fault.  
It’s very easy to append element, just `set: {interest: ["football"]}`, it will add element “football” to interest, not just set the interest to `["football"]`.  
I inspire by [graphql±doc](https://dgraph.io/docs/query-language/schema/#list-type) – “A set operation adds to the list of values. The order of the stored values is non-deterministic.”

---

<div class="post-metadata">

**Author:** ![Wisgon](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/wisgon/32/3898_2.png) [@Wisgon](https://discuss.dgraph.io/u/Wisgon)\
**Post date:** [March 29, 2020, 6:14am UTC](https://discuss.dgraph.io/t/how-can-i-append-element-to-array-wiht-graphql/6188/3 "2020-03-29T06:14:22Z")

</div>

By the way, `remove: {interest:[""]}` to empty the array.

---

<div class="post-metadata">

**Author:** ![matthewmcneely](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matthewmcneely/32/4774_2.png) [@matthewmcneely](https://discuss.dgraph.io/u/matthewmcneely)\
**Post date:** [December 8, 2020, 8:32pm UTC](https://discuss.dgraph.io/t/how-can-i-append-element-to-array-wiht-graphql/6188/4 "2020-12-08T20:32:28Z")

</div>

Thanks @Wisgon… BTW for anyone searching manipulating arrays in graphql, an idiom I’ve developed is to chain mutations in an update to first clear arrays, then “rewrite” them, for instance:

```auto
mutation updateLocation($id: ID!, $location: LocationPatch!) {
  # clear existing address lines (ensure new/existing lines are in LocationPatch)
  clearAddressesLines: updateLocation(input: {filter: {id: [$id]}, remove: {addresses: [""]}}) {
    location {
      id
    }
  }
  updateLocation(input: {filter: {id: [$id]}, set: $location}) {
    location {
      id
      addresses
      locality
      region
      postcode
    }
  }
}

```
