Skip to content

Commit f3bde42

Browse files
committed
Add call options parameter to client objects and options parameter to the constructor
1 parent 92f203c commit f3bde42

File tree

5 files changed

+174
-38
lines changed

5 files changed

+174
-38
lines changed

generated/api_grpc_pb.d.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,38 @@ import * as grpc from "grpc";
55
import * as api_pb from "./api_pb";
66

77
export class DgraphClient extends grpc.Client {
8-
public alter(operation: api_pb.Operation, callback: (err?: Error | null, res?: api_pb.Payload) => void): void;
9-
public query(request: api_pb.Request, callback: (err?: Error | null, res?: api_pb.Response) => void): void;
10-
public mutate(request: api_pb.Mutation, callback: (err?: Error | null, res?: api_pb.Assigned) => void): void;
11-
public commitOrAbort(request: api_pb.TxnContext, callback: (err?: Error | null, res?: api_pb.TxnContext) => void): void;
12-
public checkVersion(request: api_pb.Check, callback: (err?: Error | null, res?: api_pb.Version) => void): void;
8+
public alter(
9+
operation: api_pb.Operation,
10+
metadata: grpc.Metadata | null,
11+
options: grpc.CallOptions | null,
12+
callback: (err?: Error | null, res?: api_pb.Payload) => void,
13+
): void;
14+
15+
public query(
16+
request: api_pb.Request,
17+
metadata: grpc.Metadata | null,
18+
options: grpc.CallOptions | null,
19+
callback: (err?: Error | null, res?: api_pb.Response) => void,
20+
): void;
21+
22+
public mutate(
23+
request: api_pb.Mutation,
24+
metadata: grpc.Metadata | null,
25+
options: grpc.CallOptions | null,
26+
callback: (err?: Error | null, res?: api_pb.Assigned) => void,
27+
): void;
28+
29+
public commitOrAbort(
30+
request: api_pb.TxnContext,
31+
metadata: grpc.Metadata | null,
32+
options: grpc.CallOptions | null,
33+
callback: (err?: Error | null, res?: api_pb.TxnContext) => void,
34+
): void;
35+
36+
public checkVersion(
37+
request: api_pb.Check,
38+
metadata: grpc.Metadata | null,
39+
options: grpc.CallOptions | null,
40+
callback: (err?: Error | null, res?: api_pb.Version) => void,
41+
): void;
1342
}

src/clientStub.ts

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,48 @@ import * as grpc from "grpc";
33
import * as services from "../generated/api_grpc_pb";
44
import * as messages from "../generated/api_pb";
55

6-
import { promisify } from "./util";
6+
import { promisify1, promisify3 } from "./util";
77

88
/**
99
* Stub is a stub/client connecting to a single dgraph server instance.
1010
*/
1111
export class DgraphClientStub {
1212
private stub: services.DgraphClient;
1313
private promisified: {
14-
alter(op: messages.Operation): Promise<messages.Payload>;
15-
query(req: messages.Request): Promise<messages.Response>;
16-
mutate(mu: messages.Mutation): Promise<messages.Assigned>;
17-
commitOrAbort(ctx: messages.TxnContext): Promise<messages.TxnContext>;
18-
checkVersion(check: messages.Check): Promise<messages.Version>;
14+
alter(
15+
op: messages.Operation,
16+
metadata: grpc.Metadata | null,
17+
options: grpc.CallOptions | null,
18+
): Promise<messages.Payload>;
19+
20+
query(
21+
req: messages.Request,
22+
metadata: grpc.Metadata | null,
23+
options: grpc.CallOptions | null,
24+
): Promise<messages.Response>;
25+
26+
mutate(
27+
mu: messages.Mutation,
28+
metadata: grpc.Metadata | null,
29+
options: grpc.CallOptions | null,
30+
): Promise<messages.Assigned>;
31+
32+
commitOrAbort(
33+
ctx: messages.TxnContext,
34+
metadata: grpc.Metadata | null,
35+
options: grpc.CallOptions | null,
36+
): Promise<messages.TxnContext>;
37+
38+
checkVersion(
39+
check: messages.Check,
40+
metadata: grpc.Metadata | null,
41+
options: grpc.CallOptions | null,
42+
): Promise<messages.Version>;
43+
1944
waitForReady(deadline: grpc.Deadline): Promise<void>;
2045
};
2146

22-
constructor(addr?: string | null, credentials?: grpc.ChannelCredentials | null) {
47+
constructor(addr?: string | null, credentials?: grpc.ChannelCredentials | null, options?: object | null) {
2348
if (addr == null) {
2449
// tslint:disable-next-line no-parameter-reassignment
2550
addr = "localhost:9080";
@@ -29,35 +54,35 @@ export class DgraphClientStub {
2954
credentials = grpc.credentials.createInsecure();
3055
}
3156

32-
this.stub = new services.DgraphClient(addr, credentials);
57+
this.stub = new services.DgraphClient(addr, credentials, options);
3358
this.promisified = {
34-
alter: promisify(this.stub.alter, this.stub),
35-
query: promisify(this.stub.query, this.stub),
36-
mutate: promisify(this.stub.mutate, this.stub),
37-
commitOrAbort: promisify(this.stub.commitOrAbort, this.stub),
38-
checkVersion: promisify(this.stub.checkVersion, this.stub),
39-
waitForReady: promisify(this.stub.waitForReady, this.stub),
59+
alter: promisify3(this.stub.alter, this.stub),
60+
query: promisify3(this.stub.query, this.stub),
61+
mutate: promisify3(this.stub.mutate, this.stub),
62+
commitOrAbort: promisify3(this.stub.commitOrAbort, this.stub),
63+
checkVersion: promisify3(this.stub.checkVersion, this.stub),
64+
waitForReady: promisify1(this.stub.waitForReady, this.stub),
4065
};
4166
}
4267

43-
public alter(op: messages.Operation): Promise<messages.Payload> {
44-
return this.promisified.alter(op);
68+
public alter(op: messages.Operation, options?: grpc.CallOptions | null): Promise<messages.Payload> {
69+
return this.promisified.alter(op, undefined, options);
4570
}
4671

47-
public query(req: messages.Request): Promise<messages.Response> {
48-
return this.promisified.query(req);
72+
public query(req: messages.Request, options?: grpc.CallOptions | null): Promise<messages.Response> {
73+
return this.promisified.query(req, undefined, options);
4974
}
5075

51-
public mutate(mu: messages.Mutation): Promise<messages.Assigned> {
52-
return this.promisified.mutate(mu);
76+
public mutate(mu: messages.Mutation, options?: grpc.CallOptions | null): Promise<messages.Assigned> {
77+
return this.promisified.mutate(mu, undefined, options);
5378
}
5479

55-
public commitOrAbort(ctx: messages.TxnContext): Promise<messages.TxnContext> {
56-
return this.promisified.commitOrAbort(ctx);
80+
public commitOrAbort(ctx: messages.TxnContext, options?: grpc.CallOptions | null): Promise<messages.TxnContext> {
81+
return this.promisified.commitOrAbort(ctx, undefined, options);
5782
}
5883

59-
public checkVersion(check: messages.Check): Promise<messages.Version> {
60-
return this.promisified.checkVersion(check);
84+
public checkVersion(check: messages.Check, options?: grpc.CallOptions | null): Promise<messages.Version> {
85+
return this.promisified.checkVersion(check, undefined, options);
6186
}
6287

6388
public waitForReady(deadline: grpc.Deadline): Promise<void> {

src/util.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function isConflictError(err: any): boolean { // tslint:disable-line no-a
4949
return ec.valid && (ec.code === grpc.status.ABORTED || ec.code === grpc.status.FAILED_PRECONDITION);
5050
}
5151

52-
export function promisify<A, T>(
52+
export function promisify1<A, T>(
5353
f: (arg: A, cb: (err?: Error | null, res?: T) => void) => void,
5454
thisContext?: any, // tslint:disable-line no-any
5555
): (arg: A) => Promise<T> {
@@ -65,6 +65,24 @@ export function promisify<A, T>(
6565
};
6666
}
6767

68+
export function promisify3<A, B, C, T>(
69+
f: (argA: A, argB: B, argC: C, cb: (err?: Error | null, res?: T) => void) => void,
70+
thisContext?: any, // tslint:disable-line no-any
71+
): (argA: A, argB: B, argC: C) => Promise<T> {
72+
return (argA: A, argB: B, argC: C) => {
73+
// tslint:disable-next-line no-any
74+
return new Promise((resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void): void => {
75+
f.call(
76+
thisContext,
77+
argA,
78+
argB,
79+
argC,
80+
(err?: Error | null, result?: T): void => (err != null) ? reject(err) : resolve(result),
81+
);
82+
});
83+
};
84+
}
85+
6886
export function stringifyMessage(msg: jspb.Message): string {
6987
return JSON.stringify(msg.toObject());
7088
}

tests/clientStub.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as grpc from "grpc";
2+
13
import * as dgraph from "../src";
24

35
import { SERVER_ADDR, SERVER_CREDENTIALS, setup } from "./helper";
@@ -24,6 +26,19 @@ describe("clientStub", () => {
2426
});
2527
});
2628

29+
describe("checkVersion with call options", () => {
30+
it("should check version with call options", async () => {
31+
const clientStub = new dgraph.DgraphClientStub(SERVER_ADDR, SERVER_CREDENTIALS);
32+
const p = clientStub.checkVersion(new dgraph.Check(), {
33+
deadline: 0,
34+
propagate_flags: grpc.propagate.DEFAULTS,
35+
credentials: null,
36+
});
37+
38+
await expect(p).rejects.toBeDefined();
39+
});
40+
});
41+
2742
describe("waitForReady", () => {
2843
it("should provide a promisified version of grpc.Client#waitForReady", async () => {
2944
const clientStub = new dgraph.DgraphClientStub(SERVER_ADDR, SERVER_CREDENTIALS);

tests/util.spec.ts

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ import * as grpc from "grpc";
22

33
import * as dgraph from "../src";
44
// Non-exported functions.
5-
import { isAbortedError, isBase64, isConflictError, mergeLinReads, promisify } from "../src/util";
5+
import { isAbortedError, isBase64, isConflictError, mergeLinReads, promisify1, promisify3 } from "../src/util";
66

77
import { areLinReadsEqual, createLinRead } from "./helper";
88

9-
function fnAddThisVal(a: number, cb: (err?: Error | null, res?: number) => void): void {
9+
function fnAddThisVal1(a: number, cb: (err?: Error | null, res?: number) => void): void {
1010
// tslint:disable-next-line no-invalid-this
1111
cb(null, (<{ val: number }>this).val + a);
1212
}
1313

14+
function fnAddThisVal3(a: number, b: number, c: number, cb: (err?: Error | null, res?: number) => void): void {
15+
// tslint:disable-next-line no-invalid-this
16+
cb(null, (<{ val: number }>this).val + a + b + c);
17+
}
18+
1419
describe("util", () => {
1520
describe("mergeLinReads", () => {
1621
it("should merge two differnt linReads", () => {
@@ -70,13 +75,13 @@ describe("util", () => {
7075
});
7176
});
7277

73-
describe("promisify", () => {
78+
describe("promisify1", () => {
7479
it("should handle valid response in callback", async () => {
7580
const f = (_: number, cb: (err?: Error | null, res?: number) => void) => {
7681
cb(null, 2);
7782
};
7883

79-
await expect(promisify(f, null)(1)).resolves.toBe(2);
84+
await expect(promisify1(f, null)(1)).resolves.toBe(2);
8085
});
8186

8287
it("should handle error in callback", async () => {
@@ -85,7 +90,7 @@ describe("util", () => {
8590
cb(e);
8691
};
8792

88-
await expect(promisify(f, null)(1)).rejects.toBe(e);
93+
await expect(promisify1(f, null)(1)).rejects.toBe(e);
8994
});
9095

9196
it("should handle error if valid response is also present in callback", async () => {
@@ -94,23 +99,67 @@ describe("util", () => {
9499
cb(e, 2);
95100
};
96101

97-
await expect(promisify(f, null)(1)).rejects.toBe(e);
102+
await expect(promisify1(f, null)(1)).rejects.toBe(e);
98103
});
99104

100105
it("should handle callback called without arguments", async () => {
101106
const f = (_: number, cb: (err?: Error | null, res?: number) => void) => {
102107
cb();
103108
};
104109

105-
await expect(promisify(f, null)(1)).resolves.toBeUndefined();
110+
await expect(promisify1(f, null)(1)).resolves.toBeUndefined();
111+
});
112+
113+
it("should handle thisContext argument", async () => {
114+
const o = {
115+
val: 45,
116+
};
117+
118+
await expect(promisify1(fnAddThisVal1, o)(5)).resolves.toEqual(50);
119+
});
120+
});
121+
122+
describe("promisify3", () => {
123+
it("should handle valid response in callback", async () => {
124+
const f = (a: number, b: number, c: number, cb: (err?: Error | null, res?: number) => void) => {
125+
cb(null, 2);
126+
};
127+
128+
await expect(promisify3(f, null)(1, 2, 3)).resolves.toBe(2);
129+
});
130+
131+
it("should handle error in callback", async () => {
132+
const e = new Error();
133+
const f = (a: number , b: number, c: number, cb: (err?: Error | null, res?: number) => void) => {
134+
cb(e);
135+
};
136+
137+
await expect(promisify3(f, null)(1, 2, 3)).rejects.toBe(e);
138+
});
139+
140+
it("should handle error if valid response is also present in callback", async () => {
141+
const e = new Error();
142+
const f = (a: number, b: number, c: number, cb: (err?: Error | null, res?: number) => void) => {
143+
cb(e, 2);
144+
};
145+
146+
await expect(promisify3(f, null)(1, 2, 3)).rejects.toBe(e);
147+
});
148+
149+
it("should handle callback called without arguments", async () => {
150+
const f = (a: number, b: number, c: number, cb: (err?: Error | null, res?: number) => void) => {
151+
cb();
152+
};
153+
154+
await expect(promisify3(f, null)(1, 2, 3)).resolves.toBeUndefined();
106155
});
107156

108157
it("should handle thisContext argument", async () => {
109158
const o = {
110159
val: 45,
111160
};
112161

113-
await expect(promisify(fnAddThisVal, o)(5)).resolves.toEqual(50);
162+
await expect(promisify3(fnAddThisVal3, o)(5, 10, 15)).resolves.toEqual(75);
114163
});
115164
});
116165

0 commit comments

Comments
 (0)