{"id":22288978,"url":"https://github.com/marceloneppel/dgraph","last_synced_at":"2025-07-28T22:33:06.645Z","repository":{"id":47041102,"uuid":"160197940","full_name":"marceloneppel/dgraph","owner":"marceloneppel","description":"Dgraph Dart client which communicates with the server using gRPC.","archived":false,"fork":false,"pushed_at":"2021-10-28T02:30:45.000Z","size":61,"stargazers_count":27,"open_issues_count":5,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2023-03-06T00:34:31.128Z","etag":null,"topics":["dart","dart-library","dartlang","dgraph","dgraph-client","flutter","flutter-package","graph-database","grpc"],"latest_commit_sha":null,"homepage":"https://pub.dartlang.org/packages/dgraph","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marceloneppel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-03T13:47:48.000Z","updated_at":"2022-09-12T12:53:48.000Z","dependencies_parsed_at":"2022-08-27T16:31:05.221Z","dependency_job_id":null,"html_url":"https://github.com/marceloneppel/dgraph","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marceloneppel%2Fdgraph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marceloneppel%2Fdgraph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marceloneppel%2Fdgraph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marceloneppel%2Fdgraph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marceloneppel","download_url":"https://codeload.github.com/marceloneppel/dgraph/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227962295,"owners_count":17847912,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","dart-library","dartlang","dgraph","dgraph-client","flutter","flutter-package","graph-database","grpc"],"created_at":"2024-12-03T17:07:45.029Z","updated_at":"2024-12-03T17:07:45.788Z","avatar_url":"https://github.com/marceloneppel.png","language":"Dart","readme":"[![Build Status](https://app.travis-ci.com/marceloneppel/dgraph.svg?branch=master)](https://app.travis-ci.com/github/marceloneppel/dgraph)\n\n# dgraph\n[Dgraph](https://dgraph.io) Dart client which communicates with the server using [gRPC](https://grpc.io/).\n\nBefore using this client, we highly recommend that you go through [tour.dgraph.io] and [docs.dgraph.io]\nto understand how to run and work with Dgraph.\n\n[docs.dgraph.io]:https://docs.dgraph.io\n[tour.dgraph.io]:https://tour.dgraph.io\n\n\n## Table of contents\n\n- [Supported Versions](#supported-versions)\n- [Using a client](#using-a-client)\n  - [Creating a client](#creating-a-client)\n  - [Altering the database](#altering-the-database)\n  - [Creating a transaction](#creating-a-transaction)\n  - [Running a mutation](#running-a-mutation)\n  - [Running a query](#running-a-query)\n  - [Committing a transaction](#committing-a-transaction)\n- [Development](#development)\n  - [Running tests](#running-tests)\n  - [Updating protobuf](#updating-protobuf)\n\n## Supported Versions\n\nDepending on the version of Dgraph that you are connecting to, you will have to\nuse a different version of this client.\n\n| Dgraph version | dgraph client version | \n|:--------------:|:---------------------:|\n|  dgraph 1.0.X  |  dgraph client 0.5.0  |\n|  dgraph 1.1.X  |  dgraph client 1.1.X  |\n\nNote: One of the most important API breakages from dgraph client v0.5.0 to v1.1.X is in\nthe function `Txn.Mutate`. This function returns an `api.Assigned`\nvalue until v0.5.0 but an `api.Response` in v1.1.X.\n\n## Using a client\n\n### Create a client\n\n`dgraphClient` object can be initialised by passing it a list of `api.DgraphApi` clients as\nvariadic arguments. Connecting to multiple Dgraph servers in the same cluster allows for better\ndistribution of workload.\n\nThe following code snippet shows just one connection.\n\n```dart\nDgraphRpcClient rpcClient =\n    DgraphRpcClient(\"localhost\", 9080, const ChannelCredentials.insecure());\nDgraph dgraphClient = dgraph.NewDgraphClient(api.DgraphApi(rpcClient));\n```\n\n### Altering the database\n\nTo set the schema, create an instance of `api.Operation` and use the `Alter` endpoint.\n\n```dart\napi.Operation operation = api.Operation();\noperation.schema = \"\"\"\nname: string @index(exact) .\n\"\"\";\nawait dgraphClient.Alter(clientContext, operation);\n```\n\n`Operation` contains other fields as well, including `dropAttr` and `dropAll`.\n`dropAll` is useful if you wish to discard all the data, and start from a clean\nslate, without bringing the instance down. `dropAttr` is used to drop all the data\nrelated to a predicate.\n\n### Creating a transaction\n\nTo create a transaction, call `dgraphClient.NewTxn()`, which returns a `Txn` object. This\noperation incurs no network overhead.\n\nIt is a good practice to call `txn.Discard()` on a finally block after it is initialized.\nCalling `txn.Discard()` after `txn.Commit()` is a no-op and you can call `txn.Discard()` multiple\ntimes with no additional side-effects.\n\n```dart\nTxn txn;\nClientContext clientContext = ClientContext();\ntry {\n  txn = dgraphClient.NewTxn();\n  // Perform some queries and mutations.\n  // Commit the transaction.\n} finally {\n  txn.Discard(clientContext);\n}\n```\n\nRead-only transactions can be created by calling `dgraphClient.NewReadOnlyTxn()`. Read-only\ntransactions are useful to increase read speed because they can circumvent the\nusual consensus protocol. Read-only transactions cannot contain mutations and\ntrying to call `txn.Commit()` will result in an error. Calling `txn.Discard()`\nwill be a no-op.\n\n### Running a mutation\n\n`txn.Mutate(clientContext, mutation)` runs a mutation. It takes in a `ClientContext` and a `api.Mutation`\nobject. You can set the data using JSON or RDF N-Quad format.\n\nTo use JSON, use the fields setJson and deleteJson, which accept an encoded string\nrepresenting the nodes to be added or removed respectively (either as a JSON map\nor a list). To use RDF, use the fields setNquads and delNquads, which accept\na string representing the valid RDF triples (one per line) to added or removed\nrespectively. This protobuf object also contains the set and del fields which\naccept a list of RDF triples that have already been parsed into our internal\nformat. As such, these fields are mainly used internally and users should use\nthe setNquads and delNquads instead if they are planning on using RDF.\n\nWe define a Map to represent a Person and convert an instance of it to use with `Mutation`\nobject.\n```dart\nMap\u003cString, dynamic\u003e p = {\n  \"uid\": \"_:alice\",\n  \"name\": \"Alice\",\n};\nList\u003cint\u003e pb = utf8.encode(json.encode(p));\napi.Mutation mutation = api.Mutation();\nmutation.setJson = pb;\napi.Request request = api.Request();\nrequest.mutations.add(mutation);\napi.Response response = await txn.Mutate(clientContext, request);\nprint(\"Response: ${response.uids}\");\n// {alice: 0x5}\n```\n\nSometimes, you only want to commit a mutation, without querying anything further.\nIn such cases, you can use `mutation.commitNow = true` to indicate that the\nmutation must be immediately committed.\n\n### Running a query\n\nYou can run a query by calling `txn.Query(clientContext, query)`. You will need to pass in a GraphQL+- query string. If\nyou want to pass an additional map of any variables that you might want to set in the query, call\n`txn.QueryWithVars(clientContext, query, vars)` with the variables map as third argument.\n\nLet's run the following query with a variable $a:\n```dart\nString query = \"\"\"\nquery all(\\$a: string) {\n  all(func: eq(name, \\$a)) {\n    name\n  }\n}\n\"\"\";\napi.Response response =\n    await txn.QueryWithVars(clientContext, query, {\"\\$a\": \"Alice\"});\nprint(\"Response: ${utf8.decode(response.json)}\");\n// {\"all\":[{\"name\":\"Alice\"}]}\n```\n\nYou can also use `txn.Do` function to run a query.\n\n```dart\nrequest = api.Request();\nrequest.query = query;\nrequest.vars.addAll({\"\\$a\": \"Alice\"});\nresponse = await txn.Do(clientContext, request);\nprint(\"Response: ${utf8.decode(response.json)}\");\n// {\"all\":[{\"name\":\"Alice\"}]}\n```\n\nWhen running a schema query for predicate `name`, the schema response is found\nin the `json` field of `api.Response` as shown below:\n\n```dart\nString query = \"\"\"\nschema(pred: [name]) {\n  type\n  index\n  reverse\n  tokenizer\n  list\n  count\n  upsert\n  lang\n}\n\"\"\";\napi.Response response = await txn.Query(clientContext, query);\nprint(\"Response: ${utf8.decode(response.json)}\");\n// {\"schema\":[{\"predicate\":\"name\",\"type\":\"string\",\"index\":true,\"tokenizer\":[\"exact\"]}]}\n```\n\n### Committing a transaction\n\nA transaction can be committed using the `txn.Commit(clientContext)` method. If your transaction\nconsisted solely of calls to `txn.Query` or `txn.QueryWithVars`, and no calls to\n`txn.Mutate`, then calling `txn.Commit` is not necessary.\n\nAn error will be returned if other transactions running concurrently modify the same\ndata that was modified in this transaction. It is up to the user to retry\ntransactions when they fail.\n\n```dart\nTxn txn;\nClientContext clientContext = ClientContext();\ntry {\n  txn = dgraphClient.NewTxn();\n  // Perform some queries and mutations.\n  txn.Commit(clientContext);\n} catch (e) {\n  // Retry or handle error\n}\n```\n\n## Development\n\n### Running tests\n\nMake sure you have `dgraph` installed before you run the tests. This script will run the unit tests.\n\n```sh\npub run test --concurrency=1\n```\n\n### Updating protobuf\n\nTo update protobuf execute the following command from the project root:\n\n```sh\nbash lib/protos/api/regenerate-proto.sh\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarceloneppel%2Fdgraph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarceloneppel%2Fdgraph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarceloneppel%2Fdgraph/lists"}