dgraph 0.2.0 copy "dgraph: ^0.2.0" to clipboard
dgraph: ^0.2.0 copied to clipboard

outdated

Dgraph Dart client which communicates with the server using gRPC.

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",
    };
    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
      }
    }
    """;
    api.Response response =
        await txn.QueryWithVars(clientContext, query, {"\$a": "Alice"});
    print(
        "Response: ${latin1.decode(base64.decode(json.decode(response.writeToJson())['1']))}");

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

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

Documentation

Documentation

Publisher

verified publishermarceloneppel.dev

Weekly Downloads

Dgraph Dart client which communicates with the server using gRPC.

Homepage

License

unknown (license)

Dependencies

grpc, protobuf

More

Packages that depend on dgraph