Skip to content

Commit 1e13066

Browse files
authored
move v2 APIs into v1 APIs (#273)
1 parent 8fb2c71 commit 1e13066

File tree

20 files changed

+1877
-3447
lines changed

20 files changed

+1877
-3447
lines changed

.trunk/trunk.yaml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
version: 0.1
44

55
cli:
6-
version: 1.22.12
6+
version: 1.25.0
77

88
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
99
plugins:
1010
sources:
1111
- id: trunk
12-
ref: v1.6.8
12+
ref: v1.7.2
1313
uri: https://github.com/trunk-io/plugins
1414

1515
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
1616
runtimes:
1717
enabled:
1818
19-
- node@18.20.5
19+
- node@22.16.0
2020
2121

2222
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
@@ -27,18 +27,19 @@ lint:
2727
- protos/api/*.pb.go
2828
- protos/api.v2/*.pb.go
2929
enabled:
30-
31-
30+
31+
32+
3233
33-
34+
3435
- git-diff-check
3536
3637
37-
- markdownlint@0.44.0
38-
- osv-scanner@2.0.1
39-
- prettier@3.5.3
40-
- trufflehog@3.88.24
41-
38+
- markdownlint@0.45.0
39+
- osv-scanner@2.2.2
40+
- prettier@3.6.2
41+
- trufflehog@3.90.5
42+
4243
actions:
4344
enabled:
4445
- trunk-announce

README.md

Lines changed: 26 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ repository.**
1414
## Table of contents
1515

1616
- [Supported Versions](#supported-versions)
17-
- [v2 APIs](#v2-apis)
17+
- [New APIs in v25](#new-apis-in-v25)
1818
- [Connection Strings](#connection-strings)
1919
- [Advanced Client Creation](#advanced-client-creation)
20-
- [Connecting To Dgraph Cloud](#connecting-to-dgraph-cloud)
2120
- [Dropping All Data](#dropping-all-data)
2221
- [Set Schema](#set-schema)
2322
- [Running a Mutation](#running-a-mutation)
@@ -30,9 +29,8 @@ repository.**
3029
- [Running a Conditional Upsert](#running-a-conditional-upsert)
3130
- [Creating a New Namespace](#creating-a-new-namespace)
3231
- [Dropping a Namespace](#dropping-a-namespace)
33-
- [Rename a Namespace](#rename-a-namespace)
3432
- [List All Namespaces](#list-all-namespaces)
35-
- [v1 APIs](#v1-apis)
33+
- [Existing APIs](#existing-apis)
3634
- [Creating a Client](#creating-a-client)
3735
- [Login into a namespace](#login-into-a-namespace)
3836
- [Altering the database](#altering-the-database)
@@ -59,10 +57,7 @@ version of this client and their corresponding import paths.
5957
| dgraph 25.X.Y | dgo 240.X.Y | "github.com/dgraph-io/dgo/v240" |
6058
| dgraph 25.X.Y | dgo 250.X.Y | "github.com/dgraph-io/dgo/v250" |
6159

62-
## v2 APIs
63-
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.
60+
## New APIs in v25
6661

6762
### Connection Strings
6863

@@ -129,37 +124,12 @@ defer client.Close()
129124
// Use the client
130125
```
131126

132-
### Connecting To Dgraph Cloud
133-
134-
You can use either `Open` or `NewClient` to connect to Dgraph Cloud. Note `DialCloud` is marked
135-
deprecated and will be removed in later versions.
136-
137-
Using `Open` with a connection string:
138-
139-
```go
140-
client, err := dgo.Open("dgraph://foo-bar.grpc.cloud.dgraph.io:443?sslmode=verify-ca&apikey=AValidKeYFromDgrAPHCloud=")
141-
// Check error
142-
defer client.Close()
143-
```
144-
145-
Using `NewClient`:
146-
147-
```go
148-
client, err := dgo.NewClient("foo-bar.grpc.cloud.dgraph.io:443",
149-
dgo.WithDgraphAPIKey("AValidKeYFromDgrAPHCloud="),
150-
dgo.WithSystemCertPool(),
151-
)
152-
// Check error
153-
defer client.Close()
154-
```
155-
156127
### Dropping All Data
157128

158-
In order to drop all data in the Dgraph Cluster and start fresh, use the `DropAllNamespaces`
159-
function.
129+
In order to drop all data in the Dgraph Cluster and start fresh, use the `DropAll` function.
160130

161131
```go
162-
err := client.DropAllNamespaces(context.TODO())
132+
err := client.DropAll(context.TODO())
163133
// Handle error
164134
```
165135

@@ -192,7 +162,7 @@ mutationDQL := `{
192162
resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, mutationDQL)
193163
// Handle error
194164
// Print map of blank UIDs
195-
fmt.Printf("%+v\n", resp.BlankUids)
165+
fmt.Printf("%+v\n", resp.Uids)
196166
```
197167

198168
### Running a Query
@@ -209,7 +179,7 @@ queryDQL := `{
209179
}`
210180
resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, queryDQL)
211181
// Handle error
212-
fmt.Printf("%s\n", resp.QueryResult)
182+
fmt.Printf("%s\n", resp.Json)
213183
```
214184

215185
### Running a Query With Variables
@@ -227,7 +197,7 @@ queryDQL = `query Alice($name: string) {
227197
vars := map[string]string{"$name": "Alice"}
228198
resp, err := client.RunDQLWithVars(context.TODO(), dgo.RootNamespace, queryDQL, vars)
229199
// Handle error
230-
fmt.Printf("%s\n", resp.QueryResult)
200+
fmt.Printf("%s\n", resp.Json)
231201
```
232202

233203
### Running a Best Effort Query
@@ -244,7 +214,7 @@ queryDQL := `{
244214
}`
245215
resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, queryDQL, dgo.WithBestEffort())
246216
// Handle error
247-
fmt.Printf("%s\n", resp.QueryResult)
217+
fmt.Printf("%s\n", resp.Json)
248218
```
249219

250220
### Running a ReadOnly Query
@@ -261,7 +231,7 @@ queryDQL := `{
261231
}`
262232
resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, queryDQL, dgo.WithReadOnly())
263233
// Handle error
264-
fmt.Printf("%s\n", resp.QueryResult)
234+
fmt.Printf("%s\n", resp.Json)
265235
```
266236

267237
### Running a Query with RDF Response
@@ -278,7 +248,7 @@ queryDQL := `{
278248
}`
279249
resp, err = client.RunDQL(context.TODO(), dgo.RootNamespace, queryDQL, dgo.WithResponseFormat(api_v2.RespFormat_RDF))
280250
// Handle error
281-
fmt.Printf("%s\n", resp.QueryResult)
251+
fmt.Printf("%s\n", resp.Rdf)
282252
```
283253

284254
### Running an Upsert
@@ -299,8 +269,8 @@ upsertQuery := `upsert {
299269
}`
300270
resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, upsertQuery)
301271
// Handle error
302-
fmt.Printf("%s\n", resp.QueryResult)
303-
fmt.Printf("%+v\n", resp.BlankUids)
272+
fmt.Printf("%s\n", resp.Json)
273+
fmt.Printf("%+v\n", resp.Uids)
304274
```
305275

306276
### Running a Conditional Upsert
@@ -319,35 +289,24 @@ upsertQuery := `upsert {
319289
}`
320290
resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, upsertQuery)
321291
// Handle error
322-
fmt.Printf("%s\n", resp.QueryResult)
292+
fmt.Printf("%s\n", resp.Json)
323293
```
324294

325295
### Creating a New Namespace
326296

327-
Dgraph v25 supports namespaces that have names. You can create one using the dgo client.
297+
Dgraph v25 supports creating namespaces using grpc API. You can create one using the dgo client.
328298

329299
```go
330-
err := client.CreateNamespace(context.TODO(), "finance-graph")
300+
_, err := client.CreateNamespace(context.TODO())
331301
// Handle error
332302
```
333303

334-
You can now pass this name to `SetSchema`, `RunDQL` or similar functions.
335-
336304
### Dropping a Namespace
337305

338306
To drop a namespace:
339307

340308
```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")
309+
_, err := client.DropNamespace(context.TODO())
351310
// Handle error
352311
```
353312

@@ -359,7 +318,7 @@ namespaces, err := client.ListNamespaces(context.TODO())
359318
fmt.Printf("%+v\n", namespaces)
360319
```
361320

362-
## v1 APIs
321+
## Existing APIs
363322

364323
### Creating a Client
365324

@@ -517,8 +476,8 @@ q := `query all($a: string) {
517476
}
518477
}`
519478

520-
res, err := txn.QueryWithVars(ctx, q, map[string]string{"$a": "Alice"})
521-
fmt.Printf("%s\n", res.Json)
479+
resp, err := txn.QueryWithVars(ctx, q, map[string]string{"$a": "Alice"})
480+
fmt.Printf("%s\n", resp.Json)
522481
```
523482

524483
You can also use `txn.Do` function to run a query.
@@ -528,9 +487,9 @@ req := &api.Request{
528487
Query: q,
529488
Vars: map[string]string{"$a": "Alice"},
530489
}
531-
res, err := txn.Do(ctx, req)
490+
resp, err := txn.Do(ctx, req)
532491
// Check error
533-
fmt.Printf("%s\n", res.Json)
492+
fmt.Printf("%s\n", resp.Json)
534493
```
535494

536495
When running a schema query for predicate `name`, the schema response is found in the `Json` field
@@ -548,9 +507,9 @@ q := `schema(pred: [name]) {
548507
lang
549508
}`
550509

551-
res, err := txn.Query(ctx, q)
510+
resp, err := txn.Query(ctx, q)
552511
// Check error
553-
fmt.Printf("%s\n", res.Json)
512+
fmt.Printf("%s\n", resp.Json)
554513
```
555514

556515
### Query with RDF response
@@ -570,12 +529,12 @@ const q = `
570529
}
571530
}
572531
`
573-
res, err := txn.QueryRDF(context.Background(), q)
532+
resp, err := txn.QueryRDF(context.Background(), q)
574533
// check error
575534

576535
// <0x17> <name> "Alice" .
577536
// <0x17> <balance> 100 .
578-
fmt.Println(res.Rdf)
537+
fmt.Println(resp.Rdf)
579538
```
580539

581540
`txn.QueryRDFWithVars` is also available when you need to pass values for variables used in the

alter.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package dgo
7+
8+
import (
9+
"context"
10+
11+
"github.com/dgraph-io/dgo/v250/protos/api"
12+
)
13+
14+
func (d *Dgraph) DropAll(ctx context.Context) error {
15+
req := &api.Operation{DropAll: true}
16+
return d.doAlter(ctx, req)
17+
}
18+
19+
func (d *Dgraph) DropData(ctx context.Context) error {
20+
req := &api.Operation{DropOp: api.Operation_DATA}
21+
return d.doAlter(ctx, req)
22+
}
23+
24+
func (d *Dgraph) DropPredicate(ctx context.Context, predicate string) error {
25+
req := &api.Operation{DropOp: api.Operation_ATTR, DropValue: predicate}
26+
return d.doAlter(ctx, req)
27+
}
28+
29+
func (d *Dgraph) DropType(ctx context.Context, typeName string) error {
30+
req := &api.Operation{DropOp: api.Operation_TYPE, DropValue: typeName}
31+
return d.doAlter(ctx, req)
32+
}
33+
34+
func (d *Dgraph) SetSchema(ctx context.Context, schema string) error {
35+
req := &api.Operation{Schema: schema}
36+
return d.doAlter(ctx, req)
37+
}
38+
39+
func (d *Dgraph) doAlter(ctx context.Context, req *api.Operation) error {
40+
_, err := doWithRetryLogin(ctx, d, func(dc api.DgraphClient) (*api.Payload, error) {
41+
return dc.Alter(d.getContext(ctx), req)
42+
})
43+
return err
44+
}

0 commit comments

Comments
 (0)