Skip to content

Update gRPC library in dgraph-js to use gRPC-js #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions examples/simple/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const dgraph = require("dgraph-js");
const grpc = require("grpc");

// Create a client stub.
function newClientStub() {
Expand Down Expand Up @@ -64,13 +63,13 @@ async function createData(dgraphClient) {
{
name: "Charlie",
age: 29,
}
},
],
school: [
{
name: "Crown Public School",
}
]
},
],
};

// Run mutation.
Expand All @@ -84,10 +83,16 @@ async function createData(dgraphClient) {
// 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(
`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}`));
response
.getUidsMap()
.forEach((uid, key) => console.log(`${key} => ${uid}`));
console.log();
} finally {
// Clean up. Calling this after txn.commit() is a no-op
Expand Down Expand Up @@ -117,7 +122,9 @@ async function queryData(dgraphClient) {
}
}`;
const vars = { $a: "Alice" };
const res = await dgraphClient.newTxn({ readOnly: true }).queryWithVars(query, vars);
const res = await dgraphClient
.newTxn({ readOnly: true })
.queryWithVars(query, vars);
const ppl = res.getJson();

// Print results.
Expand All @@ -141,8 +148,10 @@ async function main() {
dgraphClientStub.close();
}

main().then(() => {
console.log("\nDONE!");
}).catch((e) => {
console.log("ERROR: ", e);
});
main()
.then(() => {
console.log("\nDONE!");
})
.catch((e) => {
console.log("ERROR: ", e);
});
47 changes: 31 additions & 16 deletions examples/tls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ const fs = require('fs');
const path = require('path');

const dgraph = require("dgraph-js");
const grpc = require("grpc");

// Create a client stub.
function newClientStub() {
// First create the appropriate TLS certs with dgraph cert:
// $ dgraph cert
// $ dgraph cert -n localhost
// $ dgraph cert -c user
const rootCaCert = fs.readFileSync(path.join(__dirname, 'tls', 'ca.crt'));
const clientCertKey = fs.readFileSync(path.join(__dirname, 'tls', 'client.user.key'));
const clientCert = fs.readFileSync(path.join(__dirname, 'tls', 'client.user.crt'));
console.log(path.join(__dirname, "tls", "ca.crt"));
const rootCaCert = fs.readFileSync(path.join(__dirname, "tls", "ca.crt"));
const clientCertKey = fs.readFileSync(
path.join(__dirname, "tls", "client.user.key")
);
const clientCert = fs.readFileSync(
path.join(__dirname, "tls", "client.user.crt")
);
return new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createSsl(rootCaCert, clientCertKey, clientCert));
dgraph.grpc.credentials.createSsl(rootCaCert, clientCertKey, clientCert)
);
}

// Create a client.
Expand Down Expand Up @@ -69,13 +74,13 @@ async function createData(dgraphClient) {
{
name: "Charlie",
age: 29,
}
},
],
school: [
{
name: "Crown Public School",
}
]
},
],
};

// Run mutation.
Expand All @@ -89,10 +94,16 @@ async function createData(dgraphClient) {
// 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(
`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}`));
response
.getUidsMap()
.forEach((uid, key) => console.log(`${key} => ${uid}`));
console.log();
} finally {
// Clean up. Calling this after txn.commit() is a no-op
Expand Down Expand Up @@ -122,7 +133,9 @@ async function queryData(dgraphClient) {
}
}`;
const vars = { $a: "Alice" };
const res = await dgraphClient.newTxn({ readOnly: true }).queryWithVars(query, vars);
const res = await dgraphClient
.newTxn({ readOnly: true })
.queryWithVars(query, vars);
const ppl = res.getJson();

// Print results.
Expand All @@ -142,8 +155,10 @@ async function main() {
dgraphClientStub.close();
}

main().then(() => {
console.log("\nDONE!");
}).catch((e) => {
console.log("ERROR: ", e);
});
main()
.then(() => {
console.log("\nDONE!");
})
.catch((e) => {
console.log("ERROR: ", e);
});
3 changes: 1 addition & 2 deletions examples/tls/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "tls",
"dependencies": {
"dgraph-js": "^20.3.0",
"grpc": "^1.24.2"
"dgraph-js": "^20.3.0"
}
}
14 changes: 7 additions & 7 deletions generated/api_grpc_pb.d.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
// package: api
// file: api.proto

import * as grpc from "grpc";
import * as grpc from "@grpc/grpc-js";
import * as api_pb from "./api_pb";

export class DgraphClient extends grpc.Client {
public login(
request: api_pb.LoginRequest,
metadata: grpc.Metadata | null,
options: grpc.CallOptions | null,
callback: (err?: Error | null, res?: api_pb.Response) => void,
callback: (err?: Error | null, res?: api_pb.Response) => void
): void;

public alter(
operation: api_pb.Operation,
metadata: grpc.Metadata | null,
options: grpc.CallOptions | null,
callback: (err?: Error | null, res?: api_pb.Payload) => void,
callback: (err?: Error | null, res?: api_pb.Payload) => void
): void;

public query(
request: api_pb.Request,
metadata: grpc.Metadata | null,
options: grpc.CallOptions | null,
callback: (err?: Error | null, res?: api_pb.Response) => void,
callback: (err?: Error | null, res?: api_pb.Response) => void
): void;

public mutate(
request: api_pb.Mutation,
metadata: grpc.Metadata | null,
options: grpc.CallOptions | null,
callback: (err?: Error | null, res?: api_pb.Response) => void,
callback: (err?: Error | null, res?: api_pb.Response) => void
): void;

public commitOrAbort(
request: api_pb.TxnContext,
metadata: grpc.Metadata | null,
options: grpc.CallOptions | null,
callback: (err?: Error | null, res?: api_pb.TxnContext) => void,
callback: (err?: Error | null, res?: api_pb.TxnContext) => void
): void;

public checkVersion(
request: api_pb.Check,
metadata: grpc.Metadata | null,
options: grpc.CallOptions | null,
callback: (err?: Error | null, res?: api_pb.Version) => void,
callback: (err?: Error | null, res?: api_pb.Version) => void
): void;
}
Loading