• Stars
    star
    347
  • Rank 117,703 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Official Dgraph Go client

dgo GoDoc

Official Dgraph Go client which communicates with the server using gRPC.

Before using this client, we highly recommend that you go through dgraph.io/tour and dgraph.io/docs to understand how to run and work with Dgraph.

Use Github Issues for reporting issues about this repository.

Table of contents

Supported Versions

Depending on the version of Dgraph that you are connecting to, you will have to use a different version of this client and their corresponding import paths.

Dgraph version dgo version dgo import path
dgraph 20.11.0 dgo 200.03.0 "github.com/dgraph-io/dgo/v200"
dgraph 21.X.Y dgo 210.X.Y "github.com/dgraph-io/dgo/v210"
dgraph 22.X.Y dgo 210.X.Y "github.com/dgraph-io/dgo/v210"
dgraph 23.X.Y dgo 230.X.Y "github.com/dgraph-io/dgo/v230"

Note: One of the most important API breakages from dgo v1 to v2 is in the function dgo.Txn.Mutate. This function returns an *api.Assigned value in v1 but an *api.Response in v2.

Note: We have removed functions DialSlashEndpoint, DialSlashGraphQLEndpoint from v230.0.0. Please use DialCloud instead.

Note: There is no breaking API change from v2 to v200 but we have decided to follow the CalVer Versioning Scheme.

Using a client

Creating a client

dgraphClient object can be initialized by passing it a list of api.DgraphClient clients as variadic arguments. Connecting to multiple Dgraph servers in the same cluster allows for better distribution of workload.

The following code snippet shows just one connection.

conn, err := grpc.Dial("localhost:9080", grpc.WithTransportCredentials(insecure.NewCredentials()))
// Check error
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))

The client can be configured to use gRPC compression:

dialOpts := append([]grpc.DialOption{},
	grpc.WithTransportCredentials(insecure.NewCredentials()),
	grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
d, err := grpc.Dial("localhost:9080", dialOpts...)

Login into a namespace

If your server has Access Control Lists enabled (Dgraph v1.1 or above), the client must be logged in for accessing data. Use Login endpoint:

Calling login will obtain and remember the access and refresh JWT tokens. All subsequent operations via the logged in client will send along the stored access token.

err := dgraphClient.Login(ctx, "user", "passwd")
// Check error

If your server additionally has namespaces (Dgraph v21.03 or above), use the LoginIntoNamespace API.

err := dgraphClient.LoginIntoNamespace(ctx, "user", "passwd", 0x10)
// Check error

Connecting To Dgraph Cloud

Please use the following snippet to connect to a Dgraph Cloud backend.

conn, err := dgo.DialCloud("https://your.endpoint.dgraph.io/graphql", "api-token")
// Check error
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))

Altering the database

To set the schema, create an instance of api.Operation and use the Alter endpoint.

op := &api.Operation{
  Schema: `name: string @index(exact) .`,
}
err := dgraphClient.Alter(ctx, op)
// Check error

Operation contains other fields as well, including DropAttr and DropAll. DropAll is useful if you wish to discard all the data, and start from a clean slate, without bringing the instance down. DropAttr is used to drop all the data related to a predicate.

Starting Dgraph version 20.03.0, indexes can be computed in the background. You can set RunInBackground field of the api.Operation to true before passing it to the Alter function. You can find more details here.

op := &api.Operation{
  Schema:          `name: string @index(exact) .`,
  RunInBackground: true
}
err := dgraphClient.Alter(ctx, op)

Creating a transaction

To create a transaction, call dgraphClient.NewTxn(), which returns a *dgo.Txn object. This operation incurs no network overhead.

It is a good practice to call txn.Discard(ctx) using a defer statement after it is initialized. Calling txn.Discard(ctx) after txn.Commit(ctx) is a no-op. Furthermore, txn.Discard(ctx) can be called multiple times with no additional side-effects.

txn := dgraphClient.NewTxn()
defer txn.Discard(ctx)

Read-only transactions can be created by calling c.NewReadOnlyTxn(). Read-only transactions are useful to increase read speed because they can circumvent the usual consensus protocol. Read-only transactions cannot contain mutations and trying to call txn.Commit() will result in an error. Calling txn.Discard() will be a no-op.

Running a mutation

txn.Mutate(ctx, mu) runs a mutation. It takes in a context.Context and a *api.Mutation object. You can set the data using JSON or RDF N-Quad format.

To use JSON, use the fields SetJson and DeleteJson, which accept a string representing the nodes to be added or removed respectively (either as a JSON map or a list). To use RDF, use the fields SetNquads and DelNquads, which accept a string representing the valid RDF triples (one per line) to added or removed respectively. This protobuf object also contains the Set and Del fields which accept a list of RDF triples that have already been parsed into our internal format. As such, these fields are mainly used internally and users should use the SetNquads and DelNquads instead if they are planning on using RDF.

We define a Person struct to represent a Person and marshal an instance of it to use with Mutation object.

type Person struct {
	Uid   string   `json:"uid,omitempty"`
	Name  string   `json:"name,omitempty"`
	DType []string `json:"dgraph.type,omitempty"`
}

p := Person{
	Uid:   "_:alice",
	Name:  "Alice",
	DType: []string{"Person"},
}

pb, err := json.Marshal(p)
// Check error

mu := &api.Mutation{
	SetJson: pb,
}
res, err := txn.Mutate(ctx, mu)
// Check error

For a more complete example, see Example.

Sometimes, you only want to commit a mutation, without querying anything further. In such cases, you can use mu.CommitNow = true to indicate that the mutation must be immediately committed.

Mutation can be run using txn.Do as well.

mu := &api.Mutation{
  SetJson: pb,
}
req := &api.Request{CommitNow:true, Mutations: []*api.Mutation{mu}}
res, err := txn.Do(ctx, req)
// Check error

Running a query

You can run a query by calling txn.Query(ctx, q). You will need to pass in a DQL query string. If you want to pass an additional map of any variables that you might want to set in the query, call txn.QueryWithVars(ctx, q, vars) with the variables map as third argument.

Let's run the following query with a variable $a:

q := `query all($a: string) {
    all(func: eq(name, $a)) {
      name
    }
  }`

res, err := txn.QueryWithVars(ctx, q, map[string]string{"$a": "Alice"})
fmt.Printf("%s\n", res.Json)

You can also use txn.Do function to run a query.

req := &api.Request{
  Query: q,
  Vars: map[string]string{"$a": "Alice"},
}
res, err := txn.Do(ctx, req)
// Check error
fmt.Printf("%s\n", res.Json)

When running a schema query for predicate name, the schema response is found in the Json field of api.Response as shown below:

q := `schema(pred: [name]) {
  type
  index
  reverse
  tokenizer
  list
  count
  upsert
  lang
}`

res, err := txn.Query(ctx, q)
// Check error
fmt.Printf("%s\n", res.Json)

Query with RDF response

You can get query result as a RDF response by calling txn.QueryRDF. The response would contain a Rdf field, which has the RDF encoded result.

Note: If you are querying only for uid values, use a JSON format response.

// Query the balance for Alice and Bob.
const q = `
{
	all(func: anyofterms(name, "Alice Bob")) {
		name
		balance
	}
}
`
res, err := txn.QueryRDF(context.Background(), q)
// check error

// <0x17> <name> "Alice" .
// <0x17> <balance> 100 .
fmt.Println(res.Rdf)

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

Running an Upsert: Query + Mutation

The txn.Do function allows you to run upserts consisting of one query and one mutation. Variables can be defined in the query and used in the mutation. You could also use txn.Do to perform a query followed by a mutation.

To know more about upsert, we highly recommend going through the docs at Upsert Block.

query = `
  query {
      user as var(func: eq(email, "[email protected]"))
  }`
mu := &api.Mutation{
  SetNquads: []byte(`uid(user) <email> "[email protected]" .`),
}
req := &api.Request{
  Query: query,
  Mutations: []*api.Mutation{mu},
  CommitNow:true,
}

// Update email only if matching uid found.
_, err := dg.NewTxn().Do(ctx, req)
// Check error

Running Conditional Upsert

The upsert block also allows specifying a conditional mutation block using an @if directive. The mutation is executed only when the specified condition is true. If the condition is false, the mutation is silently ignored.

See more about Conditional Upsert Here.

query = `
  query {
      user as var(func: eq(email, "[email protected]"))
  }`
mu := &api.Mutation{
  Cond: `@if(eq(len(user), 1))`, // Only mutate if "[email protected]" belongs to single user.
  SetNquads: []byte(`uid(user) <email> "[email protected]" .`),
}
req := &api.Request{
  Query: query,
  Mutations: []*api.Mutation{mu},
  CommitNow:true,
}

// Update email only if exactly one matching uid is found.
_, err := dg.NewTxn().Do(ctx, req)
// Check error

Committing a transaction

A transaction can be committed using the txn.Commit(ctx) method. If your transaction consisted solely of calls to txn.Query or txn.QueryWithVars, and no calls to txn.Mutate, then calling txn.Commit is not necessary.

An error will be returned if other transactions running concurrently modify the same data that was modified in this transaction. It is up to the user to retry transactions when they fail.

txn := dgraphClient.NewTxn()
// Perform some queries and mutations.

err := txn.Commit(ctx)
if err == y.ErrAborted {
  // Retry or handle error
}

Setting Metadata Headers

Metadata headers such as authentication tokens can be set through the context of gRPC methods. Below is an example of how to set a header named "auth-token".

// The following piece of code shows how one can set metadata with
// auth-token, to allow Alter operation, if the server requires it.
md := metadata.New(nil)
md.Append("auth-token", "the-auth-token-value")
ctx := metadata.NewOutgoingContext(context.Background(), md)
dg.Alter(ctx, &op)

Development

Running tests

Make sure you have dgraph installed in your GOPATH before you run the tests. The dgo test suite requires that a Dgraph cluster with ACL enabled be running locally. To start such a cluster, you may use the docker compose file located in the testing directory t.

docker compose -f t/docker-compose.yml up -d
# wait for cluster to be healthy
go test -v ./...
docker compose -f t/docker-compose.yml down

More Repositories

1

dgraph

The high-performance database for modern applications
Go
19,791
star
2

badger

Fast key-value DB in Go.
Go
12,961
star
3

ristretto

A high performance memory-bound Go cache
Go
5,088
star
4

dgraph-js

Official Dgraph JavaScript client
JavaScript
322
star
5

sroar

Serialized Roaring Bitmaps
Go
267
star
6

pydgraph

Official Dgraph Python client
Python
261
star
7

dgraph4j

Official Dgraph Java client
Java
157
star
8

travel

Starter Kit For Building Graph Database Go and Browser Apps Using Dgraph
Go
126
star
9

graphoverflow

Run the entire StackOverflow on Dgraph. Work in progress.
JavaScript
116
star
10

ratel

Dgraph Data Visualizer and Cluster Manager
JavaScript
114
star
11

wisemonk

Bot to move conversations from Slack to Discourse.
Go
106
star
12

benchmarks

Run benchmarks with RDF data
Go
97
star
13

badger-bench

Benchmarks of Badger
Go
92
star
14

graphql-sample-apps

This repository contains sample GraphQL applications powered by Dgraph.
JavaScript
73
star
15

dgraph-js-http

A JavaScript HTTP client for Dgraph
TypeScript
67
star
16

gru

Help identify the right minions
JavaScript
46
star
17

dgraph-lambda

TypeScript
39
star
18

tutorial

A Tour of Dgraph
HTML
38
star
19

dgraph-docs

A native GraphQL Database with a graph backend
Shell
35
star
20

charts

Helm charts for Dgraph
Shell
32
star
21

ingressutil

Utils for building an ingress controller
Go
31
star
22

flock

Twitter on Dgraph
Go
31
star
23

dgraph.net

Official Dgraph .NET Client
C#
28
star
24

dgraph-operator

Dgraph Operator creates/configures/manages Dgraph clusters atop Kubernetes
Go
23
star
25

hugo-dgraph-theme

Hugo theme used for our blog
CSS
22
star
26

typhoon-ui

πŸŒ€ typhoon-ui: Token-based React component library built using emotion.
TypeScript
14
star
27

mediawiki-dgraph-skin

Mediawiki skin used by Dgraph wiki.
CSS
13
star
28

hugo-docs

Theme for Dgraph documentation built via Hugo
HTML
13
star
29

experiments

Go
13
star
30

graphql-kanban

Project management app written with Dgraph and Apollo Client
TypeScript
13
star
31

svelte-urql-example

Svelte
12
star
32

slash-graphql-cli

Command Line Tools for Slash GraphQL
TypeScript
11
star
33

auth-webinar

Resources for Serverless Authentication + Authorization Webinar
JavaScript
9
star
34

discuss-tutorial

Discuss clone app built for a tutorial
CSS
8
star
35

tove

Crash vulnerability tests for Badger using the ALICE framework
Go
7
star
36

graphql-dgraph-web

GraphQL Web
SCSS
7
star
37

graphql-asia-workshop-2020

JavaScript
7
star
38

rails-graphoverflow

Showcase of using Dgraph in Ruby on Rails
Ruby
7
star
39

dgraph-react-todomvc

TodoMVC in React with GraphQL, Dgraph, NoSQL graph database
JavaScript
7
star
40

grpc-proxy

Go
6
star
41

webinar-1-twitter-graphql

JavaScript
5
star
42

react-slash-graphql-starter

JavaScript
4
star
43

gatsby-dgraph-graphql

A simple blog app with Gatsby and Dgraph's GraphQL API
JavaScript
4
star
44

auth0-integration

Slash GraphQL + Auth0 integration quick start
JavaScript
4
star
45

tweetsletter-workshop

JavaScript
4
star
46

dgraph-day

Learn graph database best practices from experts using Dgraph at scale in production and building the next generation of apps. Sign up for the FREE virtual event, April 15-16th.
4
star
47

hello

Dgraph's take on Go present
JavaScript
3
star
48

live-demo

JavaScript
3
star
49

dev-jokes

Slash powered DevJokes Application
JavaScript
3
star
50

Install-Dgraph

Dgraph installation scripts
Shell
2
star
51

workshop

Dgraph workshop: Build a Twitter graph with Dgraph!
2
star
52

video-tutorial-todo

JavaScript
2
star
53

vlg

Dgraph's Very Large Graph (WIP) Project
Go
2
star
54

website-old

Website code for dgraph.io
CSS
2
star
55

tudo-tutorial

Tutorial for building a GraphQL React app with Slash GraphQL
JavaScript
2
star
56

dgraph-io.github.io

Dgraph blog
HTML
2
star
57

cloud-docs

Documentation for Dgraph Cloud
Shell
1
star
58

discuss-tutorial-vue

CSS
1
star
59

developer-connect-sessions

1
star