I have some problems with the latest changes to the client dgraph-js (v20.3.1) in combination with the grpc (v1.24.3) client.
When I launch dgraph I use the auth-token attribute:
docker run -it -p 5080:5080 -p 6080:6080 -p 8080:8080 -p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph --name dgraph dgraph/dgraph:latest dgraph zero
docker exec -it dgraph dgraph alpha --lru_mb 2048 --zero localhost:5080 --whitelist 0.0.0.0/0 --auth_token "secret"
When I try to run the dgraph-js sample client, where I have updated the package.json to the latest versions.
------- package.json
{
"name": "simple",
"dependencies": {
"dgraph-js": "^20.3.1",
"grpc": "^1.24.3"
}
}
---------
And when adding metadata to the alter operations with the auth-token as in this updated simple example file.
---- simple - index.js
const dgraph = require("dgraph-js");
const grpc = require("grpc");
let meta = new grpc.Metadata();
meta.add("auth-token", "secret");
// Create a client stub.
function newClientStub() {
return new dgraph.DgraphClientStub("localhost:9080");
}
// Create a client.
function newClient(clientStub) {
return new dgraph.DgraphClient(clientStub);
}
// Drop All - discard all data, schema and start from a clean slate.
async function dropAll(dgraphClient) {
const op = new dgraph.Operation();
op.setDropAll(true);
await dgraphClient.alter(op, meta);
}
// Drop All Data, but keep the schema.
async function dropData(dgraphClient) {
const op = new dgraph.Operation();
op.setDropOp(dgraph.Operation.DropOp.DATA);
await dgraphClient.alter(op, meta);
}
// Set schema.
async function setSchema(dgraphClient) {
const schema = `
name: string @index(exact) .
age: int .
married: bool .
loc: geo .
dob: datetime .
friend: [uid] @reverse .
`;
const op = new dgraph.Operation();
op.setSchema(schema);
await dgraphClient.alter(op, meta);
}
// Create data using JSON.
async function createData(dgraphClient) {
// Create a new transaction.
const txn = dgraphClient.newTxn();
try {
// Create data.
const p = {
uid: "_:alice",
name: "Alice",
age: 26,
married: true,
loc: {
type: "Point",
coordinates: [1.1, 2],
},
dob: new Date(1980, 1, 1, 23, 0, 0, 0),
friend: [
{
name: "Bob",
age: 24,
},
{
name: "Charlie",
age: 29,
},
],
school: [
{
name: "Crown Public School",
},
],
};
// Run mutation.
const mu = new dgraph.Mutation();
mu.setSetJson(p);
const response = await txn.mutate(mu);
// Commit transaction.
await txn.commit();
// Get uid of the outermost object (person named "Alice").
// Response#getUidsMap() returns a map from blank node names to uids.
// For a json mutation, blank node label is used for the name of the created nodes.
console.log(
`Created person named "Alice" with uid = ${response
.getUidsMap()
.get("alice")}\n`
);
console.log("All created nodes (map from blank node names to uids):");
response
.getUidsMap()
.forEach((uid, key) => console.log(`${key} => ${uid}`));
console.log();
} finally {
// Clean up. Calling this after txn.commit() is a no-op
// and hence safe.
await txn.discard();
}
}
// Query for data.
async function queryData(dgraphClient) {
// Run query.
const query = `query all($a: string) {
all(func: eq(name, $a)) {
uid
name
age
married
loc
dob
friend {
name
age
}
school {
name
}
}
}`;
const vars = { $a: "Alice" };
const res = await dgraphClient
.newTxn({ readOnly: true })
.queryWithVars(query, vars);
const ppl = res.getJson();
// Print results.
console.log(`Number of people named "Alice": ${ppl.all.length}`);
ppl.all.forEach((person) => console.log(person));
}
async function main() {
const dgraphClientStub = newClientStub();
const dgraphClient = newClient(dgraphClientStub);
await dropAll(dgraphClient);
await setSchema(dgraphClient);
await createData(dgraphClient);
await queryData(dgraphClient);
await dropData(dgraphClient);
await queryData(dgraphClient);
await createData(dgraphClient);
await queryData(dgraphClient);
// Close the client stub.
dgraphClientStub.close();
}
main()
.then(() => {
console.log("\nDONE!");
})
.catch((e) => {
console.log("ERROR: ", e);
});
-----
When I try to run the simple example I get the following errors:
ERROR: Error: Incorrect arguments passed
at ServiceClientImpl.checkOptionalUnaryResponseArguments (/Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/@grpc/grpc-js/build/src/client.js:111:23)
at ServiceClientImpl.makeUnaryRequest (/Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/@grpc/grpc-js/build/src/client.js:118:39)
at ServiceClientImpl.alter (/Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/@grpc/grpc-js/build/src/make-client.js:93:19)
at /Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/dgraph-js/lib/util.js:53:15
at new Promise (<anonymous>)
at Object.alter (/Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/dgraph-js/lib/util.js:52:16)
at DgraphClientStub.alter (/Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/dgraph-js/lib/clientStub.js:95:33)
at DgraphClient.<anonymous> (/Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/dgraph-js/lib/client.js:67:42)
at step (/Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/dgraph-js/lib/client.js:33:23)
at Object.next (/Users/erlendo/tmp/dgraph-js/examples/simple/node_modules/dgraph-js/lib/client.js:14:53)
If someone could provide some light to why this happens, and what I’m doing wrong, that would be most appreciated.
Cheers
Erlend