Skip to content

Commit 5832d46

Browse files
committed
Add option parameter to client and txn methods
1 parent ab5dfe7 commit 5832d46

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/client.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import * as messages from "../generated/api_pb";
1+
import * as grpc from "grpc";
2+
3+
import * as messages from "../generated/api_pb";
24

35
import { DgraphClientStub } from "./clientStub";
46
import { ERR_NO_CLIENTS } from "./errors";
@@ -39,11 +41,11 @@ export class DgraphClient {
3941
*
4042
* 3. Drop the database.
4143
*/
42-
public async alter(op: messages.Operation): Promise<types.Payload> {
44+
public async alter(op: messages.Operation, options?: grpc.CallOptions | null): Promise<types.Payload> {
4345
this.debug(`Alter request:\n${stringifyMessage(op)}`);
4446

4547
const c = this.anyClient();
46-
const pl = types.createPayload(await c.alter(op));
48+
const pl = types.createPayload(await c.alter(op, options));
4749
this.debug(`Alter response:\n${stringifyMessage(pl)}`);
4850

4951
return pl;

src/txn.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as grpc from "grpc";
2+
13
import * as messages from "../generated/api_pb";
24

35
import { DgraphClient } from "./client";
@@ -41,8 +43,8 @@ export class Txn {
4143
* need to be made in the same transaction, it's convenient to chain the method,
4244
* e.g. client.newTxn().query("...").
4345
*/
44-
public query(q: string): Promise<types.Response> {
45-
return this.queryWithVars(q);
46+
public query(q: string, options?: grpc.CallOptions | null): Promise<types.Response> {
47+
return this.queryWithVars(q, null, options);
4648
}
4749

4850
/**
@@ -52,6 +54,7 @@ export class Txn {
5254
public async queryWithVars(
5355
q: string,
5456
vars?: { [k: string]: any } | null, // tslint:disable-line no-any
57+
options?: grpc.CallOptions | null,
5558
): Promise<types.Response> {
5659
if (this.finished) {
5760
this.dc.debug(`Query request (ERR_FINISHED):\nquery = ${q}\nvars = ${vars}`);
@@ -74,7 +77,7 @@ export class Txn {
7477
this.dc.debug(`Query request:\n${stringifyMessage(req)}`);
7578

7679
const c = this.dc.anyClient();
77-
const res = types.createResponse(await c.query(req));
80+
const res = types.createResponse(await c.query(req, options));
7881
this.mergeContext(res.getTxn());
7982
this.dc.debug(`Query response:\n${stringifyMessage(res)}`);
8083

@@ -93,7 +96,7 @@ export class Txn {
9396
* If the mutation fails, then the transaction is discarded and all future
9497
* operations on it will fail.
9598
*/
96-
public async mutate(mu: types.Mutation): Promise<messages.Assigned> {
99+
public async mutate(mu: types.Mutation, options?: grpc.CallOptions | null): Promise<messages.Assigned> {
97100
if (this.finished) {
98101
this.dc.debug(`Mutate request (ERR_FINISHED):\nmutation = ${stringifyMessage(mu)}`);
99102
throw ERR_FINISHED;
@@ -106,7 +109,7 @@ export class Txn {
106109
let ag: messages.Assigned;
107110
const c = this.dc.anyClient();
108111
try {
109-
ag = await c.mutate(<messages.Mutation>mu);
112+
ag = await c.mutate(<messages.Mutation>mu, options);
110113
} catch (e) {
111114
// Since a mutation error occurred, the txn should no longer be used (some
112115
// mutations could have applied but not others, but we don't know which ones).
@@ -142,7 +145,7 @@ export class Txn {
142145
* It's up to the user to decide if they wish to retry. In this case, the user
143146
* should create a new transaction.
144147
*/
145-
public async commit(): Promise<void> {
148+
public async commit(options?: grpc.CallOptions | null): Promise<void> {
146149
if (this.finished) {
147150
throw ERR_FINISHED;
148151
}
@@ -154,7 +157,7 @@ export class Txn {
154157

155158
const c = this.dc.anyClient();
156159
try {
157-
await c.commitOrAbort(this.ctx);
160+
await c.commitOrAbort(this.ctx, options);
158161
} catch (e) {
159162
throw isAbortedError(e) ? ERR_ABORTED : e;
160163
}
@@ -170,7 +173,7 @@ export class Txn {
170173
* is unavailable. In these cases, the server will eventually do the transaction
171174
* clean up.
172175
*/
173-
public async discard(): Promise<void> {
176+
public async discard(options?: grpc.CallOptions | null): Promise<void> {
174177
if (this.finished) {
175178
return;
176179
}
@@ -182,7 +185,7 @@ export class Txn {
182185

183186
this.ctx.setAborted(true);
184187
const c = this.dc.anyClient();
185-
await c.commitOrAbort(this.ctx);
188+
await c.commitOrAbort(this.ctx, options);
186189
}
187190

188191
private mergeContext(src?: messages.TxnContext | null): void {

0 commit comments

Comments
 (0)