Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Updates client-nodejs to use latest SessionService gRPC API (#7)
Browse files Browse the repository at this point in the history
# Why is this PR needed?

Fixes #5 
So we are able to use `client-nodejs` with the latest `@graknlabs_grakn_core`

# What does the PR do?

- Upgrades commit hash of `@graknlabs_grakn_core`
- Fixes the client to comply with new API

# Does it break backwards compatibility?

—

# List of future improvements not on this PR

—
  • Loading branch information
vmax authored Feb 19, 2019
1 parent d24d6bb commit af9a565
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ npm_package(
":bundle",
"@nodejs_dependencies//grpc",
"@nodejs_dependencies//google-protobuf"
]
],
visibility = ["//visibility:public"]
)

deploy_npm(
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "graknlabs_grakn_core",
remote = "https://github.com/graknlabs/grakn",
commit = "f0959ee2681b5c2fe850aa7b697c35b2c2d4d720"
commit = "80a9d8f01cb2fe642b9c29d0c550987dee3feb67"
)

git_repository(
Expand Down
19 changes: 17 additions & 2 deletions src/service/Session/SessionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
const grpc = require("grpc");
const services = require("../../../client-nodejs-proto/protocol/session/Session_grpc_pb");
const TxService = require("./TransactionService");
const RequestBuilder = require("./util/RequestBuilder");


/**
Expand All @@ -32,22 +33,36 @@ function SessionService(uri, keyspace, credentials) {
this.stub = new services.SessionServiceClient(uri, grpc.credentials.createInsecure());
}

function wrapInPromise(self, fn, requestMessage){
return new Promise((resolve, reject) => {
fn.call(self, requestMessage, (error, response) => {
if (error) { reject(error); }
resolve(response);
});
});
}

/**
* This method creates a new Duplex Stream (this.stub.transaction()) over which gRPC will communicate when
* exchanging messages related to the Transaction service.
* It also sends an Open request before returning the TransactionService
* @param {Grakn.txType} txType type of transaction to be open
*/
SessionService.prototype.transaction = async function create(txType) {
if (this.sessionId === undefined) {
this.sessionId = (await wrapInPromise(this.stub, this.stub.open, RequestBuilder.openSession(this.keyspace))).getSessionid();
}

const txService = new TxService(this.stub.transaction());
await txService.openTx(this.keyspace, txType, this.credentials);
await txService.openTx(this.sessionId, txType, this.credentials);
return txService;
}

/**
* Closes connection to the server
*/
SessionService.prototype.close = function close() {
SessionService.prototype.close = async function close() {
await wrapInPromise(this.stub, this.stub.close, RequestBuilder.closeSession(this.sessionId));
grpc.closeClient(this.stub);
}

Expand Down
4 changes: 2 additions & 2 deletions src/service/Session/TransactionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ TransactionService.prototype.getAttributesByValue = function (value, dataType) {
.then(response => this.respConverter.getAttributesByValue(response));
}

TransactionService.prototype.openTx = function (keyspace, txType, credentials) {
const txRequest = RequestBuilder.openTx(keyspace, txType, credentials);
TransactionService.prototype.openTx = function (sessionId, txType, credentials) {
const txRequest = RequestBuilder.openTx(sessionId, txType, credentials);
return this.communicator.send(txRequest);
};

Expand Down
14 changes: 12 additions & 2 deletions src/service/Session/util/RequestBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,20 @@ const methods = {
txRequest.setGetattributesReq(getAttributesReq);
return txRequest;
},
openTx: function (keyspace, txType, credentials) {
openSession: function(keyspace) {
const sessionRequest = new messages.Session.Open.Req();
sessionRequest.setKeyspace(keyspace);
return sessionRequest;
},
closeSession: function(sessionId) {
const sessionRequest = new messages.Session.Close.Req();
sessionRequest.setSessionid(sessionId);
return sessionRequest;
},
openTx: function (sessionId, txType, credentials) {
const openRequest = new messages.Transaction.Open.Req();
const txRequest = new messages.Transaction.Req();
openRequest.setKeyspace(keyspace);
openRequest.setSessionid(sessionId);
openRequest.setType(txType);
if (credentials) {
openRequest.setUsername(credentials.username);
Expand Down

0 comments on commit af9a565

Please sign in to comment.