|
| 1 | +--- |
| 2 | +title: v25 Preview |
| 3 | +description: |
| 4 | +--- |
| 5 | + |
| 6 | +import DgraphWIP from "/snippets/dgraph-wip.mdx" |
| 7 | + |
| 8 | +<DgraphWIP /> |
| 9 | + |
| 10 | +[](https://pkg.go.dev/github.com/dgraph-io/dgo/v250) |
| 11 | + |
| 12 | +These APIs are released as part of the preview of Dgraph v25. They are |
| 13 | +_experimental_ APIs and subject to change before general availability. Please |
| 14 | +share feedback via [Discord](https://discord.hypermode.com) or |
| 15 | +[GitHub](https://github.com/hypermodeinc/dgo). |
| 16 | + |
| 17 | +## Opening a connection |
| 18 | + |
| 19 | +The dgo package supports connecting to a Dgraph cluster using connection |
| 20 | +strings. Dgraph connection strings take the form |
| 21 | +`dgraph://{username:password@}host:port?args`. |
| 22 | + |
| 23 | +`username` and `password` are optional. If username is provided, a password must |
| 24 | +also be present. If supplied, these credentials are used to log into a Dgraph |
| 25 | +cluster through the ACL mechanism. |
| 26 | + |
| 27 | +Valid connection string arguments: |
| 28 | + |
| 29 | +| Arg | Value | Description | |
| 30 | +| ----------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 31 | +| bearertoken | \<token\> | an access token | |
| 32 | +| 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. | |
| 33 | + |
| 34 | +Using the `Open` function with a connection string: |
| 35 | + |
| 36 | +```go |
| 37 | +// open a connection to an ACL-enabled, non-TLS cluster and login as groot |
| 38 | +client, err := dgo.Open("dgraph://groot:password@localhost:9080") |
| 39 | +// Check error |
| 40 | +defer client.Close() |
| 41 | +// Use the clients |
| 42 | +``` |
| 43 | + |
| 44 | +## Advanced client creation |
| 45 | + |
| 46 | +For more control, you can create a client using the `NewClient` function. |
| 47 | + |
| 48 | +```go |
| 49 | +client, err := dgo.NewClient("localhost:9080", |
| 50 | + // add Dgraph ACL credentials |
| 51 | + dgo.WithACLCreds("groot", "password"), |
| 52 | + // add insecure transport credentials |
| 53 | + dgo.WithGrpcOption(grpc.WithTransportCredentials(insecure.NewCredentials())), |
| 54 | +) |
| 55 | +// Check error |
| 56 | +defer client.Close() |
| 57 | +// Use the client |
| 58 | +``` |
| 59 | + |
| 60 | +You can connect to multiple alphas using `NewRoundRobinClient`. |
| 61 | + |
| 62 | +```go |
| 63 | +client, err := dgo.NewRoundRobinClient([]string{"localhost:9181", "localhost:9182", "localhost:9183"}, |
| 64 | + // add Dgraph ACL credentials |
| 65 | + dgo.WithACLCreds("groot", "password"), |
| 66 | + // add insecure transport credentials |
| 67 | + dgo.WithGrpcOption(grpc.WithTransportCredentials(insecure.NewCredentials())), |
| 68 | +) |
| 69 | +// Check error |
| 70 | +defer client.Close() |
| 71 | +// Use the client |
| 72 | +``` |
| 73 | + |
| 74 | +## Connecting to Hypermode graph |
| 75 | + |
| 76 | +You can use either `Open` or `NewClient` to connect to a Hypermode Graph. |
| 77 | + |
| 78 | +Using `Open` with a connection string: |
| 79 | + |
| 80 | +```go |
| 81 | +client, err := dgo.Open("foo-bar.grpc.hypermode.com?sslmode=verify-ca&bearertoken=some-bearer-token") |
| 82 | +// Check error |
| 83 | +defer client.Close() |
| 84 | +``` |
| 85 | + |
| 86 | +Using `NewClient`: |
| 87 | + |
| 88 | +```go |
| 89 | +client, err := dgo.NewClient("foo-bar.grpc.hypermode.com:443", |
| 90 | + dgo.WithBearerToken("some-bearer-token"), |
| 91 | + dgo.WithSystemCertPool(), |
| 92 | +) |
| 93 | +// Check error |
| 94 | +defer client.Close() |
| 95 | +``` |
| 96 | + |
| 97 | +## Dropping all data |
| 98 | + |
| 99 | +In order to drop all data in the Dgraph cluster and start fresh, use the |
| 100 | +`DropAllNamespaces` function. |
| 101 | + |
| 102 | +```go |
| 103 | +err := client.DropAllNamespaces(context.TODO()) |
| 104 | +// Handle error |
| 105 | +``` |
| 106 | + |
| 107 | +## Set schema |
| 108 | + |
| 109 | +To set the schema, use the `SetSchema` function. |
| 110 | + |
| 111 | +```go |
| 112 | +sch := ` |
| 113 | + name: string @index(exact) . |
| 114 | + email: string @index(exact) @unique . |
| 115 | + age: int . |
| 116 | +` |
| 117 | +err := client.SetSchema(context.TODO(), dgo.RootNamespace, sch) |
| 118 | +// Handle error |
| 119 | +``` |
| 120 | + |
| 121 | +## Running a mutation |
| 122 | + |
| 123 | +To run a mutation, use the `RunDQL` function. |
| 124 | + |
| 125 | +```go |
| 126 | +mutationDQL := `{ |
| 127 | + set { |
| 128 | + _:alice <name> "Alice" . |
| 129 | + _:alice <email> "alice@example.com" . |
| 130 | + _:alice <age> "29" . |
| 131 | + } |
| 132 | +}` |
| 133 | +resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, mutationDQL) |
| 134 | +// Handle error |
| 135 | +// Print map of blank UIDs |
| 136 | +fmt.Printf("%+v\n", resp.BlankUids) |
| 137 | +``` |
| 138 | + |
| 139 | +## Running a query |
| 140 | + |
| 141 | +To run a query, use the same `RunDQL` function. |
| 142 | + |
| 143 | +```go |
| 144 | +queryDQL := `{ |
| 145 | + alice(func: eq(name, "Alice")) { |
| 146 | + name |
| 147 | + email |
| 148 | + age |
| 149 | + } |
| 150 | +}` |
| 151 | +resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, queryDQL) |
| 152 | +// Handle error |
| 153 | +fmt.Printf("%s\n", resp.QueryResult) |
| 154 | +``` |
| 155 | + |
| 156 | +## Running a query with variables |
| 157 | + |
| 158 | +To run a query with variables, using `RunDQLWithVars`. |
| 159 | + |
| 160 | +```go |
| 161 | +queryDQL = `query Alice($name: string) { |
| 162 | + alice(func: eq(name, $name)) { |
| 163 | + name |
| 164 | + email |
| 165 | + age |
| 166 | + } |
| 167 | +}` |
| 168 | +vars := map[string]string{"$name": "Alice"} |
| 169 | +resp, err := client.RunDQLWithVars(context.TODO(), dgo.RootNamespace, queryDQL, vars) |
| 170 | +// Handle error |
| 171 | +fmt.Printf("%s\n", resp.QueryResult) |
| 172 | +``` |
| 173 | + |
| 174 | +## Running a best effort query |
| 175 | + |
| 176 | +To run a `BestEffort` query, use the same `RunDQL` function with `TxnOption`. |
| 177 | + |
| 178 | +```go |
| 179 | +queryDQL := `{ |
| 180 | + alice(func: eq(name, "Alice")) { |
| 181 | + name |
| 182 | + email |
| 183 | + age |
| 184 | + } |
| 185 | +}` |
| 186 | +resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, queryDQL, dgo.WithBestEffort()) |
| 187 | +// Handle error |
| 188 | +fmt.Printf("%s\n", resp.QueryResult) |
| 189 | +``` |
| 190 | + |
| 191 | +## Running a read-only query |
| 192 | + |
| 193 | +To run a `ReadOnly` query, use the same `RunDQL` function with `TxnOption`. |
| 194 | + |
| 195 | +```go |
| 196 | +queryDQL := `{ |
| 197 | + alice(func: eq(name, "Alice")) { |
| 198 | + name |
| 199 | + email |
| 200 | + age |
| 201 | + } |
| 202 | +}` |
| 203 | +resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, queryDQL, dgo.WithReadOnly()) |
| 204 | +// Handle error |
| 205 | +fmt.Printf("%s\n", resp.QueryResult) |
| 206 | +``` |
| 207 | + |
| 208 | +## Running a query with rdf response |
| 209 | + |
| 210 | +To get the query response in RDF format instead of JSON format, use the |
| 211 | +following `TxnOption`. |
| 212 | + |
| 213 | +```go |
| 214 | +queryDQL := `{ |
| 215 | + alice(func: eq(name, "Alice")) { |
| 216 | + name |
| 217 | + email |
| 218 | + age |
| 219 | + } |
| 220 | +}` |
| 221 | +resp, err = client.RunDQL(context.TODO(), dgo.RootNamespace, queryDQL, dgo.WithResponseFormat(api_v25.RespFormat_RDF)) |
| 222 | +// Handle error |
| 223 | +fmt.Printf("%s\n", resp.QueryResult) |
| 224 | +``` |
| 225 | + |
| 226 | +## Running an Upsert |
| 227 | + |
| 228 | +The `RunDQL` function also allows you to run upserts as well. |
| 229 | + |
| 230 | +```go |
| 231 | +upsertQuery := `upsert { |
| 232 | + query { |
| 233 | + user as var(func: eq(email, "alice@example.com")) |
| 234 | + } |
| 235 | + mutation { |
| 236 | + set { |
| 237 | + uid(user) <age> "30" . |
| 238 | + uid(user) <name> "Alice Sayum" . |
| 239 | + } |
| 240 | + } |
| 241 | +}` |
| 242 | +resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, upsertQuery) |
| 243 | +// Handle error |
| 244 | +fmt.Printf("%s\n", resp.QueryResult) |
| 245 | +fmt.Printf("%+v\n", resp.BlankUids) |
| 246 | +``` |
| 247 | + |
| 248 | +## Running a conditional upsert |
| 249 | + |
| 250 | +```go |
| 251 | +upsertQuery := `upsert { |
| 252 | + query { |
| 253 | + user as var(func: eq(email, "alice@example.com")) |
| 254 | + } |
| 255 | + mutation @if(eq(len(user), 1)) { |
| 256 | + set { |
| 257 | + uid(user) <age> "30" . |
| 258 | + uid(user) <name> "Alice Sayum" . |
| 259 | + } |
| 260 | + } |
| 261 | +}` |
| 262 | +resp, err := client.RunDQL(context.TODO(), dgo.RootNamespace, upsertQuery) |
| 263 | +// Handle error |
| 264 | +fmt.Printf("%s\n", resp.QueryResult) |
| 265 | +``` |
| 266 | + |
| 267 | +## Creating a new namespace |
| 268 | + |
| 269 | +Dgraph v25 supports namespaces that have names. You can create one using the dgo |
| 270 | +client. |
| 271 | + |
| 272 | +```go |
| 273 | +err := client.CreateNamespace(context.TODO(), "finance-graph") |
| 274 | +// Handle error |
| 275 | +``` |
| 276 | + |
| 277 | +You can now pass this name to `SetSchema`, `RunDQL` or similar functions. |
| 278 | + |
| 279 | +## Dropping a Namespace |
| 280 | + |
| 281 | +To drop a namespace: |
| 282 | + |
| 283 | +```go |
| 284 | +err := client.DropNamespace(context.TODO(), "finance-graph") |
| 285 | +// Handle error |
| 286 | +``` |
| 287 | + |
| 288 | +## Rename a Namespace |
| 289 | + |
| 290 | +A namespace can be renamed as follows. |
| 291 | + |
| 292 | +```go |
| 293 | +err := client.RenameNamespace(context.TODO(), "finance-graph", "new-finance-graph") |
| 294 | +// Handle error |
| 295 | +``` |
| 296 | + |
| 297 | +## List all namespaces |
| 298 | + |
| 299 | +```go |
| 300 | +namespaces, err := client.ListNamespaces(context.TODO()) |
| 301 | +// Handle error |
| 302 | +fmt.Printf("%+v\n", namespaces) |
| 303 | +``` |
0 commit comments