gpahal commented :
It’s working with this example.
const dgraph = require("dgraph-js");
async function main() {
const clientStub = new dgraph.DgraphClientStub("localhost:9080");
const client = new dgraph.DgraphClient(clientStub);
console.log("DROP ALL");
let op = new dgraph.Operation();
op.setDropAll(true);
await client.alter(op);
console.log("SET SCHEMA");
const schema = `
name: string @index(term) .
topic.external.wikidata: int .
`;
op = new dgraph.Operation();
op.setSchema(schema);
await client.alter(op);
console.log("CREATE DATA");
let txn = client.newTxn();
let uid;
try {
const mu = new dgraph.Mutation();
mu.setSetNquads(`
<_:a> <name> "a" .
<_:a> <topic.external.wikidata> "1" .
<_:b> <name> "b" .
<_:b> <topic.external.wikidata> "2" .
`);
const assigned = await txn.mutate(mu);
await txn.commit();
uid = assigned.getUidsMap().get("b")
} finally {
await txn.discard();
}
console.log("QUERY DATA");
let query = `{
all(func: has(name)) {
uid
name
topic.external.wikidata
}
}`;
let res = await client.newTxn().query(query);
let ppl = res.getJson();
console.log(`BEFORE:\nNumber of people: ${ppl.all.length}`);
console.log(`People: ${JSON.stringify(ppl)}\n`);
console.log("DELETE DATA");
txn = client.newTxn();
try {
const mu = new dgraph.Mutation();
mu.setDeleteJson([{ uid: uid, "topic.external.wikidata": null }]);
await txn.mutate(mu);
await txn.commit();
} finally {
await txn.discard();
}
console.log("QUERY DATA");
query = `{
all(func: has(name)) {
uid
name
topic.external.wikidata
}
}`;
res = await client.newTxn().query(query);
ppl = res.getJson();
console.log(`AFTER:\nNumber of people: ${ppl.all.length}`);
console.log(`People: ${JSON.stringify(ppl)}`);
clientStub.close();
}
main().then(() => {
console.log("DONE!");
}).catch((e) => {
console.log("ERROR: ", e);
});