dgraph 0.5.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new47

dgraph

Dgraph Dart client which communicates with the server using gRPC.

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

Table of contents

Using a client

Create a client

dgraphClient object can be initialised by passing it a list of api.DgraphApi 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.

DgraphRpcClient rpcClient =
    DgraphRpcClient("localhost", 9080, const ChannelCredentials.insecure());
Dgraph dgraphClient = dgraph.NewDgraphClient(api.DgraphApi(rpcClient));

Alter the database

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

api.Operation operation = api.Operation();
operation.schema = """
name: string @index(exact) .
""";
await dgraphClient.Alter(clientContext, operation);

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.

Create a transaction

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

It is a good practice to call txn.Discard() on a finally block after it is initialized. Calling txn.Discard() after txn.Commit() is a no-op and you can call txn.Discard() multiple times with no additional side-effects.

Txn txn;
ClientContext clientContext = ClientContext();
try {
  txn = dgraphClient.NewTxn();
  // Perform some queries and mutations.
  // Commit the transaction.
} finally {
  txn.Discard(clientContext);
}

Run a mutation

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

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

Map<String, dynamic> p = {
  "uid": "_:alice",
  "name": "Alice",
};
List<int> pb = utf8.encode(json.encode(p));
api.Mutation mutation = api.Mutation();
mutation.setJson = pb;
api.Assigned assigned = await txn.Mutate(clientContext, mutation);
print("Assigned: $assigned");
// uids: {alice : 0x1}

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

Run a query

You can run a query by calling txn.Query(clientContext, query). You will need to pass in a GraphQL+- 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(clientContext, query, vars) with the variables map as third argument.

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

String query = """
query all(\$a: string) {
  all(func: eq(name, \$a)) {
    name
  }
}
""";
api.Response response =
    await txn.QueryWithVars(clientContext, query, {"\$a": "Alice"});
print(
    "Response: ${latin1.decode(base64.decode(json.decode(response.writeToJson())['1']))}");
// {"all":[{"name":"Alice"}]}

Commit a transaction

A transaction can be committed using the txn.Commit(clientContext) 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 txn;
ClientContext clientContext = ClientContext();
try {
  txn = dgraphClient.NewTxn();
  // Perform some queries and mutations.
  txn.Commit(clientContext);
} catch (e) {
  // Retry or handle error
}

0.5.0

  • Changed Commit and Discard methods to async
  • Added dropAttr to example

0.4.0

  • Improved README
  • Ran darfmt on dart file

0.3.0

  • Fixed 'Name types using UpperCamelCase' suggestion on package analysis
  • Changed grpc package version to 0.6.6 to make dartdoc works

0.2.0

  • Improved README
  • Added dropAll call to example
  • Ran darfmt on dart files

0.1.0

  • Initial release

example/example.dart

import 'package:dgraph/api.dart';
import 'package:dgraph/dgraph.dart';
import 'package:dgraph/protos/api/api_pb.dart' as api;
import 'package:dgraph/txn.dart';
import 'package:grpc/grpc.dart';
import 'package:protobuf/protobuf.dart';
import 'dart:convert';

void main(List<String> arguments) async {
  // Create a client
  DgraphRpcClient rpcClient =
      DgraphRpcClient("localhost", 9080, const ChannelCredentials.insecure());
  Dgraph dgraphClient = dgraph.NewDgraphClient(api.DgraphApi(rpcClient));

  Txn txn;
  ClientContext clientContext = ClientContext();
  try {
    // Alter the database
    api.Operation operation = api.Operation();
    operation.schema = """
    name: string @index(exact) .
    """;
    await dgraphClient.Alter(clientContext, operation);

    // Create a transaction
    txn = dgraphClient.NewTxn();

    // Run a mutation
    Map<String, dynamic> p = {
      "uid": "_:alice",
      "name": "Alice",
      "age": 18,
    };
    List<int> pb = utf8.encode(json.encode(p));
    api.Mutation mutation = api.Mutation();
    mutation.setJson = pb;
    api.Assigned assigned = await txn.Mutate(clientContext, mutation);
    print("Assigned: $assigned");

    // Run a query
    String query = """
    query all(\$a: string) {
      all(func: eq(name, \$a)) {
        name
        age
      }
    }
    """;
    api.Response response =
        await txn.QueryWithVars(clientContext, query, {"\$a": "Alice"});
    print(
        "Response: ${latin1.decode(base64.decode(json.decode(response.writeToJson())['1']))}");

    // Commit a transaction
    await txn.Commit(clientContext);

    // Alter the database
    operation = api.Operation();
    operation.dropAttr = "age";
    await dgraphClient.Alter(clientContext, operation);

    // Create another transaction
    txn = dgraphClient.NewTxn();

    // Run the same query again
    response = await txn.QueryWithVars(clientContext, query, {"\$a": "Alice"});
    print(
        "Response: ${latin1.decode(base64.decode(json.decode(response.writeToJson())['1']))}");

    // Finish transaction without commit
    await txn.Discard(clientContext);

    // Alter the database
    operation = api.Operation();
    operation.dropAll = true;
    await dgraphClient.Alter(clientContext, operation);
  } catch (e) {
    print("Error: $e");
  } finally {
    if (txn != null) {
      await txn.Discard(clientContext);
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dgraph: ^0.5.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dgraph/dgraph.dart';
  
Version Uploaded Documentation Archive
0.5.0 Dec 4, 2018 Go to the documentation of dgraph 0.5.0 Download dgraph 0.5.0 archive
0.4.0 Dec 4, 2018 Go to the documentation of dgraph 0.4.0 Download dgraph 0.4.0 archive
0.3.0 Dec 4, 2018 Go to the documentation of dgraph 0.3.0 Download dgraph 0.3.0 archive
0.2.0 Dec 4, 2018 Go to the documentation of dgraph 0.2.0 Download dgraph 0.2.0 archive
0.1.0 Dec 3, 2018 Go to the documentation of dgraph 0.1.0 Download dgraph 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
91
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
47
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, other

Primary library: package:dgraph/dgraph.dart with components: io.

Health issues and suggestions

Document public APIs (-9.34 points)

468 out of 501 API elements (library, class, field or method) have no adequate dartdoc content. Good documentation improves code readability and discoverability through search.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
grpc 0.6.6 0.6.6 0.6.8
protobuf ^0.10.5 0.10.5
Transitive dependencies
async 2.0.8
charcode 1.1.2
collection 1.14.11
convert 2.0.2
crypto 2.0.6
fixnum 0.10.9
googleapis_auth 0.2.6
http 0.12.0
http2 0.1.9
http_parser 3.1.3
meta 1.1.6
path 1.6.2
source_span 1.4.1
string_scanner 1.0.4
typed_data 1.1.6
Dev dependencies
test ^1.5.1