@@ -13,24 +13,39 @@ repository.**
13
13
14
14
## Table of contents
15
15
16
- - [ dgo] ( #dgo- )
17
- - [ Table of contents] ( #table-of-contents )
18
- - [ Supported Versions] ( #supported-versions )
19
- - [ Using a client] ( #using-a-client )
20
- - [ Creating a client] ( #creating-a-client )
21
- - [ Login into a namespace] ( #login-into-a-namespace )
22
- - [ Connecting To Dgraph Cloud] ( #connecting-to-dgraph-cloud )
23
- - [ Altering the database] ( #altering-the-database )
24
- - [ Creating a transaction] ( #creating-a-transaction )
25
- - [ Running a mutation] ( #running-a-mutation )
26
- - [ Running a query] ( #running-a-query )
27
- - [ Query with RDF response] ( #query-with-rdf-response )
28
- - [ Running an Upsert: Query + Mutation] ( #running-an-upsert-query--mutation )
29
- - [ Running Conditional Upsert] ( #running-conditional-upsert )
30
- - [ Committing a transaction] ( #committing-a-transaction )
31
- - [ Setting Metadata Headers] ( #setting-metadata-headers )
32
- - [ Development] ( #development )
33
- - [ Running tests] ( #running-tests )
16
+ - [ Supported Versions] ( #supported-versions )
17
+ - [ v25 APIs] ( #v25-apis )
18
+ - [ Connection Strings] ( #connection-strings )
19
+ - [ Advanced Client Creation] ( #advanced-client-creation )
20
+ - [ Connecting To Dgraph Cloud] ( #connecting-to-dgraph-cloud )
21
+ - [ Dropping All Data] ( #dropping-all-data )
22
+ - [ Set Schema] ( #set-schema )
23
+ - [ Running a Mutation] ( #running-a-mutation )
24
+ - [ Running a Query] ( #running-a-query )
25
+ - [ Running a Query With Variables] ( #running-a-query-with-variables )
26
+ - [ Running a Best Effort Query] ( #running-a-best-effort-query )
27
+ - [ Running a ReadOnly Query] ( #running-a-readonly-query )
28
+ - [ Running a Query with RDF Response] ( #running-a-query-with-rdf-response )
29
+ - [ Running an Upsert] ( #running-an-upsert )
30
+ - [ Running a Conditional Upsert] ( #running-a-conditional-upsert )
31
+ - [ Creating a New Namespace] ( #creating-a-new-namespace )
32
+ - [ Dropping a Namespace] ( #dropping-a-namespace )
33
+ - [ Rename a Namespace] ( #rename-a-namespace )
34
+ - [ List All Namespaces] ( #list-all-namespaces )
35
+ - [ v1 APIs] ( #v1-apis )
36
+ - [ Creating a Client] ( #creating-a-client )
37
+ - [ Login into a namespace] ( #login-into-a-namespace )
38
+ - [ Altering the database] ( #altering-the-database )
39
+ - [ Creating a transaction] ( #creating-a-transaction )
40
+ - [ Running a mutation] ( #running-a-mutation-1 )
41
+ - [ Running a query] ( #running-a-query-1 )
42
+ - [ Query with RDF response] ( #query-with-rdf-response )
43
+ - [ Running an Upsert: Query + Mutation] ( #running-an-upsert-query--mutation )
44
+ - [ Running Conditional Upsert] ( #running-conditional-upsert )
45
+ - [ Committing a transaction] ( #committing-a-transaction )
46
+ - [ Setting Metadata Headers] ( #setting-metadata-headers )
47
+ - [ Development] ( #development )
48
+ - [ Running tests] ( #running-tests )
34
49
35
50
## Supported Versions
36
51
@@ -41,19 +56,15 @@ version of this client and their corresponding import paths.
41
56
| -------------- | ----------- | ------------------------------- |
42
57
| dgraph 23.X.Y | dgo 230.X.Y | "github.com/dgraph-io/dgo/v230" |
43
58
| dgraph 24.X.Y | dgo 240.X.Y | "github.com/dgraph-io/dgo/v240" |
59
+ | dgraph 25.X.Y | dgo 240.X.Y | "github.com/dgraph-io/dgo/v240" |
44
60
| dgraph 25.X.Y | dgo 250.X.Y | "github.com/dgraph-io/dgo/v250" |
45
61
46
- Note: We have removed functions ` DialSlashEndpoint ` , ` DialSlashGraphQLEndpoint ` from ` v230.0.0 ` .
47
- ` DialCloud ` is now marked deprecated and will be removed in a future release, use either ` Open ` or
48
- ` NewClient ` (see below).
62
+ ## v25 APIs
49
63
50
- ## Using a client
64
+ These are _ experimental_ APIs that we are still making changes to. If you have any feedback, please
65
+ let us know either on Discord or GitHub.
51
66
52
- ### Creating a client
53
-
54
- Note ` NewDgraphClient ` is marked as deprecated, but is still available in this release.
55
-
56
- #### Connection Strings
67
+ ### Connection Strings
57
68
58
69
The dgo package supports connecting to a Dgraph cluster using connection strings. Dgraph connections
59
70
strings take the form ` dgraph://{username:password@}host:port?args ` .
@@ -85,40 +96,43 @@ Using the `Open` function with a connection string:
85
96
client , err := dgo.Open (" dgraph://groot:password@localhost:8090" )
86
97
// Check error
87
98
defer client.Close ()
88
- // Use the client
99
+ // Use the clients
89
100
```
90
101
91
- #### Advanced Client Creation
102
+ ### Advanced Client Creation
92
103
93
- For more control, you can create a client using the ` NewClient ` and ` NewRoundRobinClient ` functions .
104
+ For more control, you can create a client using the ` NewClient ` function .
94
105
95
106
``` go
96
- // endpoints for three alpha nodes
97
- endpoints := []string {" localhost:9180" , " localhost:9182" , " localhost:9183" }
107
+ client , err := dgo.NewClient (" localhost:9181" ,
108
+ // add Dgraph ACL credentials
109
+ dgo.WithACLCreds (" groot" , " password" ),
110
+ // add insecure transport credentials
111
+ dgo.WithGrpcOption (grpc.WithTransportCredentials (insecure.NewCredentials ())),
112
+ )
113
+ // Check error
114
+ defer client.Close ()
115
+ // Use the client
116
+ ```
98
117
99
- client , err := dgo.NewRoundRobinClient (endpoints,
118
+ You can connect to multiple alphas using ` NewRoundRobinClient ` .
119
+
120
+ ``` go
121
+ client , err := dgo.NewRoundRobinClient ([]string {" localhost:9181" , " localhost:9182" , " localhost:9183" },
100
122
// add Dgraph ACL credentials
101
123
dgo.WithACLCreds (" groot" , " password" ),
102
124
// add insecure transport credentials
103
125
dgo.WithGrpcOption (grpc.WithTransportCredentials (insecure.NewCredentials ())),
104
- // add retry policy
105
- dgo.WithGrpcOption (grpc.WithDefaultServiceConfig (` {
106
- "methodConfig": [{
107
- "retryPolicy": {
108
- "MaxAttempts": 4
109
- }
110
- }]
111
- }` )),
112
126
)
113
127
// Check error
114
128
defer client.Close ()
115
129
// Use the client
116
130
```
117
131
118
- #### Connecting To Dgraph Cloud
132
+ ### Connecting To Dgraph Cloud
119
133
120
134
You can use either ` Open ` or ` NewClient ` to connect to Dgraph Cloud. Note ` DialCloud ` is marked
121
- deprecated but is still supported in this version .
135
+ deprecated and will be removed in later versions .
122
136
123
137
Using ` Open ` with a connection string:
124
138
@@ -139,6 +153,229 @@ client, err := dgo.NewClient("foo-bar.grpc.cloud.dgraph.io:443",
139
153
defer client.Close ()
140
154
```
141
155
156
+ ### Dropping All Data
157
+
158
+ In order to drop all data in the Dgraph Cluster and start fresh, use the ` DropAllNamespaces `
159
+ function.
160
+
161
+ ``` go
162
+ err := client.DropAllNamespaces (context.TODO ())
163
+ // Handle error
164
+ ```
165
+
166
+ ### Set Schema
167
+
168
+ To set the schema, use the ` SetSchema ` function.
169
+
170
+ ``` go
171
+ sch := `
172
+ name: string @index(exact) .
173
+ email: string @index(exact) @unique .
174
+ age: int .
175
+ `
176
+ err := client.SetSchema (context.TODO (), dgo.RootNamespace , sch)
177
+ // Handle error
178
+ ```
179
+
180
+ ### Running a Mutation
181
+
182
+ To run a mutation, use the ` RunDQL ` function.
183
+
184
+ ``` go
185
+ mutationDQL := ` {
186
+ set {
187
+ _:alice <name> "Alice" .
188
+ _:alice <email> "[email protected] " .
189
+ _:alice <age> "29" .
190
+ }
191
+ }`
192
+ resp , err := client.RunDQL (context.TODO (), dgo.RootNamespace , mutationDQL)
193
+ // Handle error
194
+ // Print map of blank UIDs
195
+ fmt.Printf (" %+v \n " , resp.BlankUids )
196
+ ```
197
+
198
+ ### Running a Query
199
+
200
+ To run a query, use the same ` RunDQL ` function.
201
+
202
+ ``` go
203
+ queryDQL := ` {
204
+ alice(func: eq(name, "Alice")) {
205
+ name
206
+ email
207
+ age
208
+ }
209
+ }`
210
+ resp , err := client.RunDQL (context.TODO (), dgo.RootNamespace , queryDQL)
211
+ // Handle error
212
+ fmt.Printf (" %s \n " , resp.QueryResult )
213
+ ```
214
+
215
+ ### Running a Query With Variables
216
+
217
+ To run a query with variables, using ` RunDQLWithVars ` .
218
+
219
+ ``` go
220
+ queryDQL = ` query Alice($name: string) {
221
+ alice(func: eq(name, $name)) {
222
+ name
223
+ email
224
+ age
225
+ }
226
+ }`
227
+ vars := map [string ]string {" $name" : " Alice" }
228
+ resp , err := client.RunDQLWithVars (context.TODO (), dgo.RootNamespace , queryDQL, vars)
229
+ // Handle error
230
+ fmt.Printf (" %s \n " , resp.QueryResult )
231
+ ```
232
+
233
+ ### Running a Best Effort Query
234
+
235
+ To run a ` BestEffort ` query, use the same ` RunDQL ` function with ` TxnOption ` .
236
+
237
+ ``` go
238
+ queryDQL := ` {
239
+ alice(func: eq(name, "Alice")) {
240
+ name
241
+ email
242
+ age
243
+ }
244
+ }`
245
+ resp , err := client.RunDQL (context.TODO (), dgo.RootNamespace , queryDQL, dgo.WithBestEffort ())
246
+ // Handle error
247
+ fmt.Printf (" %s \n " , resp.QueryResult )
248
+ ```
249
+
250
+ ### Running a ReadOnly Query
251
+
252
+ To run a ` ReadOnly ` query, use the same ` RunDQL ` function with ` TxnOption ` .
253
+
254
+ ``` go
255
+ queryDQL := ` {
256
+ alice(func: eq(name, "Alice")) {
257
+ name
258
+ email
259
+ age
260
+ }
261
+ }`
262
+ resp , err := client.RunDQL (context.TODO (), dgo.RootNamespace , queryDQL, dgo.WithReadOnly ())
263
+ // Handle error
264
+ fmt.Printf (" %s \n " , resp.QueryResult )
265
+ ```
266
+
267
+ ### Running a Query with RDF Response
268
+
269
+ To get the query response in RDF format instead of JSON format, use the following ` TxnOption ` .
270
+
271
+ ``` go
272
+ queryDQL := ` {
273
+ alice(func: eq(name, "Alice")) {
274
+ name
275
+ email
276
+ age
277
+ }
278
+ }`
279
+ resp, err = client.RunDQL (context.TODO (), dgo.RootNamespace , queryDQL, dgo.WithResponseFormat (api_v25.RespFormat_RDF ))
280
+ // Handle error
281
+ fmt.Printf (" %s \n " , resp.QueryResult )
282
+ ```
283
+
284
+ ### Running an Upsert
285
+
286
+ The ` RunDQL ` function also allows you to run upserts as well.
287
+
288
+ ``` go
289
+ upsertQuery := ` upsert {
290
+ query {
291
+ user as var(func: eq(email, "[email protected] "))
292
+ }
293
+ mutation {
294
+ set {
295
+ uid(user) <age> "30" .
296
+ uid(user) <name> "Alice Sayum" .
297
+ }
298
+ }
299
+ }`
300
+ resp , err := client.RunDQL (context.TODO (), dgo.RootNamespace , upsertQuery)
301
+ // Handle error
302
+ fmt.Printf (" %s \n " , resp.QueryResult )
303
+ fmt.Printf (" %+v \n " , resp.BlankUids )
304
+ ```
305
+
306
+ ### Running a Conditional Upsert
307
+
308
+ ``` go
309
+ upsertQuery := ` upsert {
310
+ query {
311
+ user as var(func: eq(email, "[email protected] "))
312
+ }
313
+ mutation @if(eq(len(user), 1)) {
314
+ set {
315
+ uid(user) <age> "30" .
316
+ uid(user) <name> "Alice Sayum" .
317
+ }
318
+ }
319
+ }`
320
+ resp , err := client.RunDQL (context.TODO (), dgo.RootNamespace , upsertQuery)
321
+ // Handle error
322
+ fmt.Printf (" %s \n " , resp.QueryResult )
323
+ ```
324
+
325
+ ### Creating a New Namespace
326
+
327
+ Dgraph v25 supports namespaces that have names. You can create one using the dgo client.
328
+
329
+ ``` go
330
+ err := client.CreateNamespace (context.TODO (), " finance-graph" )
331
+ // Handle error
332
+ ```
333
+
334
+ You can now pass this name to ` SetSchema ` , ` RunDQL ` or similar functions.
335
+
336
+ ### Dropping a Namespace
337
+
338
+ To drop a namespace:
339
+
340
+ ``` go
341
+ err := client.DropNamespace (context.TODO (), " finance-graph" )
342
+ // Handle error
343
+ ```
344
+
345
+ ### Rename a Namespace
346
+
347
+ A namespace can be renamed as follows.
348
+
349
+ ``` go
350
+ err := client.RenameNamespace (context.TODO (), " finance-graph" , " new-finance-graph" )
351
+ // Handle error
352
+ ```
353
+
354
+ ### List All Namespaces
355
+
356
+ ``` go
357
+ namespaces , err := client.ListNamespaces (context.TODO ())
358
+ // Handle error
359
+ fmt.Printf (" %+v \n " , namespaces)
360
+ ```
361
+
362
+ ## v1 APIs
363
+
364
+ ### Creating a Client
365
+
366
+ ` dgraphClient ` object can be initialized by passing it a list of ` api.DgraphClient ` clients as
367
+ variadic arguments. Connecting to multiple Dgraph servers in the same cluster allows for better
368
+ distribution of workload.
369
+
370
+ The following code snippet shows just one connection.
371
+
372
+ ``` go
373
+ conn , err := grpc.Dial (" localhost:9080" , grpc.WithInsecure ())
374
+ // Check error
375
+ defer conn.Close ()
376
+ dgraphClient := dgo.NewDgraphClient (api.NewDgraphClient (conn))
377
+ ```
378
+
142
379
### Login into a namespace
143
380
144
381
If your server has Access Control Lists enabled (Dgraph v1.1 or above), the client must be logged in
0 commit comments