How to update existing node without creating new node?

I’m trying to do one of two things when I get a UID from my client:

  • Update a value predicate on the node with the given UID
  • Throw an error if there is no node with the given UID

At the very bottom of this page in the tour, it says:

If you write a mutation with an unallocated UID, Dgraph will return an error.

That sounds like exactly what I want, but when I try to mutate a non-existent UID, a new node simply gets created with that UID. I never see an error from Dgraph.

Below is a simple schema and some test code using the JavaScript client. I ran this on a brand new instance of Dgraph, so there was not any node with UID 0x123 (and I can reproduce the behavior by repeatedly changing the UID in the script to some higher value, such as 0x456, etc.).

What am I missing?

Schema

type Tag {
  Tag.name
}

Tag.name: string .

Code

import {
  DgraphClient,
  DgraphClientStub,
  Mutation,
  Txn
} from 'dgraph-js'

async function test(): Promise<void> {
  const stub = new DgraphClientStub('localhost:9080')
  const client = new DgraphClient(stub)
  const txn: Txn = client.newTxn({ readOnly: false })
  const mutation = new Mutation()
  mutation.setSetJson({
    'dgraph.type': 'Tag',
    'Tag.name': '#software',
    uid: '0x123'
  })
  await txn.mutate(mutation)
  await txn.commit()
  await txn.discard()
}

test()

That’s because it happens only when you mutate something. You can query any UID that it will return something.

The only way to return an error is by throwing it yourself by using length. e.g

This is a pseudo code.

const doSomething = (e) =>  {

   const response = mutate(e.myRDF);

   let check = response.data.myquery.length;

    if (check < 1) 
        throw new Error(`There's no node with that UID`);

    return response;
};