Skip to content

Commit 3638e05

Browse files
resolved review comments
1 parent b9a4b73 commit 3638e05

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ Valid connection string args:
7676
| dgraph://foo-bar.grpc.us-west-2.aws.cloud.dgraph.io:443?sslmode=verify-ca&apikey=\<your-api-connection-key\> | Connect to a Dgraph Cloud cluster |
7777
| dgraph://foo-bar.grpc.hypermode.com?sslmode=verify-ca&bearertoken=\<some access token\> | Connect to a Dgraph cluster protected by a secure gateway |
7878

79-
Using the `Open` function with a connection string:
79+
Using the `open` function with a connection string:
8080

8181
```js
8282
// open a connection to an ACL-enabled, non-TLS cluster and login as groot
83-
const { client, closeStub } = dgraph.Open("dgraph://groot:password@localhost:8090")
83+
const client = dgraph.open("dgraph://groot:password@localhost:8090")
8484
// Use the client
8585

86-
closeStub()
86+
// this will close all the client stubs
87+
client.close()
8788
```
8889

8990
To facilitate debugging, [debug mode](#debug-mode) can be enabled for a client.
@@ -380,14 +381,14 @@ const SERVER_ADDR = "localhost:9080"
380381
const SERVER_CREDENTIALS = grpc.credentials.createInsecure()
381382

382383
// Create instances of DgraphClient.
383-
const { client, closeStub } = dgraph.Open("dgraph://groot:password@${SERVER_ADDR}")
384+
const client = dgraph.open("dgraph://groot:password@${SERVER_ADDR}")
384385

385386
// ...
386387
// Use dgraphClient
387388
// ...
388389

389390
// Cleanup resources by closing client stubs.
390-
closeStub()
391+
client.close()
391392
```
392393

393394
### Debug mode

examples/simple/index.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
const dgraph = require("dgraph-js")
22

3-
// Create a client stub.
4-
function newClientStub() {
5-
return new dgraph.DgraphClientStub("localhost:9080")
6-
}
7-
8-
// Create a client.
9-
function newClient(clientStub) {
10-
return new dgraph.DgraphClient(clientStub)
11-
}
12-
133
// Drop All - discard all data, schema and start from a clean slate.
144
async function dropAll(dgraphClient) {
155
const op = new dgraph.Operation()
@@ -125,7 +115,7 @@ async function queryData(dgraphClient) {
125115
}
126116

127117
async function main() {
128-
const { dgraphClient, closeStub } = dgraph.Open()
118+
const dgraphClient = dgraph.open()
129119
await dropAll(dgraphClient)
130120
await setSchema(dgraphClient)
131121
await createData(dgraphClient)
@@ -136,7 +126,7 @@ async function main() {
136126
await queryData(dgraphClient)
137127

138128
// Close the client stub.
139-
closeStub()
129+
dgraphClient.close()
140130
}
141131

142132
main()

src/client.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ export class DgraphClient {
102102
public anyClient(): DgraphClientStub {
103103
return this.clients[Math.floor(Math.random() * this.clients.length)]
104104
}
105+
106+
public close(): void {
107+
this.clients.forEach((clientStub) => {
108+
try {
109+
clientStub.close() // Call the close method on each client stub
110+
console.log("Closed client stub successfully")
111+
} catch (error) {
112+
console.error("Failed to close client stub:", error)
113+
}
114+
})
115+
}
105116
}
106117

107118
// isJwtExpired returns true if the error indicates that the jwt has expired.
@@ -157,9 +168,7 @@ function addBearerTokenToCredentials(
157168
return grpc.credentials.combineChannelCredentials(baseCreds, metaCreds)
158169
}
159170

160-
export async function Open(
161-
connStr: string,
162-
): Promise<{ client: DgraphClient; closeStub: () => void }> {
171+
export async function open(connStr: string): Promise<DgraphClient> {
163172
const parsedUrl = new URL(connStr)
164173
if (parsedUrl.protocol !== dgraphScheme) {
165174
throw new Error("Invalid scheme: must start with dgraph://")
@@ -229,8 +238,5 @@ export async function Open(
229238
}
230239
}
231240

232-
return {
233-
client: new DgraphClient(clientStub),
234-
closeStub: () => clientStub.close(),
235-
}
241+
return new DgraphClient(clientStub)
236242
}

tests/integration/connect.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { SERVER_ADDR } from "../helper"
1010
describe("open function", () => {
1111
it("should connect with authentication and execute a query", async () => {
1212
const url = `dgraph://groot:password@${SERVER_ADDR}`
13-
const { client, closeStub } = await dgraph.Open(url)
13+
const client = await dgraph.open(url)
1414
const query = `
1515
{
1616
me(func: uid(1)) {
@@ -25,45 +25,45 @@ describe("open function", () => {
2525
expect(response).not.toBeNull()
2626
const parsedJson = response.getJson() // No need for JSON.parse
2727
expect(parsedJson.me[0].uid).toBe("0x1")
28-
closeStub()
28+
client.close()
2929
})
3030

3131
it("should throw an error for invalid scheme", async () => {
3232
const invalidUrl = `http://${SERVER_ADDR}`
33-
await expect(async () => dgraph.Open(invalidUrl)).rejects.toThrowError(
33+
await expect(async () => dgraph.open(invalidUrl)).rejects.toThrowError(
3434
"Invalid scheme: must start with dgraph://",
3535
)
3636
})
3737

3838
it("should throw an error for missing hostname", async () => {
3939
const invalidUrl = `dgraph://:9081`
40-
await expect(async () => dgraph.Open(invalidUrl)).rejects.toThrowError("Invalid URL")
40+
await expect(async () => dgraph.open(invalidUrl)).rejects.toThrowError("Invalid URL")
4141
})
4242

4343
it("should throw an error for missing port", async () => {
4444
const invalidUrl = `dgraph://localhost`
45-
await expect(async () => await dgraph.Open(invalidUrl)).rejects.toThrowError(
45+
await expect(async () => await dgraph.open(invalidUrl)).rejects.toThrowError(
4646
"Invalid connection string: port required",
4747
)
4848
})
4949

5050
it("should throw an error for username without password", async () => {
5151
const invalidUrl = `dgraph://groot@${SERVER_ADDR}`
52-
await expect(async () => await dgraph.Open(invalidUrl)).rejects.toThrowError(
52+
await expect(async () => await dgraph.open(invalidUrl)).rejects.toThrowError(
5353
"Invalid connection string: password required when username is provided",
5454
)
5555
})
5656

5757
it("should throw an error for unsupported sslmode", async () => {
5858
const invalidUrl = `dgraph://${SERVER_ADDR}?sslmode=invalidsllmode`
59-
await expect(async () => await dgraph.Open(invalidUrl)).rejects.toThrowError(
59+
await expect(async () => await dgraph.open(invalidUrl)).rejects.toThrowError(
6060
"Invalid SSL mode: invalidsllmode (must be one of disable, require, verify-ca)",
6161
)
6262
})
6363

6464
it("should fail login with invalid credentials", async () => {
6565
const invalidUrl = `dgraph://groot:wrongpassword@${SERVER_ADDR}`
66-
await expect(async () => await dgraph.Open(invalidUrl)).rejects.toThrowError(
66+
await expect(async () => await dgraph.open(invalidUrl)).rejects.toThrowError(
6767
"Failed to sign in user:",
6868
)
6969
})

0 commit comments

Comments
 (0)