JSON mutation: Cannot directly create reverse predicate


Using JSON mutation with the Go client, I’d like to be able to create edges from a reversed predicate.

For instance, if I have the following models:

type Person struct {
	Id     string   `json:"uid,omitempty"`
	Name   string   `json:"name,omitempty"`
	Bike   *Bike    `json:"owns,omitempty"`
type Bike struct {
	Id     string   `json:"uid,omitempty"`
	Brand  string   `json:"brand,omitempty"`
	Owner  *Person  `json:"~owns,omitempty"`

I can easily create a Person that owns a bike with the following mutation model:

mutationModel := Person{ Name: "John", Bike { Brand: "Giant" } }

But if I’d like to create a bike and set its owner, only the bike is created:

mutationModel := Bike{ Brand: "Giant", Owner { Name: "John" } }

Do I have to always create nodes from the normal edge direction?
If so it will require many more mutation operations for my application :face_with_raised_eyebrow:

1 Like

Nevermind, I just understood how to correlate IDs using Id: "_:my-key" and then reuse _:my-key wherever I need to reference the same new ID. That way I can do a single mutation with an array of several different models referencing each others.

1 Like

Get the uid of Bob from this :

p := Person{
    Name: "Bob",
    Age:  24,

bob := assigned.Uids["blank-0"]

Your can reference its uid and create an edge between Alice and Bob
Like this :

p = Person{
    Name:    "Alice",
    Age:     26,
    Friends: []Person{{
        Uid: bob,

So in your example for owner and bike, you can create a link between both the nodes by referencing the uid.

mutationModel := Person{ Name: "John", Bike { Brand: "Giant" } }
john := assigned.Uids["blank-0"]
mutationModel := Bike{ Brand: "Giant", Owner { Uid: John } }

Hope ir clears your doubt… :slight_smile:

Thx for the example.
But you do it with 2 mutation calls while I wanted to do it with a single one.
My solution looks like that:

mutation1 := Bike{ Id: "_:bike", Brand: "Giant", Owner { Id: "_:person", Name: "John" } }
mutation2 := Person{ Id: "_:person", Name: "John", Bike { Id: "_:bike" } }
mutation := interface{}
mutation = append(mutation, mutation1, mutation2)
// Do mutation

That way I only do one call to dGraph.
In this basic example it could have been done in a simpler manner but when you have several reverse predicates, you cannot create all your nodes with a single mutation.

is there a documentation link for this method? thanks

Yes, in the Go client documentation.

been trying this in Python and it was not working. ok thanks

According to the Mutations documentation:

JSON mutations are only available via gRPC clients, such as the Go client, JS client, and Java client. They’re not available via the raw HTTP interface.

1 Like

actually in version 1.0.6 you can send JSON via Raw HTTP

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.