Skip to content

Commit 20f8da7

Browse files
feat: add support for dgraph connection strings (#263)
This PR adds support to open gRPC endpoints using "connection strings", specifically the `dgraph.Open()` function.
1 parent 240d17d commit 20f8da7

File tree

14 files changed

+454
-168
lines changed

14 files changed

+454
-168
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Thumbs.db
3434

3535
# Dgraph
3636

37-
37+
dgraph-local-data/
3838
tls/
3939
p/
4040
w/

README.md

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,40 @@ of using the Dgraph JavaScript client. Follow the instructions in the README of
5151

5252
### Creating a Client
5353

54-
A `DgraphClient` object can be initialised by passing it a list of `DgraphClientStub` clients as
55-
variadic arguments. Connecting to multiple Dgraph servers in the same cluster allows for better
56-
distribution of workload.
54+
#### Connection Strings
5755

58-
The following code snippet shows just one connection.
56+
The dgraph-js supports connecting to a Dgraph cluster using connection strings. Dgraph connections
57+
strings take the form `dgraph://{username:password@}host:port?args`.
58+
59+
`username` and `password` are optional. If username is provided, a password must also be present. If
60+
supplied, these credentials are used to log into a Dgraph cluster through the ACL mechanism.
61+
62+
Valid connection string args:
63+
64+
| Arg | Value | Description |
65+
| ----------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
66+
| apikey | \<key\> | a Dgraph Cloud API Key |
67+
| bearertoken | \<token\> | an access token |
68+
| sslmode | disable \| require \| verify-ca | TLS option, the default is `disable`. If `verify-ca` is set, the TLS certificate configured in the Dgraph cluster must be from a valid certificate authority. |
69+
70+
## Some example connection strings
71+
72+
| Value | Explanation |
73+
| ------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------- |
74+
| dgraph://localhost:9080 | Connect to localhost, no ACL, no TLS |
75+
| dgraph://sally:supersecret@dg.example.com:443?sslmode=verify-ca | Connect to remote server, use ACL and require TLS and a valid certificate from a CA |
76+
| 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 |
77+
| dgraph://foo-bar.grpc.hypermode.com?sslmode=verify-ca&bearertoken=\<some access token\> | Connect to a Dgraph cluster protected by a secure gateway |
78+
79+
Using the `open` function with a connection string:
5980

6081
```js
61-
const dgraph = require("dgraph-js")
62-
const grpc = require("@grpc/grpc-js")
63-
64-
const clientStub = new dgraph.DgraphClientStub(
65-
// addr: optional, default: "localhost:9080"
66-
"localhost:9080",
67-
// credentials: optional, default: grpc.credentials.createInsecure()
68-
grpc.credentials.createInsecure(),
69-
)
70-
const dgraphClient = new dgraph.DgraphClient(clientStub)
82+
// open a connection to an ACL-enabled, non-TLS cluster and login as groot
83+
const client = await dgraph.open("dgraph://groot:password@localhost:8090")
84+
// Use the client
85+
86+
// this will close all the client stubs
87+
client.close()
7188
```
7289

7390
To facilitate debugging, [debug mode](#debug-mode) can be enabled for a client.
@@ -89,25 +106,6 @@ In the example above, the client logs into namespace `123` using username `groot
89106
`password`. Once logged in, the client can perform all the operations allowed to the `groot` user of
90107
namespace `123`.
91108

92-
### Creating a Client for Dgraph Cloud Endpoint
93-
94-
If you want to connect to Dgraph running on your [Dgraph Cloud](https://cloud.dgraph.io) instance,
95-
then all you need is the URL of your Dgraph Cloud endpoint and the API key. You can get a client
96-
using them as follows:
97-
98-
```js
99-
const dgraph = require("dgraph-js")
100-
101-
const clientStub = dgraph.clientStubFromCloudEndpoint(
102-
"https://frozen-mango.eu-central-1.aws.cloud.dgraph.io/graphql",
103-
"<api-key>",
104-
)
105-
const dgraphClient = new dgraph.DgraphClient(clientStub)
106-
```
107-
108-
**Note:** the `clientStubFromSlashGraphQLEndpoint` method is deprecated and will be removed in the
109-
next release. Instead use `clientStubFromCloudEndpoint` method.
110-
111109
### Altering the Database
112110

113111
To set the schema, create an `Operation` object, set the schema and pass it to
@@ -376,27 +374,21 @@ try {
376374

377375
### Cleanup Resources
378376

379-
To cleanup resources, you have to call `DgraphClientStub#close()` individually for all the instances
380-
of `DgraphClientStub`.
377+
To cleanup resources, you have to call `close()`.
381378

382379
```js
383380
const SERVER_ADDR = "localhost:9080"
384381
const SERVER_CREDENTIALS = grpc.credentials.createInsecure()
385382

386-
// Create instances of DgraphClientStub.
387-
const stub1 = new dgraph.DgraphClientStub(SERVER_ADDR, SERVER_CREDENTIALS)
388-
const stub2 = new dgraph.DgraphClientStub(SERVER_ADDR, SERVER_CREDENTIALS)
389-
390-
// Create an instance of DgraphClient.
391-
const dgraphClient = new dgraph.DgraphClient(stub1, stub2)
383+
// Create instances of DgraphClient.
384+
const client = await dgraph.open("dgraph://groot:password@${SERVER_ADDR}")
392385

393386
// ...
394387
// Use dgraphClient
395388
// ...
396389

397-
// Cleanup resources by closing all client stubs.
398-
stub1.close()
399-
stub2.close()
390+
// Cleanup resources by closing client stubs.
391+
client.close()
400392
```
401393

402394
### Debug mode

examples/simple/index.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
const dgraph = require("dgraph-js")
2-
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-
}
1+
import * as dgraph from "dgraph-js"
122

133
// Drop All - discard all data, schema and start from a clean slate.
144
async function dropAll(dgraphClient) {
@@ -125,8 +115,7 @@ async function queryData(dgraphClient) {
125115
}
126116

127117
async function main() {
128-
const dgraphClientStub = newClientStub()
129-
const dgraphClient = newClient(dgraphClientStub)
118+
const dgraphClient = await dgraph.open("dgraph://groot:password@localhost:9080")
130119
await dropAll(dgraphClient)
131120
await setSchema(dgraphClient)
132121
await createData(dgraphClient)
@@ -137,7 +126,7 @@ async function main() {
137126
await queryData(dgraphClient)
138127

139128
// Close the client stub.
140-
dgraphClientStub.close()
129+
dgraphClient.close()
141130
}
142131

143132
main()

examples/simple/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "simple",
3+
"type": "module",
34
"dependencies": {
45
"dgraph-js": "^24.1.0",
56
"@grpc/grpc-js": "1.8.22"

0 commit comments

Comments
 (0)