# Chained mutations creating nodes and using id from first creation in second?

**URL:** https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269
**Category:** Dgraph
**Tags:** mutation, kind:question
**Created:** [September 10, 2020, 7:20pm UTC](https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269 "2020-09-10T19:20:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![haikuty](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/haikuty/32/4725_2.png) [@haikuty](https://discuss.dgraph.io/u/haikuty)
#### Post date: [September 10, 2020, 7:20pm UTC](https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269/1 "2020-09-10T19:20:34Z")

</div>

with a Schema like (I can’t use the actual schema - client project/NDA - so typing this made-up one in directly here but it’s hopefully close):

```auto
type Location {
  id: ID!
  lat: Float!
  long: Float!
  title: String
}

type Event {
   id: ID!
   title: String!
   location: Location!
}

```

I would like to create an `Event` for a new `Location`.

Dgraph will automatically create Mutation fields of the form:  
`addLocation(input: [LocationInput!]!):LocationPayload`  
and a similar  
`addEvent(input: [EventInput!]!): EventPayload`

I can call the mutations one at a time using `graphiql` and something like:

```auto
mutation {
   addLocation(input: [{lat: 10.5, long: 22.5, title: "fake place"}]) {
       id
       title
   }
}

```

and if it returned an id of “0x7034”, I can copy and paste that into the next mutation to create the event for that location:

```auto
mutation {
    addEvent(input: [{title: "Rally!", location: { id: "0x7034"} }]) {
       id
       title
   }
}

```

and it will work great, but the “copy-and-paste” part is unfortunate…

What I _think_ should be possible is to bundle the two into a single `mutation` and have the id returned for the location created in the first one (or it’s ‘uid’ in the system) usable for the location field of the second mutation to create the event.

Something like:

```auto
   addLocation(input: [{lat: 10.5, long: 22.5, title: "fake place"}]) {
       locid as id
       title
   }
   addEvent(input: [{title: "Rally!", location: { id: $locid }}]) {
       id
       title
   }
}

```

I tried the “locid as id” as shown above (or as “uid”), from the documentation. I tried the `_:name` style notation from elsewhere in the docs. I tried `id @export(as: "locid")`. (“export not defined”).

No luck - Dgraph always returns an error of one kind or another.

- Is this possible? (example using graphiql would be most helpful)
- How? 🙂

Thanks in advance for any insight.

---

<div class="post-metadata">

### Author: ![haikuty](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/haikuty/32/4725_2.png) [@haikuty](https://discuss.dgraph.io/u/haikuty)
#### Post date: [September 10, 2020, 7:23pm UTC](https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269/2 "2020-09-10T19:23:29Z")

</div>

(and if this belongs in a different area, please let me know so I can move it. New to Dgraph and this forum so still figuring things out).

---

<div class="post-metadata">

### Author: ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)
#### Post date: [September 10, 2020, 10:55pm UTC](https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269/3 "2020-09-10T22:55:39Z")

</div>

You can do it in a single mutation addEvent. The generated schema will allow you to define a new Location OR link to an existing if you provide the id.

---

<div class="post-metadata">

### Author: ![haikuty](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/haikuty/32/4725_2.png) [@haikuty](https://discuss.dgraph.io/u/haikuty)
#### Post date: [September 10, 2020, 11:43pm UTC](https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269/4 "2020-09-10T23:43:27Z")

</div>

Thanks for the reply. I do not have an existing Location and the actual situation involves more steps than my simple example (with four different nodes needing to be created and then their ids passed to the final fifth node creation).

I was not able to figure out how to define a new location inside the addEvent, but now that you say it should work I will go back and give that a try. Thanks.

---

<div class="post-metadata">

### Author: ![haikuty](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/haikuty/32/4725_2.png) [@haikuty](https://discuss.dgraph.io/u/haikuty)
#### Post date: [September 11, 2020, 4:36pm UTC](https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269/5 "2020-09-11T16:36:20Z")

</div>

Hey - thanks for confirming this should work because I went back and worked through getting all the query fields right (leaving out “id” mostly seemed to be the necessary piece I missed) and it works!

Still seems like there might be a way to do what I was asking in this thread, but maybe not and now I don’t need it for the immediate task so… moving on! 🙂

Thanks again.

---

<div class="post-metadata">

### Author: ![haikuty](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/haikuty/32/4725_2.png) [@haikuty](https://discuss.dgraph.io/u/haikuty)
#### Post date: [September 11, 2020, 4:37pm UTC](https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269/6 "2020-09-11T16:37:47Z")

</div>

(don’t really see how to “close” this issue, but maybe it’s a permissions thing?)

---

<div class="post-metadata">

### Author: ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)
#### Post date: [September 11, 2020, 5:10pm UTC](https://discuss.dgraph.io/t/chained-mutations-creating-nodes-and-using-id-from-first-creation-in-second/10269/7 "2020-09-11T17:10:22Z")

</div>

> [@haikuty](#):
>
> Still seems like there might be a way to do what I was asking in this thread, but maybe not

The need does arise as I presented over here:

> [@Add id manually while using GraphQL](http://discuss.hypermode.com/t/add-id-manually-while-using-graphql/10047/14):
>
> I think the intended concept is to have a similar experience to using blank nodes in Dgraph in the add mutation. The OP does not want to use custom IDs but rather use Dgraph’s IDs but just have a way to define links between them in a single addMutation. To expand on the example for better proof of concept lets say we wanted to create these new people as friends: Or \<-\> Michal Or \<-\> Justin Or \<-\> Elior Michal \<-\> Justin This becomes complicated with a single mutation because you have to stop …

Trying to add four way related data is not possible as explained better in the referenced topic.

Glad it worked and solved your problem at this stage.
