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

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):

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:

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:

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:

   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? :slight_smile:

Thanks in advance for any insight.

(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).

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.

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.

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! :slight_smile:

Thanks again.

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

The need does arise as I presented over here:

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.

2 Likes