@@ -51,24 +51,38 @@ 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 ` .
59
58
60
- ``` 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)
71
- ```
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: | Value | Explanation | |
71
+
72
+ | ----------------------------------------------------------------------------------- | |
73
+ dgraph://localhost:9080 | Connect to localhost, no ACL, no TLS | |
74
+ dgraph://sally: supersecret @dg.example.com:443?sslmode=verify-ca | Connect to remote server, use ACL
75
+ 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\>
77
+ | Connect to a Dgraph Cloud cluster | |
78
+ dgraph://foo-bar.grpc.hypermode.com?sslmode=verify-ca&bearertoken=\< some access token\> | Connect to
79
+ a Dgraph cluster protected by a secure gateway |
80
+
81
+ Using the ` Open ` function with a connection string: // open a connection to an ACL-enabled, non-TLS
82
+ cluster and login as groot const {client,closeStub} =
83
+ dgraph.Open("dgraph://groot: password @localhost:8090")
84
+
85
+ ````
72
86
73
87
To facilitate debugging, [debug mode](#debug-mode) can be enabled for a client.
74
88
@@ -83,31 +97,12 @@ In order to create a JavaScript client, and make the client login into namespace
83
97
```js
84
98
const dgraphClientStub = new dgraph.DgraphClientStub("localhost:9080")
85
99
await dgraphClientStub.loginIntoNamespace("groot", "password", 123) // where 123 is the namespaceId
86
- ```
100
+ ````
87
101
88
102
In the example above, the client logs into namespace ` 123 ` using username ` groot ` and password
89
103
` password ` . Once logged in, the client can perform all the operations allowed to the ` groot ` user of
90
104
namespace ` 123 ` .
91
105
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
106
### Altering the Database
112
107
113
108
To set the schema, create an ` Operation ` object, set the schema and pass it to
@@ -376,27 +371,21 @@ try {
376
371
377
372
### Cleanup Resources
378
373
379
- To cleanup resources, you have to call ` DgraphClientStub#close() ` individually for all the instances
380
- of ` DgraphClientStub ` .
374
+ To cleanup resources, you have to call ` close() ` .
381
375
382
376
``` js
383
377
const SERVER_ADDR = " localhost:9080"
384
378
const SERVER_CREDENTIALS = grpc .credentials .createInsecure ()
385
379
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)
380
+ // Create instances of DgraphClient.
381
+ const { client , closeStub } = dgraph .Open (" dgraph://groot:password@${SERVER_ADDR}" )
392
382
393
383
// ...
394
384
// Use dgraphClient
395
385
// ...
396
386
397
- // Cleanup resources by closing all client stubs.
398
- stub1 .close ()
399
- stub2 .close ()
387
+ // Cleanup resources by closing client stubs.
388
+ closeStub ()
400
389
```
401
390
402
391
### Debug mode
0 commit comments