@@ -51,23 +51,40 @@ of using the Dgraph JavaScript client. Follow the instructions in the README of
51
51
52
52
### Creating a Client
53
53
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
57
55
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:
59
80
60
81
``` 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 ()
71
88
```
72
89
73
90
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
89
106
` password ` . Once logged in, the client can perform all the operations allowed to the ` groot ` user of
90
107
namespace ` 123 ` .
91
108
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
-
111
109
### Altering the Database
112
110
113
111
To set the schema, create an ` Operation ` object, set the schema and pass it to
@@ -376,27 +374,21 @@ try {
376
374
377
375
### Cleanup Resources
378
376
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() ` .
381
378
382
379
``` js
383
380
const SERVER_ADDR = " localhost:9080"
384
381
const SERVER_CREDENTIALS = grpc .credentials .createInsecure ()
385
382
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}" )
392
385
393
386
// ...
394
387
// Use dgraphClient
395
388
// ...
396
389
397
- // Cleanup resources by closing all client stubs.
398
- stub1 .close ()
399
- stub2 .close ()
390
+ // Cleanup resources by closing client stubs.
391
+ client .close ()
400
392
```
401
393
402
394
### Debug mode
0 commit comments