Reputation: 1174
The following code is intended to query a Dgraph server for the values associated with a specific node, similar to looking up a row by primary key in a relational database.
const dgraph = require("dgraph-js");
const grpc = require("grpc");
const clientStub = new dgraph.DgraphClientStub( grpc.credentials.createInsecure() );
const dgraphClient = new dgraph.DgraphClient(clientStub);
const lookupNode = `query nodeValues($id:uid) {
nodeValues(func: uid($id)) {
value1,
value2,
value3
}
}`;
const res = await dgraphClient.newTxn().queryWithVars(query, {$id: "0x13");
This raises Error: 2 UNKNOWN: Type "uid" not supported
when executing. If the $id
variable is typed as a string
or int
no results are found. How does one query by UID of a node with variables?
Upvotes: 0
Views: 547
Reputation:
const dgraph = require("dgraph-js");
const grpc = require("grpc");
const clientStub = new dgraph.DgraphClientStub( grpc.credentials.createInsecure() );
const dgraphClient = new dgraph.DgraphClient(clientStub);
const lookupNode = `query nodeValues(**$id:string**) {
nodeValues(func: uid($id)) {
value1,
value2,
value3
}
}`;
const res = await dgraphClient.newTxn().queryWithVars(query, {$id: "0x13");
Use String as data type for your uid :)
Upvotes: 0
Reputation: 33
The type of the $id
query variable in this case should be string
. It would be helpful to see the actual output that you have. Nevertheless I assume there is a problem with asynchronous functions in your example. The following code gives me the desired output { nodeValues: [ { value1: 'value1' } ] }
:
const dgraph = require("dgraph-js");
const grpc = require("grpc");
const clientStub = async () => new dgraph.DgraphClientStub("localhost:9080", grpc.credentials.createInsecure());
const dgraphClient = async (clientStub) => new dgraph.DgraphClient(clientStub);
async function createData(dgraphClient) {
const txn = dgraphClient.newTxn();
try {
const p = {uid: "0x13", value1: "value1"};
const mu = new dgraph.Mutation();
mu.setSetJson(p);
await txn.mutate(mu);
await txn.commit();
} finally {
await txn.discard();
}
}
const lookupNode = `query nodeValues($id:string) {
nodeValues(func: uid($id)) {
value1,
value2,
value3
}
}`;
const res = async (dgraphClient) => {
const response = await dgraphClient.newTxn().queryWithVars(lookupNode, {$id: "0x13"});
return response.getJson();
};
const main = async () => {
const stub = await clientStub();
const client = await dgraphClient(stub);
await createData(client);
return await res(client);
};
main().then((res) => {
console.log(res);
}).catch((e) => {
console.log("ERROR: ", e);
});
Based on the example from the officially supported client: https://github.com/dgraph-io/dgraph-js/blob/master/examples/simple/index.js
Upvotes: 0