Representation of dgraph.type with dgraph4j

java-client
mutation

(Shivangi) #1

How can we add type to a node while storing it in dgraph with dgraph4j client?

In the code, while adding a node, where we are converting object to JSON and using setSetJson method of Mutation class to add data to dgraph.

private boolean setData(final String personJson) {
          
        Mutation mutation = Mutation.newBuilder()
                .setSetJson(ByteString.copyFromUtf8(personJson))
                .build();

        try (Transaction txn = clients.newTransaction()) {
            txn.mutate(mutation);
            txn.commit();
            return true;
        }
    }

when using setSetJson method, it is not able to add dgraph.type to the node.
So, is there any way to add type to the node?


(Michel Conrado) #2

You can make a second mutation after you have inserted JSON. Just take the mutation mapped UID and make a new mutation by adding dgraph.type.

Another way you can do it is by using Bulk Upsert.


upsert {
  query {
    v as var(func: has(email)) #just an example, you should add a contextual query here.
  }

  mutation {
    set {
      uid(v) <dgraph.type> "User" .
    }
  }
}

(Shivangi) #3

Is there any way to add type information to the node while it is being created?


(Michel Conrado) #4

In the body of the mutation.