{"id":25070268,"url":"https://github.com/hypermodeinc/dgraph.net","last_synced_at":"2025-04-14T20:22:15.021Z","repository":{"id":44202472,"uuid":"246474608","full_name":"hypermodeinc/dgraph.net","owner":"hypermodeinc","description":"Official Dgraph .NET Client","archived":false,"fork":false,"pushed_at":"2025-04-09T22:11:26.000Z","size":424,"stargazers_count":33,"open_issues_count":1,"forks_count":15,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-04-09T23:22:25.670Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","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/hypermodeinc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-11T04:29:35.000Z","updated_at":"2025-04-09T22:11:30.000Z","dependencies_parsed_at":"2024-04-12T14:42:56.436Z","dependency_job_id":"4f90d971-dca3-485c-a7b1-11555b889967","html_url":"https://github.com/hypermodeinc/dgraph.net","commit_stats":{"total_commits":123,"total_committers":13,"mean_commits":9.461538461538462,"dds":"0.31707317073170727","last_synced_commit":"07cf1aa53f620d158b6bc2ff975754090b5ef3c4"},"previous_names":["hypermodeinc/dgraph.net","dgraph-io/dgraph.net"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hypermodeinc%2Fdgraph.net","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hypermodeinc%2Fdgraph.net/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hypermodeinc%2Fdgraph.net/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hypermodeinc%2Fdgraph.net/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hypermodeinc","download_url":"https://codeload.github.com/hypermodeinc/dgraph.net/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248952601,"owners_count":21188473,"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":[],"created_at":"2025-02-06T21:09:07.504Z","updated_at":"2025-04-14T20:22:14.997Z","avatar_url":"https://github.com/hypermodeinc.png","language":"C#","readme":"# Dgraph.net [![Nuget](https://img.shields.io/nuget/v/dgraph)](https://www.nuget.org/packages/Dgraph)\n\nThis client follows the [Dgraph Go client][goclient] closely.\n\n[goclient]: https://github.com/dgraph-io/dgo\n\nBefore using this client, we highly recommend that you go through [dgraph.io/docs], and understand\nhow to run and work with Dgraph.\n\n**Use [Discuss Issues](https://discuss.dgraph.io/tags/c/issues/35/dgraphnet) for reporting issues\nabout this repository.**\n\n[dgraph.io/docs]: https://dgraph.io/docs\n\n## Table of contents\n\n- [Install](#install)\n- [Supported Versions](#supported-versions)\n- [Using a client](#using-a-client)\n  - [Creating a client](#creating-a-client)\n  - [Login into a namespace](#login-into-a-namespace)\n  - [Connecting To Dgraph Cloud](#connecting-to-dgraph-cloud)\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  - [Query with RDF response](#query-with-rdf-response)\n  - [Running an Upsert: Query + Mutation](#running-an-upsert-query--mutation)\n  - [Running Conditional Upsert](#running-conditional-upsert)\n  - [Committing a transaction](#committing-a-transaction)\n  - [Setting Metadata Headers](#setting-metadata-headers)\n\n## Install\n\nInstall using nuget:\n\n```sh\ndotnet add package Dgraph\n```\n\n\u003e WARNING: Be aware that there may be other .NET packages with similar names. To verify the official\n\u003e package, please visit https://www.nuget.org/packages/Dgraph. Make sure you are using the correct\n\u003e and official package to avoid potential confusion.\n\n## Supported Versions\n\nDepending on the version of Dgraph that you are connecting to, you will have to use a different\nversion of this client.\n\n| Dgraph version | Dgraph.net version  |\n| -------------- | ------------------- |\n| dgraph 21.X.Y  | Dgraph.net 21.3.1.2 |\n| dgraph 23.X.Y  | Dgraph.net 23.0.0   |\n\n## Using a Client\n\nMost client and transaction methods return a `Result` object from the\n[FluentResults](https://github.com/altmann/FluentResults) library. You can check the status of the\nresult by using `Result.IsSuccess` or `Result.IsFailed`. Exceptions are contained inside\n`Result.Errors`.\n\n### Creating a Client\n\nAn `IDgraphClient` can be created with a list of `GrpcChannel` as objects. Connecting to multiple\nDgraph servers in the same cluster allows for better distribution of workload.\n\nThe following code snippet shows just one connection.\n\n```c#\nusing Dgraph;\nusing Grpc.Net.Client;\n\nGrpcChannel channel = GrpcChannel.ForAddress(\"http://localhost:9080\");\nusing var dgraphClient = DgraphClient.Create(channel);\n```\n\nThe connection can be configured by using `GrpcChannelOptions`.\n\n```c#\nusing Grpc.Net.Client;\n\nvar options = new GrpcChannelOptions\n{\n    CompressionProviders = \u003c...\u003e, // List of Grpc ICompressionProvider\n    Credentials = Grpc.Core.ChannelCredentials.Create(\u003c...\u003e)\n};\nGrpcChannel channel = GrpcChannel.ForAddress(\"http://localhost:9080\", options);\n```\n\n### Login into a namespace\n\nIf your server has Access Control Lists enabled (Dgraph v1.1 or above), the client must be logged in\nfor accessing data. Use `Login` to obtain and remember access and refresh JWTs.\n\n```c#\nvar response = await dgraphClient.Login(\"user\", \"password\");\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\nAll subsequent operations via the logged in client will send along the stored access token.\n\nIf your server additionally has namespaces (Dgraph v21.03 or above), use `LoginIntoNamespace`.\n\n```c#\nvar response = await dgraphClient.LoginIntoNamespace(\"groot\", \"password\", 123);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\n### Connecting To Dgraph Cloud\n\nUse `DgraphCloudChannel.Create` to create a GrpcChannel that connects to a Dgraph Cloud backend.\n\n`DgraphCloudChannel.Create` can accept GraphQL or gRPC URIs from\n[Dgraph Cloud](https://cloud.dgraph.io/), but it will always connect via gRPC.\n\n```c#\nusing Dgraph;\nusing Grpc.Net.Client;\n\nstring ENDPOINT = \"\u003c...\u003e\";\nstring API_KEY = \"\u003c...\u003e\";\n\nGrpcChannel channel = DgraphCloudChannel.Create(ENDPOINT, API_KEY);\nusing var dgraphClient = DgraphClient.Create(channel);\n```\n\n### Altering the Database\n\nTo set the schema, create an instance of `Dgraph.Api.Operation` and use the `Alter` endpoint.\n\n```c#\nusing Dgraph;\n\nvar operation = new Api.Operation {\n    Schema = \"name: string @index(exact) .\"\n};\nvar response = await dgraphClient.Alter(operation);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\n`Operation` contains other fields as well, including `DropAttr` and `DropAll`. `DropAll` is useful\nif you wish to discard all the data without bringing the instance down. `DropAttr` is used to drop\nall the data related to a predicate.\n\nStarting in Dgraph version 20.03.0, indexes can be computed in the background. You can set the\n`RunInBackground` field to `true` like so:\n\n```c#\nusing Dgraph;\n\nvar operation = new Api.Operation {\n    Schema = \"name: string @index(exact) .\",\n    RunInBackground = true\n};\nvar response = await dgraphClient.Alter(operation);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\n### Creating a Transaction\n\nTo create a transaction, call the `IDgraphClient.NewTransaction()` method, which returns a new\n`ITransaction`. This operation incurs no network overhead.\n\nTo ensure the `ITransaction` is properly disposed after it has completed, use the `using` keyword.\n\n```c#\nusing var transaction = dgraphClient.NewTransaction();\nvar transactionResponse = await transaction.Mutate(...);\nif (transactionResponse.IsFailed) {\n    // Handle errors\n}\nvar response = await transaction.Commit();\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\nRead-only transactions can be created by calling the `IDgraphClient.NewReadOnlyTransaction` method.\nRead-only transactions are useful to increase read speed because they can circumvent the usual\nconsensus protocol. Read-only transactions cannot contain mutations. There is nothing to dispose for\na `ReadOnlyTransaction` object, so it does not implement `IDisposable`.\n\n```c#\nvar readOnlyTransaction = dgraphClient.NewReadOnlyTransaction();\nvar response = await readOnlyTransaction.Query(...);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\n### Running a Mutation\n\n`ITransaction.Mutate` runs a mutation. It takes a `Dgraph.Api.Mutation` or a `MutationBuilder`. You\ncan set the data using JSON or RDF N-Quad format.\n\nTo use JSON, use the fields `SetJson` and `DeleteJson`, which accept a string representing the nodes\nto be added or removed respectively (either as a JSON map or a list). You can use any library to\nserialize objects to a JSON string, such as [JSON.NET](https://www.newtonsoft.com/json).\n\nTo use RDF, use the fields `SetNquads` and `DelNquads`, which accept a string representing the valid\nRDF triples (one per line) to be added or removed respectively.\n\n`Dgraph.Api.Mutation` also contains the `Set` and `Del` fields which accept a list of RDF triples\nthat have already been parsed into our internal format. As such, these fields are mainly used\ninternally and you should use the `SetNquads` and `DelNquads` fields instead if you plan on using\nRDF.\n\nWhile you can construct a `Dgraph.Api.Mutation` object directly, it is easier to let\n`MutationBuilder` handle implementation details like converting `string` to\n`Google.Protobuf.ByteString`.\n\n```c#\nusing Dgraph;\n\nvar transaction = dgraphClient.NewTransaction();\nvar mutation = new MutationBuilder().SetJson(\"...\").CommitNow();\nvar response = await transaction.Mutate(mutation);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\nIf you want to commit a mutation without querying anything further, use `MutationBuilder.CommitNow`\nto indicate that the transaction must be immediately committed.\n\n```c#\nvar mutation = new MutationBuilder().SetJson(\"...\").CommitNow();\n```\n\nMultiple mutations can be run in a single request using `ITransaction.Do` and `RequestBuilder`. To\nimmediately commit the request, use `RequestBuilder.CommitNow`.\n\n```c#\nusing Dgraph;\n\nvar transaction = dgraphClient.NewTransaction();\nvar request = new RequestBuilder().WithMutations(\n    new MutationBuilder().SetJson(\"...\"),\n    new MutationBuilder().SetJson(\"...\"),\n    new MutationBuilder().SetJson(\"...\"),\n);\nvar response = await transaction.Do(request);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\nKeep in mind that if you do not use `RequestBuilder.CommitNow` or `MutationBuilder.CommitNow`, you\nwill still need to manually commit the transaction using `ITransaction.Commit`.\n\nCheck out the example in `source/Dgraph.tests.e2e/TransactionTest.cs`.\n\n### Running a Query\n\nYou can run a query by calling `ITransaction.Query`. You will need to pass in a DQL query string. If\nyou want to pass an additional map of any variables that you might want to set in the query, call\n`ITransaction.QueryWithVars` with the variables dictionary as the second argument.\n\nLet’s run the following query with a variable $a:\n\n```c#\nvar query = @\"\n  query all($a: string) {\n    all(func: eq(name, $a)) {\n      name\n    }\n  }\";\nvar varMap = new Dictionary\u003cstring, string\u003e { { \"$a\", \"Alice\" } };\nvar response = await transaction.QueryWithVars(query, varMap);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\nYou can also use `ITransaction.Do` to run a query.\n\n```c#\nvar query = @\"\n  query all($a: string) {\n    all(func: eq(name, $a)) {\n      name\n    }\n  }\";\nvar varMap = new Dictionary\u003cstring, string\u003e { { \"$a\", \"Alice\" } };\nvar request = new RequestBuilder().WithQuery(query).WithVars(varMap);\nvar response = await transaction.Do(request);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\nWhen running a schema query for predicate `name`, the schema response is found in the `Json` field\nof the transaction response:\n\n```c#\nvar query = @\"\n  schema(pred: [name]) {\n    type\n    index\n    reverse\n    tokenizer\n    list\n    count\n    upsert\n    lang\n  }\";\nvar response = await transaction.Query(query);\nif (response.IsSuccess) {\n    Console.WriteLine(response.Value.Json);\n}\n```\n\n### Query with RDF response\n\nYou can get query results as a RDF response by calling `ITransaction.QueryRdf`. The `Rdf` field in\nthe response has the encoded RDF result.\n\n**Note:** If you are querying for only `uid` values, use a JSON format response.\n\n```c#\n// Query the balance for Alice and Bob.\nvar query = @\"\n  {\n    all(func: anyofterms(name, \"\"Alice Bob\"\")) {\n      name\n      balance\n    }\n  }\";\nvar response = await transaction.QueryRDF(query);\nif (response.IsSuccess) {\n    // \u003c0x17\u003e \u003cname\u003e \"Alice\" .\n    // \u003c0x17\u003e \u003cbalance\u003e 100 .\n    Console.WriteLine(response.Value.Rdf);\n}\n```\n\n`ITransaction.QueryRDFWithVars` is also available when you need to pass values for variables used in\nthe query.\n\n### Running an Upsert: Query + Mutation\n\nThe `ITransaction.Do` method allows you to run upserts consisting of one query and one mutation.\nVariables can be defined in the query and used in the mutation.\n\nTo know more about upsert, we highly recommend going through the docs at\n[Upsert Block](https://dgraph.io/docs/howto/upserts/)\n\n```c#\nvar query = @\"\n  query {\n    user as var(func: eq(email, \"\"wrong_email@dgraph.io\"\"))\n  }\";\nvar mutation = new MutationBuilder().SetNquads(\"uid(user) \u003cemail\u003e \\\"correct_email@dgraph.io\\\" .\");\nvar request = new RequestBuilder()\n    .WithQuery(query)\n    .WithMutations(mutation)\n    .CommitNow();\n\n// Update email only if matching uid found.\nvar response = await transaction.Do(request);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\n### Running Conditional Upsert\n\nThe upsert block also allows specifying a conditional block using and `@if` directive. The mutation\nis executed only when the specified condition is true. If the condition is false, the mutation is\nsilently ignored.\n\nSee more about Conditional Upsert\n[here](https://dgraph.io/docs/dql/dql-syntax/dql-mutation/#conditional-upsert).\n\n```c#\nvar query = @\"\n  query {\n    user as var(func: eq(email, \"\"wrong_email@dgraph.io\"\"))\n  }\";\nvar mutation = new MutationBuilder()\n    .Cond(\"@if(eq(len(user), 1))\") // Only mutate if \"wrong_email@dgraph.io\" belongs to single user.\n    .SetNquads(\"uid(user) \u003cemail\u003e \"\"correct_email@dgraph.io\"\" .\");\nvar request = new RequestBuilder()\n    .WithQuery(query)\n    .WithMutations(mutation)\n    .CommitNow();\n\n// Update email only if exactly one matching uid found.\nvar response = await transaction.Do(request);\nif (response.IsFailed) {\n    // Handle errors\n}\n```\n\n### Committing a Transaction\n\nA transaction can be committed using the `ITransaction.Commit` method. If your transaction never\nsubmitted any mutations, then `ITransaction.Commit` is not necessary.\n\nAn error will be returned if other transactions running concurrently modify the same data that was\nmodified in this transaction. It is up to the user to retry transactions when they fail.\n\n```c#\nusing var transaction = dgraphClient.NewTransaction();\n\n// Perform some queries and mutations.\n\nvar response = await transaction.Commit();\nif (response.IsFailed) {\n    // Retry or handle errors\n}\n```\n\n### Setting Metadata Headers\n\nMetadata headers such as authentication tokens can be set through the `options` argument of gRPC\nmethods. Below is an example of how to set a header named \"auth-token\".\n\n```c#\nusing Grpc.Core;\n\nvar metadata = new Metadata\n{\n    { \"auth-token\", \"the-auth-token-value\" }\n};\nvar options = new CallOptions(headers: metadata);\nclient.Alter(operation, options);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhypermodeinc%2Fdgraph.net","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhypermodeinc%2Fdgraph.net","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhypermodeinc%2Fdgraph.net/lists"}