TinyDgraphClient: A Dgraph Client for .NET

I am currently working on a project using Dgraph:

Dgraph is a horizontally scalable and distributed graph database, providing ACID transactions, consistent replication and linearizable reads. It's built from ground up to perform for a rich set of queries. Being a native graph database, it tightly controls how the data is arranged on disk to optimize for query performance and throughput, reducing disk seeks and network calls in a cluster.

For the project I want to have a Dgraph .NET client implementation, that is as close as possible to the Protobuf Schema provided by the Dgraph Team.

The idea is to learn more about Dgraph. And I want to have a .NET client, that is easy to update when the Protobuf API is updated.

TinyDgraphClient is a thin wrapper for the Dgraph API. It is based on the great Dgraph Dart and JavaScript implementations:

You can find more information about Dgraph here:

Installing TinyDgraphClient

You can use NuGet to install TinyDgraphClient. Run the following command in the Package Manager Console.

PM> Install-Package TinyDgraphClient

Using the DGraphClient

Create the Schema

public static async Task Main()
{
    var client = new DGraphClient("127.0.0.1", 9080, ChannelCredentials.Insecure);

    // Drop All Data for Tests:
    await client.AlterAsync(new Operation { DropAll = true }, CancellationToken.None);

    // Create the Schema:
    await client.AlterAsync(new Operation { Schema = Query.Schema }, CancellationToken.None);

    // Insert Data:
    ...
}

Run a Mutation

Running a Mutation should be done in a Transaction. The following example shows how to get a new Transaction from the DGraphClient and use it to perform a Mutation in Dgraph:

// Get a new Transaction:
var transaction = client.NewTxn();

// Create a Mutation:
var mutation = new Mutation();

// Create NQuads to add to the mutation:
var nquads = new List<NQuad>();

nquads.Add(new NQuad { Subject = "subject", Predicate = "predicate", ObjectValue = new Value { StrVal = "value" } });

// Set the NQuads for the Mutation:
mutation.Set.AddRange(nquads);

// Tell Dgraph to commit this Mutation instantly:
mutation.CommitNow = true;

// And mutate the data:
await transaction.MutateAsync(mutation, cancellationToken);

License

The library is released under terms of the MIT License: